定制 jersyfi/laravel-localization 二次开发

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

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

jersyfi/laravel-localization

Composer 安装命令:

composer require jersyfi/laravel-localization

包简介

A package to localize a laravel application

README 文档

README

Packagist Downloads Packagist Version GitHub License

Installation

You can install the package via composer

composer require jersyfi/laravel-localization

You need to publish the config file to customize the package

php artisan vendor:publish --provider="Jersyfi\Localization\LocalizationServiceProvider" --tag="config"

You have the option to publish a migration file for adding a customizable column to a users table for storing the current locale for a logged in user. This is usefull if your application wants to send emails to every user.

php artisan vendor:publish --provider="Jersyfi\Localization\LocalizationServiceProvider" --tag="migrations"

The published config localization looks like so

return [

    /**
     * Applications default locale need to be set because the config('app.locale')
     * gives back the current locale and not the value from config
     */
    'default_locale' => 'de',

    /**
     * Application locales determines all locals that exists in the application
     * You can ignore or delete the locales from app.locales if you set some
     */
    'locales' => [
        'en',
        'de'
    ],
    
    /**
     * Redirect to default locale when not found
     */
    'redirect_default' => false,

    /**
     * Detect user locale via http header
     * When no locale is stored in session user gets redirected
     */
    'detect_locale' => false,
    
    /**
     * Application can store the prefered_locale in the users database table
     */
    'store_users_database' => true,
    
    /**
     * Setup for the users database table
     * Only if 'store_users_database' is set to true
     */
    'database' => [
        'users_table_name' => 'users',
        'prefered_locale_column_name' => 'prefered_locale',
    ],

];

How to use

More information can be found in the original Laravel documentation with version 8.x. There you need to know everything about Routing and Localization. When you also want to have translatable models i prefere to use laravel-translatable from Spatie.

Routing

The middleware is using redirect_default to redirect any request when the requested locale was not in locales. To detect the browser language when entering the page for the first time the LocaleController is using detect_locale.

You can redirect to the default_locale by accessing the LocaleController function called localize with the example:

use Jersyfi\Localization\Http\Controllers\LocaleController;

Route::get('/', [LocaleController::class, 'localize'])
    ->name('locale');

To group a route it is the easiest way to set a prefix named {locale} together with the middleware locale. Inside this group you can set your own localized routes. An example to get this localized route group:

Route::prefix('{locale}')
    ->middleware('locale')
    ->group(function () {

        // your localized routes here
    });

Helpers

The helpers can be accesed directly by aliases or by facades. When using aliases in your controller you need to include use Localization. In below examples the used values are from the above config file so that you can compare it.

Return the given locale or the current app locale with replaced separator.

$slug = Localization::getLocaleSlug('en_GB'); // en-gb
$slug = Localization::getLocaleSlug(); // de

Return all available locales set in the localization config.

$locales = Localization::getLocales(); // ['en', 'de']

Return application default locale set in the localization config.

$locale = Localization::getDefaultLocale(); // de

Return all available locales from localization config without the default locale.

$locales = Localization::getLocalesWithoutDefault(); // ['en']

Return all available locales from localization config without the current locale.

$locales = Localization::getLocalesWithoutCurrent(); // ['de']

Return the current Route URL

$url = Localization::currentRouteURL(); // https://test.de/de/home?query=true

Return the current Route URL with any different locale set in the localization config. The function replaces the {locale} set in routes with the given locale.

$url = Localization::currentRouteLocaleURL('en'); // https://test.de/en/home?query=true

Return the current Route URL with default locale set in the localization config.

$url = Localization::currentRouteDefaultLocaleURL(); // https://test.de/de/home?query=true

Check if the locales are valid

$valid = Localization::localeIsValid('de'); // true
$valid = Localization::localeIsValid('de', 'en'); // true
$valid = Localization::localeIsValid('de', 'sp'); // false

Examples

Route to named routes

We create a simple index Route named home calling whatever you want. In this example we call a Controller. Then you can call your route from whereever you want with route('home').

Route::get('/', [LocaleController::class, 'localize'])
    ->name('locale');

Route::prefix('{locale}')
    ->middleware('locale')
    ->group(function () {

        // your localized routes here
        Route::get('/', [HomeController::class, 'index'])
            ->name('home');
    });
<a href="{{ route('home') }}">Home</a>

Create a canonical link

You need to call the helper function Localization::currentRouteDefaultLocaleURL()

<link rel="canonical" href="{{ Localization::currentRouteDefaultLocaleURL() }}">

Create alternate links

To get all alternate links without the default locale you can call the helper function Localization::getLocalesWithoutDefault() inside a foreach loop. Inside the href of the html you can call the helper function Localization::currentRouteLocaleURL() and pass the $locale to it.

@foreach(Localization::getLocalesWithoutDefault() as $locale)
    <link rel="alternate" hreflang="{{ Localization::getLocaleSlug($locale) }}" href="{{ Localization::currentRouteLocaleURL($locale) }}">
@endforeach

Create a language selector

When you want to create a language selector first you need the current locale slug. For this you can call the helper function Localization::getLocaleSlug(). To loop the other locales you can decide if you want to display all available locales with Localization::getLocales() or if you want to display the available locales without the current locale with Localization::getLocalesWithoutCurrent(). Inside the foreach loop you can call the helper function Localization::currentRouteLocaleURL($locale) to get the link and Localization::getLocalSlug($locale) for the locale slug.

<div>{{ Localization::getLocaleSlug() }}</div>
<ul>
    @foreach(Localization::getLocalesWithoutCurrent() as $locale)
        <li>
            <a href="{{ Localization::currentRouteLocaleURL($locale) }}">{{ Localization::getLocalSlug($locale) }}</a>
        </li>
    @endforeach
</ul>

Changelog

Please see CHANGELOG for more information what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

jersyfi/laravel-localization 适用场景与选型建议

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

它主要适用于以下技术方向: 「locale」 「laravel」 「localization」 「multilangual」 「laravel-localization」 「jersyfi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-16