【问题标题】:Loading lang files in Laravel depending on prefix/session根据前缀/会话在 Laravel 中加载 lang 文件
【发布时间】:2014-06-20 02:52:38
【问题描述】:

我用 3 种语言(mk - 默认,sqen)构建了一个多语言网站,并最终在路由中使用以下代码.php:

$languages = array('en', 'sq');
$locale = Request::segment(1);
if(in_array($locale, $languages)){
    \App::setLocale($locale);
}else{
    $locale = null;
}
// Set the session here
Session::put('locale', $locale);

Route::group(array('prefix' => $locale), function()
{
    Route::get('/', array('as' => 'home', 'uses' => 'Controllers\Frontend\FrontendController@getIndex'));
    // .... other routes
});

我的默认语言环境是 'mk',我在 Land 文件夹中还有 2 种其他语言,sqen。 虽然路由工作正常,但问题是在加载 lang 文件时。它适用于 app.php 中设置的默认语言 mken 但不会切换到 sq 翻译,而是加载 en 语言文件。

例子:

  1. URL:http://website.com 加载 mk 语言文件

  2. 网址:http://website.com/en 加载 zh 语言文件

  3. URL:http://website./sq 加载 en 语言文件而不是 sq

在其他代码中,我在视图中有以下内容:

{{{ URL::route('home') }}}

控制器是常用的:

public function getIndex($locale = null)
{

    $data = array();
    return View::make('frontend.frontpage', $data);
}

我的问题:为什么当 URI 参数更改为 sq 时,没有加载 sq 语言文件?

【问题讨论】:

  • 为什么不使用这样的东西:github.com/mcamara/laravel-localization
  • 谢谢,按预期工作
  • 最好将该评论发布为答案并接受它作为问题的解决方案?因为这是一个合法的答案,从我的角度来看(有同样的问题,现在使用那个包)。
  • @Alomvar 我该怎么做?我只能对 Pat 的评论投赞成票
  • 请参阅stackoverflow.com/help/self-answer 了解更多信息。它应该像在其他地方回答问题一样简单,只需在问题页面的底部写一个答案。 (我会自己发布答案,但这会窃取@Pat 实际提供最佳解决方案的声誉)。

标签: laravel translation


【解决方案1】:

问题:“根据前缀/会话在 Laravel 中加载 lang 文件”

@Pat 的回答(在评论中)解决了我的问题 (Loading lang files in Laravel depending on prefix/session)

简而言之,使用https://github.com/mcamara/laravel-localization 可以轻松使用 Lavarel 创建多语言网站。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多