Laravel框架的多语言本地化支持

网友投稿 526 2022-10-24

Laravel框架的多语言本地化支持

Laravel框架的多语言本地化支持

Linguist - Localization for Laravel

This package provides an easy multilingual localization support for the Laravel framework.

Installation

Linguist is very easy to use. The locale slug is removed from the REQUEST_URI leaving the developer with the cleanest multilingual environment possible.

Install using Composer:

composer require keevitaja/linguist

Use LocalizedKernel instead the one that ships with Laravel in bootstrap/app.php:

$app->singleton( Illuminate\Contracts\Http\Kernel::class, Keevitaja\Linguist\LocalizedKernel::class);

Usage

Use LocalizeUrls middleware or Linguist->localize() in your ServiceProvider to get the localization support.

If you use the middleware make sure you set it in your web middleware stack as first item. Otherwise some redirections will not work.

UrlGenerator will add the locale slug in front of the URI when needed. No extra actions needed.

Route::get('people', ['as' => 'people.index', 'uses' => 'PeopleController@index']);

{{ route('people.index') }} or {{ url('people') }}

You will get localized urls like http://site.com/fr/about and http://site.com/about where the last one is using the default locale.

Switcher is a little helper to get the current URLs for the locale switcher.

$urls = dispatch_now(new \Keevitaja\Linguist\Switcher);

NB! Both config and route caching are working!

Queues

To make localization work in queues you need to run Linguist->localize($theLocaleYouWant) inside the queued item.

Configuration

You can publish the linguist.php configuration file where you can define which locales are enabled and the behaviour of the default locale.

Licence

MIT

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:多线程之生产者与消费者
下一篇:带缓存的输入流和输出流
相关文章

 发表评论

暂时没有评论,来抢沙发吧~