Beispiel #1
3
 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);
     };
 }
Beispiel #2
0
 /**
  * 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);
 }
Beispiel #3
0
 /**
  * @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);
     }
 }
<?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>';
}
Beispiel #5
-1
/**
 * 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();
}