如何在Laravel视图中找到当前语言? [英] How can I find the current language in a Laravel view?

查看:70
本文介绍了如何在Laravel视图中找到当前语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel Lang类对Web应用程序进行本地化.我在application/config/application.php的语言数组中添加了两种语言.这会将其用于本地化的默认语言更改为URI的第一部分指示的内容(例如bla.com/en/bla和bla.com/co/bla).现在,我需要能够查看当前视图中的默认语言.但是,由于Lang::$language变量受保护,据我所知,Lang类无法提供任何检查方法.除了手动解析URI之外,还有其他方法可以检查吗?

I'm using the Laravel Lang class for localization of my web app. I've added two languages to the languages array in application/config/application.php. This changes the default language it uses for localization to whatever 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 check what the current default language is in my view. However, the Lang class provides no way of checking this as far as I've been able to figure out, as the Lang::$language variable is protected. Is there any way of checking this apart from manually parsing the URI?

推荐答案

BenjaminRH的回答非常好,他建议的方法效果很好.我对代码段做了一些改进.现在,它会检测浏览器语言,并根据应用程序的配置文件检查是否支持该语言.

BenjaminRH's answer is very good, and his suggested approach works perfectly. I've improved the snippet a bit. Now it detects the browser language and checks if that language is supported according to the application's config file.

这是一个快速的技巧,但可以在我的应用程序上使用.请注意,现在还设置了应用程序语言.随时使用矿石进行改良.

It's a quick hack, but it works on my app. Note that the application language is also set now. Feel free to use ore 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);    
    }
});

这篇关于如何在Laravel视图中找到当前语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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