在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:39:42  来源:igfitidea点击:

Change language in Laravel 5

laravelmultilinguallaravel-blade

提问by user199320

I just begin to use Laravel 5.4, In the login.blade.php i have

我刚开始使用 Laravel 5.4,在 login.blade.php 我有

enter image description here

在此处输入图片说明

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/langfolder 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:

如果你想实现语言选择的可能性,你只需要几个简单的步骤来申请:

  1. In config/app.phpadd this code:

    'locale' => 'ru',
    'locales' => ['ru', 'en'],
    

    The name of the locale can be any.

  2. In app/Http/Middlewarecreate a new file named Locale.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);
       }
     }
    
  3. In app/Http/Kernel.phpin $middlewareGroups=[ ... ]add the following line:

    \App\Http\Middleware\Locale::class,

  4. In routes/web.phpadd:

    Route::get('setlocale/{locale}', function ($locale) {
      if (in_array($locale, \Config::get('app.locales'))) {
        session(['locale' => $locale]);
      }
      return redirect()->back();
    });
    
  1. config/app.php添加此代码:

    'locale' => 'ru',
    'locales' => ['ru', 'en'],
    

    语言环境的名称可以是任何名称。

  2. 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);
       }
     }
    
  3. app/Http/Kernel.php$middlewareGroups=[ ... ]添加以下行:

    \App\Http\Middleware\Locale::class,

  4. 另外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.phpand 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/endirectory and using @langblade directive you put your file name (without extension) and desired value separated with dot.

您的/resources/lang/en目录中可以有尽可能多的文件,并使用@lang刀片指令将文件名(不带扩展名)和所需的值用点分隔。