如何在 Laravel 5.7 中切换语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53666019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to switch language in laravel 5.7
提问by Nipuna Samarawickrama
I'm new to the this forum and this is my first question. I am developing an laravel application.so that application default language is English
. I want to change this in to Chinese
.
我是这个论坛的新手,这是我的第一个问题。我正在开发一个 Laravel 应用程序English
。所以应用程序的默认语言是. 我想把它改成Chinese
.
My requirement is main language is Chinese
and other language is English
. My language chooser is working fine, but when i change app "local" to Chinese but it's change. I tried clear cache and recheck it same thing.not change language. I have already created two language array "en" for English "ch" for Chinese.
我的要求是主要语言是 Chinese
,其他语言是English
。我的语言选择器工作正常,但是当我将应用程序“本地”更改为中文时,它发生了变化。我尝试清除缓存并重新检查相同的事情。不要更改语言。我已经为英文“ch”创建了两个语言数组“en”用于中文。
config/app.php
配置/app.php
'locale' => 'en',
'fallback_locale' => 'ch',
LanguageMiddlewareapp/Http/Middleware
语言中间件app/Http/Middleware
public function handle($request, Closure $next)
{
if(Session::has('locale')){
app()->setLocale(Session::get('locale'));
}
return $next($request);
}
web.phproutes/web.php
网页.phproutes/web.php
Route::group(['middleware' => 'language'],function(){
Route::get('/', function () {
return view('welcome');
});
});
Thank you.
谢谢你。
回答by Jignesh Joisar
try this one
试试这个
You can change the active language at runtime using the setLocale
method on the App facade
您可以使用setLocale
App 外观上的方法在运行时更改活动语言
App::setLocale($locale); //by facade.
app()->setLocate($locate); ///by helper
You may use the getLocale
and isLocale
methods to determine the current locale or check if the locale is a given value
您可以使用getLocale
和isLocale
方法来确定当前语言环境或检查语言环境是否为给定值
$locale = App::getLocale(); //get current language
if (App::isLocale('en')) { //if current language is english then true else false.
//
}
You can als configure a "fallback language", which will be used when the active language does not contain a given translation string
您还可以配置“后备语言”,当活动语言不包含给定的翻译字符串时将使用该语言
'fallback_locale' => 'en',
for more information read this articleand seelaravel official document
回答by Ismoil Shifoev
Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override them by placing files in the resources/lang/vendor/{package}/{locale}
directory.
某些软件包可能会附带它们自己的语言文件。您可以通过在resources/lang/vendor/{package}/{locale}
目录中放置文件来覆盖它们,而不是更改包的核心文件来调整这些行 。
https://laravel.com/docs/5.7/localization
https://laravel.com/docs/5.7/localization
I think this package help you to switch language
我认为这个包可以帮助您切换语言
回答by Momit Hasan
I was facing the same problem, the locale was changing in session but not in config. So have checked the session's locale in every blade and controller and set the default the language instant from there, here is the code on my blade file
我遇到了同样的问题,语言环境在会话中发生了变化,但在配置中没有发生变化。所以已经检查了每个刀片和控制器中的会话区域设置,并从那里设置默认语言即时,这是我的刀片文件中的代码
if(\Session::get('locale') == 'en') \App::setLocale('en');
else \App::setLocale('bn');
Hope it will help you
希望它会帮助你