【问题标题】:Localization and Ajax request Laravel 4本地化和 Ajax 请求 Laravel 4
【发布时间】:2014-07-25 03:21:13
【问题描述】:

我正在尝试翻译其中一些通过 Ajax 加载的视图,所以问题是,即使是语言环境,我也总是得到默认语言环境的文本,要清楚,这就是我的文件的外观:

app/config/app.php:

     //...
    'languages' => array('fr', 'en'),

    'locale' => 'fr',
     //...

routes.php

  if(!Request::ajax()) { 
// Set locale
$locale = Request::segment(1);

if(in_array($locale, Config::get('app.languages'))) {

    App::setLocale($locale);

    Session::put('locale', $locale);

} else {
    $locale = null;
    Session::put('locale', $locale);
}
$locale = Session::get('locale') or null;

}

// Group by locale
  Route::group(
array( 'prefix' => $locale ), function () {
    // Home controller
    Route::get('/' . Config::get('app.lcoale'), array( 'uses' =>     'HomeController@getIndex', 'as' => '/' ));

 //...

ajax 路由不在以 locale 为前缀的组中:

 Route::group(
     array( 'prefix' => 'ajax', 'namespace' => 'Ajax', 'before' => 'ajax' ), function () {
 //...

当我在发送 ajax 请求 Config::get('app.locale') 后检查当前语言环境时:它显示了正确的语言环境,但视图中呈现的文本是默认语言环境 (fr)。

这对你有意义吗?,请帮忙。

谢谢

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    为我解决。

     Route::filter('ajax', function(){
    
     if(! Request::ajax())
     {
    App::abort(404);
    }
    
        // Set again the locale
        if(Session::has('locale'))
        App::setLocale(Session::get('locale'));
     });
    

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2013-07-08
      • 2014-08-30
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2016-09-20
      相关资源
      最近更新 更多