如何使用Laravel 9创建多语言网站

发布于 2025-01-29 11:00:17 字数 190 浏览 0 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2025-02-05 11:00:17

我最终使用了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.

尽揽少女心 2025-02-05 11:00:17

我很好奇,为什么您不能使用会话(这是一个真正的问题,我真的很想理解)?

您可以使用相同的方法使用会话:创建一个基于查询字符串的中间件来设置app :: setLocale(“ yourlanguage”)。

public function handle(Request $request, Closure $next)
{
    // This example try to get "language" variable, \
    // if it not exist will define pt as default \
    // you also can use some config value: replace 'pt' by Config::get('app.locale')

    App::setLocale(request('language', 'pt'));

    return $next($request);
}

或者,您可以按路线进行:

// This example check the if the language exist in an array before apply
Route::get('/{language?}', function ($language = null) {
    if (isset($language) && in_array($language, config('app.available_locales'))) {
        app()->setLocale($language);
    } else {
      app()->setLocale(Config::get('app.locale'));
    }
    
    return view('welcome');
});

源:

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.

public function handle(Request $request, Closure $next)
{
    // This example try to get "language" variable, \
    // if it not exist will define pt as default \
    // you also can use some config value: replace 'pt' by Config::get('app.locale')

    App::setLocale(request('language', 'pt'));

    return $next($request);
}

Or you can do it by Route:

// This example check the if the language exist in an array before apply
Route::get('/{language?}', function ($language = null) {
    if (isset($language) && in_array($language, config('app.available_locales'))) {
        app()->setLocale($language);
    } else {
      app()->setLocale(Config::get('app.locale'));
    }
    
    return view('welcome');
});

Source:

Laravel change language depending on a $_GET parameter

https://lokalise.com/blog/laravel-localization-step-by-step/

霓裳挽歌倾城醉 2025-02-05 11:00:17

您可以做这样的事情,使用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
enter image description here

And then in your middleware set the language based on the locale parameter.

We can pick dynamic url segment using request()->route('')

enter image description here

Note: You need to pass locale value when you call route() helper.

Maybe there is another better approach to it.

鹊巢 2025-02-05 11:00:17

您可以使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文