在 Laravel 5 中更改语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42395481/
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
Change language in Laravel 5
提问by user199320
I just begin to use Laravel 5.4, In the login.blade.php i have
我刚开始使用 Laravel 5.4,在 login.blade.php 我有
I don't like to put plain text in html code, is there a solution to make all the texts in seperate lang files to use them dynamically?
我不喜欢在 html 代码中放置纯文本,是否有解决方案可以让单独的 lang 文件中的所有文本动态使用它们?
Thank you
谢谢
回答by Alex Yapryntsev
The resources/lang
folder contains localization files. The file name corresponds to the view that it will be used. In order to get a value from this file, you can simply use the following code:
该resources/lang
文件夹包含本地化文件。文件名对应于将使用的视图。为了从这个文件中获取一个值,你可以简单地使用以下代码:
Lang::get('localization_file_name.variable_name');
Lang::get('localization_file_name.variable_name');
If you want to realize the possibility of language selection, you only need a few simple steps to apply:
如果你想实现语言选择的可能性,你只需要几个简单的步骤来申请:
In
config/app.php
add this code:'locale' => 'ru', 'locales' => ['ru', 'en'],
The name of the locale can be any.
In
app/Http/Middleware
create a new file namedLocale.php
. The contents of the file should be something like this:<?php namespace App\Http\Middleware; use Closure; use App; use Config; use Session; class Locale { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { //$raw_locale = Session::get('locale'); $raw_locale = $request->session()->get('locale'); if (in_array($raw_locale, Config::get('app.locales'))) { $locale = $raw_locale; } else $locale = Config::get('app.locale'); App::setLocale($locale); return $next($request); } }
In
app/Http/Kernel.php
in$middlewareGroups=[ ... ]
add the following line:\App\Http\Middleware\Locale::class,
In
routes/web.php
add:Route::get('setlocale/{locale}', function ($locale) { if (in_array($locale, \Config::get('app.locales'))) { session(['locale' => $locale]); } return redirect()->back(); });
在
config/app.php
添加此代码:'locale' => 'ru', 'locales' => ['ru', 'en'],
语言环境的名称可以是任何名称。
在
app/Http/Middleware
创建一个名为Locale.php
. 文件的内容应该是这样的:<?php namespace App\Http\Middleware; use Closure; use App; use Config; use Session; class Locale { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { //$raw_locale = Session::get('locale'); $raw_locale = $request->session()->get('locale'); if (in_array($raw_locale, Config::get('app.locales'))) { $locale = $raw_locale; } else $locale = Config::get('app.locale'); App::setLocale($locale); return $next($request); } }
在
app/Http/Kernel.php
中$middlewareGroups=[ ... ]
添加以下行:\App\Http\Middleware\Locale::class,
另外
routes/web.php
:Route::get('setlocale/{locale}', function ($locale) { if (in_array($locale, \Config::get('app.locales'))) { session(['locale' => $locale]); } return redirect()->back(); });
回答by vishal dobariya
Try this!
尝试这个!
{{ @lang('messages.login') }}
Now Add login key with it's value under language file as below
现在在语言文件下添加登录密钥及其值,如下所示
return['login'=>'Login']; // write inside messages file
return['login'=>'Login']; // write inside messages file
and Set your APP Config Local Variable Like 'en','nl','us'
并设置您的 APP 配置本地变量,例如 'en','nl','us'
App::setLocale(language name); like 'en','nl','us'
App::setLocale(language name); like 'en','nl','us'
回答by Mariusz
Laravel has a localizationmodule.
Laravel 有一个本地化模块。
Basically, you create a file, ex: resources/lang/en/login.php
and put
基本上,您创建一个文件,例如:resources/lang/en/login.php
并放入
return [
'header' => 'Login'
];
And in your template you use @lang('login.header')
instead of Login
.
在您的模板中,您使用@lang('login.header')
而不是Login
.
You can have as many files in your /resources/lang/en
directory and using @lang
blade directive you put your file name (without extension) and desired value separated with dot.
您的/resources/lang/en
目录中可以有尽可能多的文件,并使用@lang
刀片指令将文件名(不带扩展名)和所需的值用点分隔。