public function register(Fol $app) { $app['middleware'] = function ($app) { $middleware = []; if ($app->has('users')) { $middleware[] = new Middlewares\DigestAuthentication($app['users']); } $middleware[] = new Middlewares\Expires(); $middleware[] = (new Middlewares\ErrorHandler())->catchExceptions()->statusCode(function ($code) { return $code > 400 && $code < 600; })->arguments($app); $middleware[] = new Middlewares\BasePath($app->getUrlPath()); $middleware[] = new Middlewares\TrailingSlash(); $middleware[] = new Middlewares\ContentType(); $middleware[] = new Middlewares\ContentLanguage(['en', 'gl', 'es']); $middleware[] = function ($request, $next) use($app) { $language = $request->getHeaderLine('Accept-Language'); $translator = new Translator(); $translator->loadTranslations(Translations::fromPoFile(dirname(dirname(__DIR__)) . '/locales/' . $language . '.po')); $prev = $translator->register(); $app['templates']->addData(['language' => $language]); $response = $next($request); if ($prev) { $prev->register(); } return $response; }; $middleware[] = (new Middlewares\MethodOverride())->parsedBodyParameter('method-override'); $middleware[] = (new Middlewares\Reader(dirname(dirname(__DIR__)) . '/assets'))->continueOnError(); $middleware[] = (new Middlewares\AuraRouter($app['router']))->arguments($app); return new Dispatcher($middleware); }; }
private static function loadParsed($locale) { # Also, we will work with gettext/gettext library # because PHP gones crazy when mo files are updated bindtextdomain(self::$config['domain'], self::$config['storage']); bind_textdomain_codeset(self::$config['domain'], 'UTF-8'); textdomain(self::$config['domain']); $file = dirname(self::getFile(self::$locale)) . '/' . self::$config['domain']; $translations = null; foreach (self::$config['formats'] as $format) { if ($translations = self::loadFormat($format, $file)) { break; } } if ($translations === null) { $translations = new Translations(); } Translator::initGettextFunctions((new Translator())->loadTranslations($translations)); }
/** * Load the i12n translation file. */ public function load_translation() { $translator = new \Gettext\Translator(); $i18n_path = realpath(__DIR__ . '/../../i18n/' . Config::$locale . '.po'); if (file_exists($i18n_path)) { $translations = \Gettext\Translations::fromPoFile($i18n_path); $translator->loadTranslations($translations); } Translator::initGettextFunctions($translator); }
/** * @param string $path */ public static function addTranslation($path) { $file = new File($path); if ($file->exists()) { $parts = explode('.', $path); $extension = $parts[count($parts) - 1]; switch ($extension) { case 'po': $translations = Translations::fromPoFile($path); break; case 'mo': $translations = Translations::fromMoFile($path); break; case 'php': $translations = Translations::fromPhpArrayFile($path); break; } self::$activeTranslation->mergeWith($translations); self::$t->loadTranslations(self::$activeTranslation); } }
public static function load() { $locale = self::$locale . '.UTF-8'; # IMPORTANT: locale must be installed in server! # sudo locale-gen es_ES.UTF-8 # sudo update-locale putenv('LANG=' . $locale); putenv('LANGUAGE=' . $locale); putenv('LC_MESSAGES=' . $locale); putenv('LC_PAPER=' . $locale); putenv('LC_TIME=' . $locale); putenv('LC_MONETARY=' . $locale); setlocale(LC_MESSAGES, $locale); setlocale(LC_COLLATE, $locale); setlocale(LC_TIME, $locale); setlocale(LC_MONETARY, $locale); bindtextdomain(self::$config['domain'], self::$config['storage']); bind_textdomain_codeset(self::$config['domain'], 'UTF-8'); textdomain(self::$config['domain']); # Also, we will work with gettext/gettext library # because PHP gones crazy when mo files are updated $path = dirname(self::getFile(self::$locale)); $file = $path . '/' . self::$config['domain']; if (is_file($file . '.php')) { $translations = $file . '.php'; } elseif (is_file($file . '.mo')) { $translations = Translations::fromMoFile($file . '.mo'); } elseif (is_file($file . '.po')) { $translations = Translations::fromPoFile($file . '.po'); } else { $translations = new Translations(); } Translator::initGettextFunctions((new Translator())->loadTranslations($translations)); }
<?php require '../vendor/autoload.php'; use Gettext\Translations; use Gettext\Translator; $locales = array('en', 'es', 'nb'); $text = "Hello, untranslated world!"; echo "<p>Original string: {$text}</p>"; foreach ($locales as $locale) { // START setup echo '<p>'; echo "Trying to set locale: {$locale}"; echo '<br>'; $translations = Translations::fromPoFile("../locale/{$locale}/LC_MESSAGES/test1.po"); echo '<br>'; $t = new Translator(); $t->loadTranslations($translations); Translator::initGettextFunctions($t); // END setup echo __($text); echo '<br>'; echo '</p>'; }
function np__($context, $original, $plural, $value) { return Translator::npgettext($context, $original, $plural, $value); }
/** * Returns the singular/plural translation of a string in a specific domain and context * * @param string $domain * @param string $context * @param string $original * @param string $plural * @param string $value * * @return string */ function dnp__($domain, $context, $original, $plural, $value) { $text = Translator::dnpgettext($domain, $context, $original, $plural, $value); if (func_num_args() === 5) { return $text; } $args = array_slice(func_get_args(), 5); return vsprintf($text, is_array($args[0]) ? $args[0] : $args); }
/** * load_gettext * Sets up our local gettext settings. * * @return void */ function load_gettext() { $lang = AmpConfig::get('lang'); $popath = AmpConfig::get('prefix') . '/locale/' . $lang . '/LC_MESSAGES/messages.po'; $t = new Translator(); if (file_exists($popath)) { $translations = Gettext\Translations::fromPoFile($popath); $t->loadTranslations($translations); } $t->register(); }