30

我正在使用 Laravel 本地化来提供两种不同的语言。我已经设置了所有路径,mydomain.com/en/bla 提供英语并存储“en”会话变量,mydomain.com/he/bla 提供希伯来语并存储“he”会话变量。但是,我想不出一种体面的方式来提供语言切换链接。这将如何工作?

4

8 回答 8

15

我通过将其添加到 routes.php 中的 before 过滤器解决了我的问题:

// Default language ($lang) & current uri language ($lang_uri)
$lang = 'he';
$lang_uri = URI::segment(1);

// Set default session language if none is set
if(!Session::has('language'))
{
    Session::put('language', $lang);
}

// Route language path if needed
if($lang_uri !== 'en' && $lang_uri !== 'he')
{
    return Redirect::to($lang.'/'.($lang_uri ? URI::current() : ''));
}
// Set session language to uri
elseif($lang_uri !== Session::get('language'))
{
    Session::put('language', $lang_uri);
}

// Store the language switch links to the session
$he2en = preg_replace('/he\//', 'en/', URI::full(), 1);
$en2he = preg_replace('/en\//', 'he/', URI::full(), 1);
Session::put('he2en', $he2en);
Session::put('en2he', $en2he);
于 2012-10-10T19:40:22.737 回答
12

这是我最初在 laravel 论坛上发布的帖子,但也许它会对其他人有所帮助,所以我也将其发布在这里。

我在为我的应用程序构建一个简单的语言切换器时遇到了一些麻烦,而且论坛上的信息有点旧(一些帖子),所以我制作了这段简单的代码,它使得在你的应用程序上更改语言变得超级容易飞。

我的观点中有以下语言字符串:

{{ __('languagefile.the_language_string'); }}

我得到了带有 URL 的语言,我认为这是最好的方式,它也有利于 seo 和人们共享的链接。例子:

www.myapp.com/fi/support (Finnish)
www.myapp.com/en/support (English)
www.myapp.com/sv/support (Swedish)

好的,所以问题是我想要一种简单的方法来即时更改语言,而不必弄乱会话和 cookie。下面是我是如何做到的:

在您的库文件夹中创建一个名为chooselang.php的库

在里面插入这段代码:

class Chooselang extends HTML {
    /**
     * Generate a Language changer link.
     *
     * <code>
     *      // Generate a link to the current location,
     *      // but still change the site langauge on the fly
     *      // Change $langcode to desired language, also change the Config::set('application.language', 'YOUR-LANG-HERE')); to desired language
     *      // Example
     *      echo Chooselang::langslug(URI::current() , $langcode = 'Finnish' . Config::set('application.language', 'fi'));
     * </code>
     *
     * @param  string  $url
     * @param  string  $langcode
     * @param  array   $attributes
     * @param  bool    $https
     * @return string
     */

    public static function langslug($url, $langcode = null, $attributes = array(), $https = null)
    {
        $url = URL::to($url, $https);

        if (is_null($langcode)) $langcode = $url;

        return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($langcode).'</a>';
    }

}

在此之后,您就可以生成您的 url 切换器 URL:s。只需像添加任何其他 Blade 链接一样添加它们。

示例如何生成芬兰语、瑞典语和英语的链接(使用 Blade)

  {{ Chooselang::langslug(URI::current() , $langcode = 'Fin' . Config::set('application.language', 'fi')); }}
  {{ Chooselang::langslug(URI::current() , $langcode = 'Swe' . Config::set('application.language', 'sv')); }}
  {{ Chooselang::langslug(URI::current() , $langcode = 'Eng' . Config::set('application.language', 'en')); }}

以上将生成始终在当前页面上的 URL:s,并将 lang slug 更改为您想要的。这样,语言就会更改为您想要的语言,并且用户自然会停留在同一页面上。默认语言 slug 永远不会添加到 url。

生成的 url 看起来像:

<a href="http://localhost/laravel/public/support">Fin</a>
<a href="http://localhost/laravel/public/sv/support">Swe</a>
<a href="http://localhost/laravel/public/en/support">Eng</a>

PS。如果您将它们添加到主模板文件中,这些链接将特别有用。

于 2012-11-20T08:05:36.310 回答
5

您可以有一个 Route to hand 语言更改,例如:

Route::get('translate/(:any)', 'translator@set');

然后在控制器中的set动作中translator可以改变会话,这取决于通过 URL 传递的语言代码。

您还可以使用更改配置设置

