Add Internationalization
- Add Utils/L10n class - Add translator functions to PHP Renderer - Refactor web controllers to prevent duplicated code - Add locale middleware - Add translation file loading - Add i18n settings
This commit is contained in:
parent
13a2068a8b
commit
5b7bb030de
21 changed files with 537 additions and 245 deletions
|
|
@ -1,9 +1,40 @@
|
|||
<?php
|
||||
// Application middleware
|
||||
|
||||
// e.g: $app->add(new \Slim\Csrf\Guard);
|
||||
// configure middleware
|
||||
use Boronczyk\LocalizationMiddleware;
|
||||
|
||||
$app->add(new \Gofabian\Negotiation\NegotiationMiddleware([
|
||||
'accept' => ['text/html', 'application/json']
|
||||
]));
|
||||
|
||||
|
||||
$middleware = new LocalizationMiddleware(
|
||||
$container->get('settings')['i18n']['locales'],
|
||||
$container->get('settings')['i18n']['default']
|
||||
);
|
||||
|
||||
$middleware->setLocaleCallback(function (string $locale) use ($container) {
|
||||
$langPath = $container->get('settings')['i18n']['path'];
|
||||
|
||||
$translator = $container->get('l10n');
|
||||
if (is_a($translator, 'Gettext\GettextTranslator')) {
|
||||
// One of them will end up working, right?
|
||||
$translator->setLanguage($locale);
|
||||
$translator->setLanguage($locale . '.utf8');
|
||||
$translator->setLanguage($locale . '.UTF8');
|
||||
$translator->setLanguage($locale . '.utf-8');
|
||||
$translator->setLanguage($locale . '.UTF-8');
|
||||
|
||||
$translator->loadDomain('strings', $langPath);
|
||||
} else {
|
||||
/** @var $translator \Gettext\Translator */
|
||||
if (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo')) {
|
||||
$translator->loadTranslations(Gettext\Translations::fromMoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo'));
|
||||
} elseif (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.po')) {
|
||||
$translator->loadTranslations(Gettext\Translations::fromPoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.po'));
|
||||
}
|
||||
}
|
||||
});
|
||||
$middleware->setUriParamName('lang');
|
||||
|
||||
$app->add($middleware);
|
||||
Loading…
Add table
Add a link
Reference in a new issue