2016-12-11 3 views
0

リクエストロケールコードを取得するのに手伝いが必要です。アプリケーションロケールを認識させる(URLからロケールを取得する)

私の理論は、次のような、URLにロケールを定義することです:

`ロケールEN(英語)

OR

` http://localhost/fr/services/web

用のためhttp://localhost/en/services/web

ロケールfr(フランス語)

and then extract the locale from the URL and have the routes still work

値はここで$lang変数

itに代わるApplication Module Class内部の私の現在のコードです:

public function onBootstrap(MvcEvent $e) 
{ 
    $lang = 'it'; // this needs to reflect current request locale instead 
    $request = $e->getApplication()->getServiceManager()->get('Request'); 
    $translator = $e->getApplication()->getServiceManager()->get('MvcTranslator'); 
    $translator->addTranslationFile("phparray",Pluto::path('language',"$lang.php")); 
    $viewHelperManager = $e->getApplication()->getServiceManager()->get('ViewHelperManager'); 
    $viewHelperManager->get('translate')->setTranslator($translator); 
} 

My solution would be to have the variable $lang populated with the request locale、残りのURL部分

をルーティングに関連します私は、私のルーティングにもいくつかの修正が加えられると思っています。

答えて

0

おはよう!

ZF2または3を使用していますか? https://github.com/juriansluiman/SlmLocaleをご覧になることをお勧めします。これは、正しい方法で達成しようとしていることを正確に行います。私は数ヶ月前にZF3と互換性を持たせるように働くことを約束しましたが、現時点では時間を見つけることができませんでしたので、後で見ていきますが、ZF2でそれを使用するだけです。:)

0

したい正確なものがある(アプリに新しいモジュールを追加したくない場合):

ZF3について:https://gist.github.com/Itach1Uchixa/6ec75b8f2af47a0b63e3f52fa0285a24

ZF2の場合:https://gist.github.com/Itach1Uchixa/4840b004be2f2dcded19ec516e8aa6f1

例の設定ZF2用:

1ステップ。下記の行をservice_managerに設定してください。

'HttpRouter' => 'Application\Factory\LocalizedTreeRouteFactory' 

2ステップリスナーをイベントマネージャーに登録する

Application\Listener\RouteListener 

3ステップ。あなたの翻訳者の設定は

'translator' => array(
    // this will be default locale of route 
    'locale' => 'en_US', 
    // key must be locale that you want 
    'locales' => array(
     'en_US' => 'English', 
     'fr' => 'French', 
     'ru_RU' => 'Russian' 
    ), 
), 

のようなステップ3でなければならないロケールのキーを持っている必要がありますあなたはあなたのルートを作る上で、別の設定 に設定を使用するルートファクトリを変更することができるオプションです。

/your/route or en_US/your/route for English 
/fr/your/route for French 
/ru_RU/your/route for Russian 
関連する問題