Improve translation logic

This commit is contained in:
Jay Trees 2022-03-22 09:12:08 +01:00
parent d53f23e830
commit 1c9eb48b64
2 changed files with 25 additions and 3 deletions

View file

@ -155,6 +155,24 @@ class Page
die();
}
}
/**
* Determine Locale
*/
$userLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locales = array_filter(
array_map(
function ($value) {
if ('po' === pathinfo($value, PATHINFO_EXTENSION)) {
return pathinfo($value, PATHINFO_FILENAME);
}
},
scandir(ROOT . '/translations')
)
);
$locale = \Locale::lookup($locales, $userLocale, true, 'en');
$this->language = $locale;
}
public function header(): void

View file

@ -11,12 +11,16 @@ use Gettext\Generator\MoGenerator;
function __(string $text)
{
$userLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$translationFilepath = ROOT . '/translations/' . $userLocale . '.po';
global $page;
/**
* Use file
*/
$translationFilepath = ROOT . '/translations/' . $page->language . '.po';
if (file_exists($translationFilepath)) {
$loader = new PoLoader();
$translations = $loader->loadFile(ROOT . '/translations/de_DE.po');
$translations = $loader->loadFile($translationFilepath);
$translation = $translations->find(null, $text);
if ($translation) {