Respect http header 'Accept-Language' on ocs and remote.php calls

This commit is contained in:
Thomas Müller 2015-02-24 17:42:26 +01:00
parent 33b11682f9
commit 2367797c17
3 changed files with 48 additions and 37 deletions

View File

@ -79,6 +79,48 @@ class OC_L10N implements \OCP\IL10N {
$this->lang = $lang; $this->lang = $lang;
} }
/**
* @param $app
* @return string
*/
public static function setLanguageFromRequest($app = null) {
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
if (is_array($app)) {
$available = $app;
} else {
$available = self::findAvailableLanguages($app);
}
// E.g. make sure that 'de' is before 'de_DE'.
sort($available);
$preferences = preg_split('/,\s*/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
foreach ($preferences as $preference) {
list($preferred_language) = explode(';', $preference);
$preferred_language = str_replace('-', '_', $preferred_language);
foreach ($available as $available_language) {
if ($preferred_language === strtolower($available_language)) {
if (is_null($app)) {
self::$language = $available_language;
}
return $available_language;
}
}
foreach ($available as $available_language) {
if (substr($preferred_language, 0, 2) === $available_language) {
if (is_null($app)) {
self::$language = $available_language;
}
return $available_language;
}
}
}
}
// Last try: English
return 'en';
}
/** /**
* @param $transFile * @param $transFile
* @param bool $mergeTranslations * @param bool $mergeTranslations
@ -403,41 +445,7 @@ class OC_L10N implements \OCP\IL10N {
return $default_language; return $default_language;
} }
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { return self::setLanguageFromRequest($app);
if(is_array($app)) {
$available = $app;
} else {
$available = self::findAvailableLanguages($app);
}
// E.g. make sure that 'de' is before 'de_DE'.
sort($available);
$preferences = preg_split('/,\s*/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
foreach($preferences as $preference) {
list($preferred_language) = explode(';', $preference);
$preferred_language = str_replace('-', '_', $preferred_language);
foreach($available as $available_language) {
if ($preferred_language === strtolower($available_language)) {
if (is_null($app)) {
self::$language = $available_language;
}
return $available_language;
}
}
foreach($available as $available_language) {
if (substr($preferred_language, 0, 2) === $available_language) {
if (is_null($app)) {
self::$language = $available_language;
}
return $available_language;
}
}
}
}
// Last try: English
return 'en';
} }
/** /**

View File

@ -39,8 +39,8 @@ try {
// load all apps to get all api routes properly setup // load all apps to get all api routes properly setup
OC_App::loadApps(); OC_App::loadApps();
// api calls always will return English // force language as given in the http request
\OC_L10N::forceLanguage('en'); \OC_L10N::setLanguageFromRequest();
OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo()); OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo());
} catch (ResourceNotFoundException $e) { } catch (ResourceNotFoundException $e) {

View File

@ -29,6 +29,9 @@ try {
exit; exit;
} }
// force language as given in the http request
\OC_L10N::setLanguageFromRequest();
$file=ltrim($file, '/'); $file=ltrim($file, '/');
$parts=explode('/', $file, 2); $parts=explode('/', $file, 2);