在Laravel的提供程序类中获取语言环境语言 [英] Getting locale language at provider class in Laravel

查看:270
本文介绍了在Laravel的提供程序类中获取语言环境语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,正在尝试使标头中的内容变量与所有视图共享,但是问题是要在提供程序(AppServiceProvider)类中获取以空值支持我的语言.

这是我的代码:

public function boot( )
{
    // $language=App::setLocale($locale);
    $locale = App::getLocale();
    \Session::put('language', 'en');
    \Config::get('app.locale');
    \Config::get('languages') ;
    \Session::get('languages', 'en');
    $lang = Session::get ('locale');   

    $products = ProductsTranslation::join('products', 'products.id', '=', 'products_translations.product_id')->where('language',$lang) ->get();                          

    $postId   = Post::get();
    view()->share('products', $products,'language',' \Session::get("language", $locale )','postId',$postId);    
}

解决方案

此代码段存在一些问题:

  • share()方法仅采用两个参数,而不是重复的键,值配对
  • 用于language的值是Session::get("language", $locale)的结果,但实际放置的是字符串'\ Session :: get("language",$ locale)'.

基于此,您需要按照以下内容进行重写

view()->share('products', $products);
view()->share('language', Session::get('language', $locale));
view()->share('postId', $postId);

I'm new at Laravel and trying to make the variable of content at the header shared with all views, but the issue is with getting the language which backing with me with null value at the provider (AppServiceProvider) class.

Here's my code :

public function boot( )
{
    // $language=App::setLocale($locale);
    $locale = App::getLocale();
    \Session::put('language', 'en');
    \Config::get('app.locale');
    \Config::get('languages') ;
    \Session::get('languages', 'en');
    $lang = Session::get ('locale');   

    $products = ProductsTranslation::join('products', 'products.id', '=', 'products_translations.product_id')->where('language',$lang) ->get();                          

    $postId   = Post::get();
    view()->share('products', $products,'language',' \Session::get("language", $locale )','postId',$postId);    
}

解决方案

There are a few issues with the snippet:

  • The share() method only takes two arguments instead of repeated key, value pairings
  • The value intended for language is the result of Session::get("language", $locale), but what's actually put is the string ' \Session::get("language", $locale )'.

Based on that, you'd need to rewrite as following

view()->share('products', $products);
view()->share('language', Session::get('language', $locale));
view()->share('postId', $postId);

这篇关于在Laravel的提供程序类中获取语言环境语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