justijndepover/laravel-localized-routes

使用本地化路由设置您的Laravel应用程序

0.3.0 2021-09-02 12:34 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:25 UTC


README

Latest Version on Packagist Software License Total Downloads

使用本地化路由设置您的Laravel应用程序

注意

此应用程序仍在开发中,可能实现破坏性更改。请自行承担风险。

安装

您可以使用Composer安装此包

composer require justijndepover/laravel-localized-routes

安装后,您应发布您的配置文件

php artisan vendor:publish --tag="laravel-localized-routes-config"

配置

这是配置文件

return [

    /**
     * This global setting can enable / disable the entire localization package.
     */
    'enable_localized_routes' => true,

    /**
     * This list contains all the available locales.
     * Simply add your own locale and thats it!
     */
    'locales' => [
        'en' => 'English',
        'nl' => 'Nederlands',
    ],

    /**
     * Automatically detect locales
     *
     * With this setting enabled, you can automatically detect locales.
     * The middleware to do so will check the request for a "locale" header
     *
     * useful for api's, where you don't want the locale prefix,
     * but still want to set the application locale
     */
    'auto_detect_locales' => true,

    /**
     * Automatically redirect requests if the localized version exists
     *
     * With this setting enabled, your requests will automatically redirect
     * to their localized counterpart.
     *
     * For example: /home => /en/home
     */
    'auto_redirect_to_localized_route' => true,

];

用法

要使您的路由多语言化,请在 web.php 中添加以下内容

Route::localized(function () {
    // Every route in here is localized
});

就这样!您仍然可以为您的路由提供一个名称,就像您习惯的那样

Route::localized(function () {
    Route::get('home', HomeController::class)->name('home');
});
<!-- in your blade view -->
<a href="{{ route('home') }}">Home</a>
<!-- will return /{locale}/home -->

如果您想切换到不同的本地化路由,可以使用 switchLanguage 函数。

switchLanguage('fr'); // will return the current route, but the localized part is substituted with the new language

相关仓库

此包是以下版本的简化版:codezero-be/laravel-localized-routes

如果您需要一个更健壮的解决方案,具有更多选项,请查看他们的版本。

安全

如果您发现任何与安全相关的问题,请打开一个问题或直接通过 justijndepover@gmail.com 联系我。

贡献

如果您希望对包进行任何更改或改进,请随时提交一个pull请求。

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件