2

I am learning laravel 5.4 locale chapter. When I write code to test, I get a question. For example, I have two language directories in my resources/lang directory:

/resources
    /lang
        /en
            messages.php
        /zh-CN
            messages.php

I set default locale is zh-CN, fallback locale is en. When I set Accept-Language to zh-CN, I can get translation string in zh-CN. But when I set Accept-Language to en, I still get string in zh-CN. So laravel does not detect request locale automatically? And If my application want to show english to those Accept-Language is en, and show chinese to those Accept-Language is zh-CN, I need to do it manually, is that correct? I though laravel will detect request locale automatically.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
DengDeng
  • 513
  • 2
  • 9
  • 22
  • 2
    Possible duplicate of [How to detect language preference in Laravel 5](http://stackoverflow.com/questions/36986236/how-to-detect-language-preference-in-laravel-5) – Marcin Nabiałek Apr 01 '17 at 08:23

2 Answers2

1

First configure the available languages in your config/app.php.

    'available_locales' => array('en', 'de', 'fr', 'it'),

Now add this code to your routes/web.php. It will detect the browser language and set the locale if the language is available.

    $availableLanguages = Config::get('app.available_locales');
    $lang = Request::getPreferredLanguage($availableLanguages);
    if ($lang) Config::set('app.locale', $lang);
fan711
  • 716
  • 3
  • 13
0

After doing those changes it is always recommended to run these commands:

php artisan cache:clear
php artisan view:clear
php artisan route:clear
lewis4u
  • 14,256
  • 18
  • 107
  • 148