2016-09-05 6 views
0

から設定します。ルートからロケールを取得してサービスコントローラーで使用するにはどうすればよいですか?私はアプリのロケールから翻訳者のロケールを設定しようとしていますサービスコントローラーのSilex 2アプリケーションロケールをルート

$app['clients.controller'] = function() use ($app) { 
    $clientTranslator = new ClientTranslator; 
    $translations = $clientTranslator->getTranslations(); 
    foreach ($translations as $domain => $data) { 
     foreach ($data as $locale => $messages) { 
      $app['translator']->addResource('array', $messages, $locale, $domain); 
     } 
    } 
    $app['translator']->setLocale($app['locale']); 
    return new ClientController($app, $app['clients.model'], $app['clients.validator']); 
}; 

お知らせ:

は、ここに私のサービスコントローラです。ここ

そして、このサービスコントローラーを呼び出すルートです:今、何が起こっている

$app->mount ('/en', function ($client) use ($app) { 
    $app ['locale'] = "en"; 
    $client->match ('/{id}/edit', 'clients.controller:editAction') 
     ->assert ('id', '\d+') 
     ->method ('GET|POST'); 
    $client->match ('/add', 'clients.controller:addAction') 
     ->method ('GET|POST'); 
    $client->match ('/{id}', 'clients.controller:deleteAction') 
     ->assert ('id', '\d+') 
     ->method ('DELETE'); 
    $client->get('/', "clients.controller:indexAction"); 
}); 
$app->mount('/fr', function ($client) use ($app) { 
    $app['locale'] = "fr"; 
    $client->match ('/{id}/edit', 'clients.controller:editAction') 
     ->assert ('id', '\d+') 
     ->method ('GET|POST'); 
    $client->match ('/add', 'clients.controller:addAction') 
     ->method ('GET|POST'); 
    $client->match ('/{id}', 'clients.controller:deleteAction') 
     ->assert ('id', '\d+') 
     ->method ('DELETE'); 
    $client->get ('/', "clients.controller:indexAction"); 
}); 

は、アプリのロケールが常にフランス人であるということです。マウント・セクションのステートメントは、そのルートが呼び出されなくても実行されるようになりました。

私が欲しいものを達成する正しい方法は何でしょうか?

$app->mount ('{_locale}/', function ($client) use ($app) { 
    $client->match ('/{id}/edit', 'clients.controller:editAction') 
     ->assert ('id', '\d+') 
     ->method ('GET|POST'); 
    $client->match ('/add', 'clients.controller:addAction') 
     ->method ('GET|POST'); 
    $client->match ('/{id}', 'clients.controller:deleteAction') 
     ->assert ('id', '\d+') 
     ->method ('DELETE'); 
    $client->get('/', "clients.controller:indexAction"); 
}); 

ロケールが自動的に設定されているこの方法を次のように、この場合のルートの

おかげ

答えて

0

正しい方法です。

関連する問題