dartmoon/laravel-localized-routes

本地化 Laravel 路由和视图

v1.1.2 2024-09-30 14:44 UTC

This package is auto-updated.

Last update: 2024-09-30 14:46:11 UTC


README

这是一个用于本地化 Laravel 路由的简单包。此包向 Route 门面添加了一些宏,以允许本地化。还有一些辅助工具。

安装

composer require dartmoon/laravel-localized-routes

使用

  1. 将所有要翻译的路由分组在 Route::localize(...) 中。这必须放在您路由的最顶层分组中。
Route::localize(function () {
    // Put here all the routes you want to localize
});
  1. 对于每个要本地化的路由,更改方法为本地化版本。本地化版本有一个 Localized 后缀。

例如,如果这是您的路由文件

Route::group(function () {
    Route::get('/home', ...);
    Route::post('/update-profile', ...);

    Route::get('/do/not/localize');
});

Route::get('/external');

那么它必须变成

Route::localize(function () {
    Route::getLocalized('/home', ...);
    Route::postLocalized('/update-profile', ...);

    Route::get('/do/not/translate/but/prefix');
});

Route::get('/external');
  1. 现在您可以使用 Laravel 翻译服务翻译所有路由。在您的 lang 文件夹中(例如 /lang/it)创建一个 routes.php 文件。
<?php

return [
    '/home' => '/home-translated',
    '/update-profile' => '/aggiorna-profilo',
];

自定义可用语言

首先,您需要发布配置。

php artisan vendor:publish --provider="Dartmoon\LaravelLocalizedRoutes\LaravelLocalizedRoutesServiceProvider"

然后您将在您的 config 文件夹中找到两个新的文件(locales.phplocalized-routes.php)。

文件 locales.php 包含可用地区的列表。默认情况下,如下所示

<?php
/**
 * Return enabled locales
 */

return [
    'available' => [
        // 'locale' => 'Name of the locale'
        'en' => 'EN',
        'it' => 'IT',
    ],
    'default' => 'en', // Default locale to be used
];

现在您可以编辑 available 以启用所需的地区。

文件 localized-routes.php 包含包的配置。默认情况下,如下所示

<?php
/**
 * Return the configuration for the localized routes
 */

return [
    'prefix_default' => false, // If true the default locale will be prefixed to the routes
];

现在您可以编辑 prefix_defaulttrue,如果您想将默认地区前缀添加到路由中。

命名路由

此包开箱即支持命名路由,并添加了一些有用的前缀。

例如,假设这些是您的路由。

Route::localize(function () {
    Route::getLocalized('/home', ...)->name('home');
});

这是您的翻译

<?php

return [
    '/home' => '/home-translated',
];

然后您可以简单地这样做

route('home'); // If you have the "en" locale loaded then '/home', if you have the "it" locale loaded than it will be '/home-translated'
route('it.home'); // Will return '/home-translated'. This route will not be defined if the current locale is "it"!
route('en.home'); // Will return '/home'. This route will not be defined if the current locale is "en"!

辅助工具

  • route_localized($name, $parameters = [], $locale = null, $absolute = true) 它的行为与 Laravel 的 route 辅助函数完全相同,但它允许您指定地区

  • url_localized($url, $locale = null) 允许您本地化一个 URL

  • current_localized($locale = null) 返回当前 URL 或本地化路由

  • available_locales() 返回可用地区,不包含名称

  • is_default_locale($locale) 如果指定的地区是默认地区则返回 true

  • is_current_locale_default 如果当前地区是默认地区则返回 true

  • locale_name($locale, $default = null) 返回指定地区的地区名称

  • default_locale() 返回默认地区

  • available_alternates() 返回可用地区的可用替代方案

使用不同的 LocaleProvider

默认情况下,此包使用 Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\DefaultLocaleProvider 来从 locales.php 配置文件中获取当前地区。

如果您想使用不同的提供者从其他地方获取可用地区,您可以创建一个新的类,该类实现了 Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract 接口,然后在服务容器中绑定它。

<?php

namespace App\LocaleProviders;

use Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract;

class MyCustomLocaleProvider implements LocaleProviderContract
{
    public function getDefaultLocale(): string
    {
        return 'en';
    }

    public function getAvailableLocales(): array
    {
        return [
            'en' => 'EN',
            'it' => 'IT',
        ];
    }

    public function getLocaleName(string $locale, string $default = null): string
    {
        return $this->getAvailableLocales()[$locale] ?? $default;
    }
}

然后您可以在 AppServiceProvider 类中绑定它。

<?php

namespace App\Providers;

use App\LocaleProviders\MyCustomLocaleProvider;
use Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(
            LocaleProviderContract::class,
            MyCustomLocaleProvider::class
        );
    }
}

许可

此项目受 MIT 许可证的许可 - 请参阅 LICENSE.md 文件以获取详细信息