在Laravel 5.2中管理区域设置,本地未设置

时间:2016-04-11 23:01:41

标签: php laravel localization

我正在尝试在我的网站上使用2个语言。

所以,我翻译了我的内容,并使用了这个lib

语言切换器代码:

            <li class="dropdown language-switch">
            <a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                @if (LaravelLocalization::getCurrentLocale() =='en')
                    <img src="/images/flags/GB.png" class="position-left" alt="">
                @elseif (LaravelLocalization::getCurrentLocale() =='es')
                    <img src="/images/flags/MX.png" class="position-left" alt="">
                @else
                    <img src="/images/flags/{{LaravelLocalization::getCurrentLocale()}}.png" class="position-left" alt="">
                @endif


                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                <li><a class="mexico" href="{{  LaravelLocalization::getLocalizedURL(LaravelLocalization::setLocale('es'), Request::url()) }}"><img src="/images/flags/MX.png" alt=""> Español</a></li>
                <li><a class="english" href="{{  LaravelLocalization::getLocalizedURL(LaravelLocalization::setLocale('en'), Request::url()) }}"><img src="/images/flags/GB.png" alt=""> English</a></li>
            </ul>
        </li>

当我尝试检索语言环境时,如果我这样做:

Lang::getLocale()

App::getLocale() // official docs

语言环境将坚持“en”

但如果我这样做:

Session::get('locale')

没关系。

大部分内容都翻译得很好,但有些部分是英文的,无论如何。

我仔细检查了翻译文件,但这些元素可以使用西班牙语翻译。

所以,我对语言环境有点困惑......

知道发生了什么???

2 个答案:

答案 0 :(得分:1)

It looks like you are manually overwriting the language from your view. This also seems to be the reason that en is the language it always ends up with as it's the last one in the list.

Change your code to this:

<li><a class="mexico" href="{{  LaravelLocalization::getLocalizedURL('es', Request::url()) }}"><img src="/images/flags/MX.png" alt=""> Español</a></li>
<li><a class="english" href="{{  LaravelLocalization::getLocalizedURL('en', Request::url()) }}"><img src="/images/flags/GB.png" alt=""> English</a></li>

答案 1 :(得分:0)

Firstly: check your config/app.php file. It should have your locale (probably es) and your fallback (probably en)

Secondly: Verify if your locale identifier matches with it's requisite.

Thirdly: dump the php info by just running phpinfo(); and look for HTTP_ACCEPT_LANGUAGE. It should be as you expect.

I Hope It's helpful!

EDIT:

I noticed you wrote tons of code to actually change just the file name. Maybe it'd be cleaner if you placed it in a lang file and just call the info using the trans() helper or @lang() blade directive.

it'd be something like this:

<img src="/images/flags/@lang('navigation.lang.image').png" class="position-left">

or

<img src="/images/flags/{{ trans('navigation.lang.image') }}.png" class="position-left">