Config::set('application.language', $url_variable');

控制器示例 - translate.php

public function action_set($url_variable)
{
     /* Your code Here */
}
于 2012-10-03T14:27:32.587 回答
3

Just in case for future users if you want to use package for localization There is a great package at https://github.com/mcamara/laravel-localization. which is easy to install and has many helpers.

于 2014-03-17T15:42:06.893 回答
2

这个问题仍然出现在 Google 搜索中,所以如果您使用的是 Laravel 4 或 5,以及 mcamara/laravellocalization,这就是答案。

<ul>
    <li class="h5"><strong><span class="ee-text-dark">{{ trans('common.chooselanguage') }}:</span></strong> </li>
        @foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
            <li>
               <a rel="alternate" hreflang="{{$localeCode}}" href="{{LaravelLocalization::getLocalizedURL($localeCode) }}">
                   <img src="/img/flags/{{$localeCode}}.gif" /> {{{ $properties['native'] }}}
               </a>
           </li>
        @endforeach
</ul>

请注意,此示例显示标志(在 public/img/flags/{{locale}}.gif 中),要使用它,您需要一些 .css,但您可以根据需要修改它以显示文本。 .

供参考。mcamara/laravellocalization 文档有示例和很多帮助程序,因此请查看 github 上的文档。( https://github.com/mcamara/laravel-localization )

于 2015-09-03T08:21:36.653 回答
1

尝试使用 Session 的。像这样的东西:

控制器:

 class Language_Controller extends Base_Controller {

        function __construct(){
            $this->action_set();
            parent::__construct();
        }

       private function checkLang($lang = null){
         if(isset($lang)){
           foreach($this->_Langs as $k => $v){
             if(strcmp($lang, $k) == 0) $Check = true;
           }
       }
        return isset($Check) ? $Check : false;
       }

       public function action_set($lang = null){
        if(isset($lang) && $this->checkLang($lang)){
            Session::put('lang', $lang);
            $this->_Langs['current'] = $lang;
            Config::set('application.language', $lang);
        } else {
            if(Session::has('lang')){
                Config::set('application.language', Session::get('lang'));
                $this->_Langs['current'] = Session::get('lang');
            } else {
                $this->_Langs['current'] = $this->_Default;
            }
        }
        return Redirect::to('/');
    }
}

在 Route.php 中:

Route::get('lang/(:any)', 'language@set');
于 2012-10-10T18:56:16.890 回答
1

我一直在这样做:

$languages = Config::get('lang.languages'); //returns array('hrv', 'eng')

$locale = Request::segment(1); //fetches first URI segment

//for default language ('hrv') set $locale prefix to "", otherwise set it to lang prefix
if (in_array($locale, $languages) && $locale != 'hrv') {
    App::setLocale($locale);
} else {
    App::setLocale('hrv');
    $locale = null;
}

// "/" routes will be default language routes, and "/$prefix" routes will be routes for all other languages
Route::group(array('prefix' => $locale), function() {

    //my routes here

});

来源: http: //forumsarchive.laravel.io/viewtopic.php?pid=35185#p35185

于 2014-05-01T10:38:01.740 回答
0

我正在做的包括两个步骤:我正在创建一个包含以下字段的语言表:

编号 | 姓名 | 蛞蝓

例如,其中包含我需要的语言数据

1 | 希腊语 | 克

2 | 英语 | zh

3 | 荷兰语 | 德

我在下面的代码中使用的语言模型是指该表。

所以,在我的 routes.php 我有类似的东西:

//get the first segment of the url
$slug = Request::segment(1);   
$requested_slug = "";

//I retrieve the recordset from the languages table that has as a slug the first url segment of request
$lang = Language::where('slug', '=', $slug)->first();

//if it's null, the language I will retrieve a new recordset with my default language
$lang ? $requested_slug = $slug :  $lang = Language::where('slug', '=', **mydefaultlanguage**')->first();

//I'm preparing the $routePrefix variable, which will help me with my forms
$requested_slug == ""? $routePrefix = "" : $routePrefix = $requested_slug.".";

//and I'm putting the data in the in the session
Session::put('lang_id', $lang->id);
Session::put('slug', $requested_slug);
Session::put('routePrefix', $routePrefix );
Session::put('lang', $lang->name);

然后我可以使用请求的 slug 作为前缀来编写路由......

Route::group(array('prefix' =>  $requested_slug), function()
{
    Route::get('/', function () {
        return "the language here is gonna be: ".Session::get('lang');
    });

    Route::resource('posts', 'PostsController');
    Route::resource('albums', 'AlbumsController');
});

这可行,但是每次我的应用程序中的路线发生变化时,此代码都会向数据库询问语言。我不知道我怎么能,如果我应该,找出一种机制来检测路由是否更改为另一种语言。

希望有帮助。

于 2013-09-24T02:11:28.133 回答