定制 dartmoon/laravel-localized-routes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dartmoon/laravel-localized-routes

Composer 安装命令:

composer require dartmoon/laravel-localized-routes

包简介

Localize laravel routes and views

README 文档

README

A simple package to localize the routes of Laravel. This pakage adds some macros to the Route facade to allow the localization. There are also some helpers.

Installation

composer require dartmoon/laravel-localized-routes

Usage

  1. Group all the routes you want to translate inside a Route::localize(...). This must be put at the topmost group level of your routes.
Route::localize(function () {
    // Put here all the routes you want to localize
});
  1. For each route that you want to localize change the method with the localized one. The localized version has a Localized suffix.

E.g. if this is your route file

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

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

Route::get('/external');

Then it must become

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

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

Route::get('/external');

// If you are using livewire or you need some routes 
// prefixed with the current locale
Route::localizeCurrentLocale(function () {
    Livewire::setUpdateRoute(function ($handle) {
        return Route::post('livewire/update', $handle);
    });
});
  1. You can now translate all your routes using the Laravel translation service. Inside the your lang folder (eg. /lang/it) create a routes.php file.
<?php

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

Customizing the available languages

First you need to publish the config.

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

Then you will find two new files (locales.php and localized-routes.php) files inside your config folder.

The file locales.php contains the list of the available locales. By default it is as follows:

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

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

You can now edit the available with the locale you want to enable.

The file localized-routes.php contains the configuration of the package. By default it is as follows:

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

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

You can now edit the prefix_default to true if you want to prefix the default locale to the routes.

Named routes

This package supports named routes out of the box and adds some useful prefixes.

E.g. Let's suppose these are your routes.

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

And this is your translation

<?php

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

Then you can simply do as follows:

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"!

Helpers

  • route_localized($name, $parameters = [], ?$locale = null, $absolute = true) it behaves exactly as the route helper of Laravel, but it allows you to specify the locale

  • url_localized($url, ?$locale = null) it allows you to localize an URL

  • current_localized(?$locale = null) it returns the current URL or route localized

  • available_locales() returns the available locales, without their names

  • is_default_locale($locale) returns true if the specified locale is the default one

  • is_current_locale_default returns true if the current locale is the default one

  • locale_name($locale, ?$default = null) returns the locale name for the specified locale

  • default_locale() returns the default locale

  • available_alternates() returns the available alternates for the available locales

Using a different LocaleProvider

By default the package uses the Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\DefaultLocaleProvider to get the current locale from the locales.php config file.

If you want to use a different provider to get the available locales from somewhere else, you can do so by creating a new class that implements the Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract interface and then binding it in the service container.

<?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;
    }
}

Then you can bind it in the service container in the AppServiceProvider class.

<?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
        );
    }
}

Using Laravel Octane

If you are using Laravel Octane you need to register the following listener to the config/octane.php file.

<?php

return [
    ...
    'listeners' => [
        ...
        RequestReceived::class => [
            ...
            \Dartmoon\LaravelLocalizedRoutes\App\Listeners\PrepareRoutesForNextRequest::class,
        ],
        ...    
    ],
    ...
];

You must cache the routes using the php artisan route:cache command otherwise it will not work as expected.

After this you can use the package as usual.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

dartmoon/laravel-localized-routes 适用场景与选型建议

dartmoon/laravel-localized-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.54k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dartmoon/laravel-localized-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dartmoon/laravel-localized-routes 我们能提供哪些服务?
定制开发 / 二次开发

基于 dartmoon/laravel-localized-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-16