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 $context * @throws \InvalidArgumentException */ public function __construct($context) { self::$t = new Translator(); $parts = explode('.', $context); $extension = $parts[count($parts) - 1]; if ('php' == $extension) { $rt = $this->startupPHP($context); } elseif ('po' == $extension) { $rt = $this->startupPO($context); } elseif ('mo' == $extension) { $rt = $this->startupMO($context); } else { throw new InvalidArgumentException('$context must either .po file or .php file!'); } if ($rt) { Translator::initGettextFunctions(self::$t); } }
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>'; }