josiasmontag/laravel-localization

Laravel 框架的本地化解决方案

1.0.2 2024-03-04 08:45 UTC

This package is auto-updated.

Last update: 2024-09-04 09:51:05 UTC


README

Build Status Total Downloads Latest Stable Version License

简介

Laravel 本地化包是为 Laravel 5.5+ 构建的,提供以下功能:

  • 基于语言URL前缀的本地化路由。
  • 基于域名的本地化路由。
  • 中间件,根据HTTP头和cookie检测用户语言。
  • 将用户重定向到本地化版本。
  • 隐藏默认语言的语言URL前缀的选项。
  • 仅本地化部分路由的选项。
  • 语言切换器和Hreflang元标签
  • 修复了route()方法,尽可能使用本地化路由。
  • artisan route:cache兼容。

安装

要开始使用,请使用Composer将包添加到项目的依赖项中

composer require josiasmontag/laravel-localization

HandleLocalization中间件添加到App/Http/Kernel.php中的web

    protected $middlewareGroups = [
        'web' => [
        
            // Other Middleware
            
            \Lunaweb\Localization\Middleware\LocalizationHandler::class,
        ],
    ];

配置

要将配置文件发布到config/localization.php

php artisan vendor:publish --provider "Lunaweb\Localization\LocalizationServiceProvider"

默认配置

return [
    
    // Add any language you want to support
    'locales' => [
        'en' => ['name' => 'English'],
        'de' => ['name' => 'German'],
    ],

    // The default locale is configured in config/app.php (locale)

    // Default locale will not be shown in the url.
    // If enabled and 'en' is the default language:
    // / -> English page, /de -> German page
    // If disabled:
    // /en -> English Page, /de -> German page
    'hide_default_locale_in_url' => true,

    // Use query parameter if there are no localized routes available.
    // Set it to null to disable usage of query parameter.
    'locale_query_parameter' => 'hl',

    // Enable redirect if there is a localized route available and the user locale was detected (via HTTP header or session)
    'redirect_to_localized_route' =>  true,

    // Try to detect user locale via Accept-Language header.
    'detect_via_http_header' => true,

    // Remember the user locale using a cookie.
    'remember_via_cookie' => true,

    // Cookie expire time in minutes
    'cookie_expires' => 20160 // 14 days

];

用法

基于前缀的本地化路由

要添加带有语言前缀的本地化路由,编辑你的routes/web.php并使用localizedRoutesGroup辅助函数

Localization::localizedRoutesGroup(function() {
    Route::get('/', 'HomeController@uploadDocuments')->name('index');
    Route::get('/register', 'RegisterController@showRegisterForm')->name('register');
});

底层将为你创建以下路由

基于域名的本地化路由

要添加基于域名的本地化路由,将本地化域名添加到config/localization.php配置文件中

'locales' => [
   'en' => ['domain'=> 'domain.com', 'name' => 'English'],
   'de' => ['domain'=> 'domain.de', 'name' => 'German'],
   'fr' => ['domain'=> 'domain.fr', 'name' => 'French'],
],

上面的示例将创建以下路由

特定语言的本地化路由

你可以使用localization()宏手动创建特定语言的路线。

Route::get('/contact', 'ContactController@showContactForm')
    ->localization('en')
    ->name('contact');
    
Route::get('/kontakt', 'ContactController@showContactForm')
    ->localization('de')
    ->name('de.contact');

辅助函数

本地化route()

route()将自动使用可用的本地化版本。使用上面的示例,route('index')解析为indexde.indexfr.index路由,具体取决于用户的语言。

检查当前路由是否本地化

Localization::isLocalizedRoute()

或者...

Route::current()->getLocalization() === null

获取当前路由的本地化信息

Route::current()->getLocalization()

获取当前路由不同语言版本的URL

Localization::getLocaleUrl($localeCode)

获取当前路由不同语言版本的路由名称

Localization::getLocaleRoute($localeCode)

Hreflang元标签

@if(Localization::isLocalizedRoute())
   @foreach(Localization::getLocales() as $localeCode => $properties)
        <link rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode) }}">
   @endforeach
@endif

语言切换器

<ul>
    @foreach(Localization::getLocales() as $localeCode => $properties)
        <li>
            <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode, true) }}">
                 {{ $properties['native'] }} </a>
        </li>
    @endforeach
</ul>