How to find current language in Laravel view?

I am using the Laravel Lang class to localize my web application. I added two languages ​​to the languages ​​array in application/config/application.php . This changes the default language that it uses for localization, regardless of what the first part of the URI indicates (e.g. bla.com/en/bla and bla.com/co/bla). Now I need to be able to verify that the current language is the default in my opinion. However, the Lang class makes it impossible to verify this, as far as I managed to find out, since the Lang::$language variable is protected. Is there a way to test this separately from manual URI analysis?

+13
localization laravel
source share
10 answers

BenjaminRH's answer is very good, and his proposed approach works just fine. I slightly improved the fragment. Now it detects the browser language and checks whether this language is supported in accordance with the application configuration file.

This is a quick hack, but it works on my application. Please note that the application language is also installed. Do not use ore to improve it.

 Route::filter('before', function() { // current uri language ($lang_uri) $lang_uri = URI::segment(1); // Set default session language if none is set if(!Session::has('language')) { // use lang in uri, if provided if(in_array($lang_uri, Config::get('application.languages'))) { $lang = $lang_uri; } // detect browser language elseif(isset(Request::server('http_accept_language'))) { $headerlang = substr(Request::server('http_accept_language'), 0, 2); if(in_array($headerlang, Config::get('application.languages'))) { // browser lang is supported, use it $lang = $headerlang; } // use default application lang else { $lang = Config::get('application.language'); } } // no lang in uri nor in browser. use default else { // use default application lang $lang = Config::get('application.language'); } // set application language for that user Session::put('language', $lang); Config::set('application.language', $lang); } // session is available else { // set application to session lang Config::set('application.language', Session::get('language')); } // prefix is missing? add it if(!in_array($lang_uri, Config::get('application.languages'))) { return Redirect::to(URI::current()); } // a valid prefix is there, but not the correct lang? change app lang elseif(in_array($lang_uri, Config::get('application.languages')) AND $lang_uri != Config::get('application.language')) { Session::put('language', $lang_uri); Config::set('application.language', $lang_uri); } }); 
+9
source share

The easiest way to find out the current language of your site in Laravel:

 Lang::locale(); 

https://laravel.com/api/5.8/Illuminate/Translation/Translator.html#method_locale

It differs from this command, which returns the default language of your site:

 Config::get('app.locale'); 
+22
source share

In newer versions of Laravel, you can get the current language:

 Config::get('app.locale'); 
+6
source share

I figured out a solution to the language problem (thanks nickstr on IRC and the accepted answer to this question). It includes saving the current language as a session variable, which is updated when the uri language changes.

 Route::filter('before', function() { // Do stuff before every request to your application... // Default language ($lang) & current uri language ($lang_uri) $lang = 'he'; $lang_uri = URI::segment(1); // Set default session language if none is set if(!Session::has('language')) { Session::put('language', $lang); } // Route language path if needed if($lang_uri !== 'en' && $lang_uri !== 'he') { Return Redirect::to($lang.'/'.URI::current()); } // Set session language to uri elseif($lang_uri !== Session::get('language')) { Session::put('language', $lang_uri); } }); 
+4
source share

This can help. Config :: get ('application.language')

+3
source share

This will work fine

 lang="{{ app()->getLocale() }}" 
+3
source share

you can use

 https://github.com/mcamara/laravel-localization 

Laravel localization uses the URL provided for the request. To achieve this, a group must be added to the routes.php file. It will filter all pages that should be localized.

  // app/routes.php Route::group(array('prefix' => LaravelLocalization::setLanguage()), function() { /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/ Route::get('/', function() { return View::make('hello'); }); Route::get('test',function(){ return View::make('test'); }); }); /** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/ 

After adding this group to the route file, the user can access all languages ​​added to "languagesAllowed" ("en" and "es" by default, see the configuration section to change this parameter). For example, a user can now access two different languages ​​using the following addresses:

http://laravel.com/en

http://laravel.com/es

http://laravel.com

+2
source share

Alternatively, a slightly shorter way might use something like this:

 app()->getLocale() 

The advantage of this is that IDEs such as PHPStorm recognize this feature and can help you grow much faster.

+2
source share

I use App::getLocale() , which is probably the most supported since the App::setLocale('EN') method is used in the documentation .

This method can be used everywhere. If this is an error somewhere, you can use \App::... to make it work.

I am using Laravel 5.0.

+1
source share

The Lang class is designed specifically to output the correct language and, as you say, controls the language inside.

Looking through the APIs, there is no way to help you directly with this and parse the URIs so that the language looks appropriate.

You can always do this to get the language string in the URI:

 $language = URI::segment(1); 

Checking Laravel Queries

0
source share

All Articles