Laravel 5多语言站点,URL中带有语言前缀

编程入门 行业动态 更新时间:2024-10-09 20:23:23
本文介绍了Laravel 5多语言站点,URL中带有语言前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我在网站上需要多种语言,并具有以下网址:

So I need multiple languages on a site with urls like this:

mysite/en/language mysite/it/language mysite/es/language

我决定在路线中使用前缀,如下所示:

I decided to go with the prefix in my routes like so:

$allLanguages = ["en", "it", "es"]; $lng = ( in_array( Request::segment(1), $allLanguages) ) ? Request::segment(1) : ""; Route::group(['prefix' => $lng ], function () { Route::get('language', function () { dd("The language is: " . Request::segment(1)); }); });

它可以工作,但是如果我想设置默认语言并像 mysite/language 这样访问url-则行不通.

It works, but if I wanted to set up a default language and access url like so mysite/language - it wouldn't work.

您有什么想法?这是处理多语言网站的最佳方法吗?

What are your thoughts? Is this the best way how to handle multi language site? How to address the problem when accessing site without prefix ( show page with default language )?

推荐答案

经过研究,我发现了这个漂亮的本地化类 mcamara/laravel-localization .安装后,我的代码现在看起来像这样,并且一切正常-魔术!!

After some research I found this beautiful localization class mcamara/laravel-localization. After installation my code now looks like this and it's all working - magic!!

Route::group(['prefix' => LaravelLocalization::setLocale() ], function () { Route::get('language', function () { dd("The language is: " . LaravelLocalization::getCurrentLocale()); }); });

更多推荐

Laravel 5多语言站点,URL中带有语言前缀

本文发布于:2023-11-02 07:13:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前缀   多语言   语言   站点   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!