如何使用Laravel 9创建多语言网站
使用Laravel设置多语言网站的最佳方法是什么? URL必须包含语言(NL,FR,EN)。例如:mywebsite.com/en/faq。 我发现的大多数示例和教程都使用该会话来存储当前无用的当前语言。我应该能够直接用特定语言链接到页面。
我可以每种语言创建一条路线,但这似乎并不是一个好主意。理想情况下,这可以使其动态化,以便轻松地创建更多的地区。
What is the best way to setup a multi language website with Laravel?
The URL must contain the language (nl, fr, en). For example: mywebsite.com/en/faq.
Most examples and tutorials I find use the session to store the current language which is completely useless of course. I should be able to directly link to a page in a specific language.
I could create a route per language but that does not really seem like a good idea. Ideally this could be made dynamic in order to easily create more locales.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(4)
我最终使用了McAmara的Laravel-Localization包。
似乎可以做我需要的一切。
我不确定为什么有人会尝试构建自己的版本,如果存在。
I ended up using the laravel-localization package by mcamara.
Seems to do everything I need.
I'm not really sure why anyone would try to build their own version if this exists.
我很好奇,为什么您不能使用会话(这是一个真正的问题,我真的很想理解)?
您可以使用相同的方法使用会话:创建一个基于查询字符串的中间件来设置app :: setLocale(“ yourlanguage”)。
或者,您可以按路线进行:
源:
laravel更改” $ _get参数
https://lokalise.com/blog/ laravel-localization-by-Step/
I'm curious, why you cannot use session (it's a true question, I really would like to understand)?
You can use the same approach than with session: create a middleware to set App::setLocale("YourLanguage") based on query string.
Or you can do it by Route:
Source:
Laravel change language depending on a $_GET parameter
https://lokalise.com/blog/laravel-localization-step-by-step/
您可以做这样的事情,使用Locale Group和Locale Middleware
路线创建路线应该看起来像这样
,然后在中间件中基于
locale
参数设置语言。我们可以使用
request() - > route('')
注意:您需要在调用路由()hutper()助手时传递位置值。
也许还有另一种更好的方法。
You can do something like this, create route with locale group and locale middleware
Route should look like this
And then in your middleware set the language based on the
locale
parameter.We can pick dynamic url segment using
request()->route('')
Note: You need to pass locale value when you call route() helper.
Maybe there is another better approach to it.
您可以使用McAmara Laravel本地化包。它将完成您需要的一切。
这是包装链接:
https://github.com/mcamara/mcamara/laravel-laravel-laravel-localization
You can use the Mcamara Laravel localization package. it'll do everything you need.
And here's the package link:
https://github.com/mcamara/laravel-localization