keevitaja / linguist
为 Laravel 框架提供易于使用的本地化
Requires
- php: ^7.2
- laravel/helpers: ^1.1
README
此软件包为 Laravel 框架提供易于使用的多语言 URL 和重定向支持。
简而言之,Laravel 将为链接和重定向生成本地化 URL。
route('people')
http://site.com/people
http://site.com/fr/people
Linguist 与名为 Laravel routes for javascript 的 https://github.com/tightenco/ziggy 非常兼容!
安装
Linguist 非常易于使用。将本地化缩略词从 REQUEST_URI 中移除,为开发者提供最干净的多语言环境。
使用 Composer 安装
composer require keevitaja/linguist
有几种选项可以使 Linguist 工作。
选项 1: 修改 public/index.php
在您的项目 public/index.php
文件中,在供应商自动加载之后添加以下行。
(new Keevitaja\Linguist\UriFixer)->fixit();
最终结果将是这样的
/* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | our application. We just need to utilize it! We'll simply require it | into the script here so that we don't have to worry about manual | loading any of our classes later on. It feels great to relax. | */ require __DIR__.'/../vendor/autoload.php'; (new Keevitaja\Linguist\UriFixer)->fixit();
选项 2: 使用 LocalizedKernel
注意:此选项仅在您未更改应用程序的根命名空间时才有效。默认值为
App
。
在您的项目 bootstrap/app.php
中,将 App\Http\Kernel
与 Keevitaja\Linguist\LocalazedKernel
交换
/* |-------------------------------------------------------------------------- | Bind Important Interfaces |-------------------------------------------------------------------------- | | Next, we need to bind some important interfaces into the container so | we will be able to resolve them when needed. The kernels serve the | incoming requests to this application from both the web and CLI. | */ $app->singleton( Illuminate\Contracts\Http\Kernel::class, //App\Http\Kernel::class Keevitaja\Linguist\LocalizedKernel::class );
选项 3: 修改 App\Http\Kernel
注意:这也适用于自定义根命名空间。
<?php namespace App\Http; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Routing\Router; use Keevitaja\Linguist\UriFixer; class Kernel extends HttpKernel { public function __construct(Application $app, Router $router) { (new UriFixer)->fixit(); parent::__construct($app, $router); }
发布配置
最后,您需要发布 Linguist 配置以设置启用的语言区域和其他相关配置。
php artisan vendor:publish --provider="Keevitaja\Linguist\LinguistServiceProvider"
您的个人配置文件将是 config/linguist.php
。
使用
您可以将 LocalizeUrls 中间件作为您网络中间件组的第一个条目添加以获取 Linguist 支持
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \Keevitaja\Linguist\LocalizeUrls::class,
注意:此中间件必须是组中的第一个条目!
另一个选项是在您的应用程序服务提供程序中使用 Linguist
class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot(\Keevitaja\Linguist\Linguist $linguist) { $linguist->localize(); }
UrlGenerator
在需要时将在 URI 前添加语言区域缩略词。不需要额外的操作。
Route::get('people', ['as' => 'people.index', 'uses' => ''PeopleController@index'']);
{{ route('people.index') }} or {{ url('people') }}
http://site.com/people // default locale from linguist config
http://site.com/fr/people
http://site.com/ru/people
切换器是一个用于获取当前语言区域切换器 URL 的小助手。
$urls = dispatch_now(new \Keevitaja\Linguist\Switcher);
注意!配置和路由缓存都正常工作!
资产
使用 linguist 辅助器进行正确的资产路由
常规资产
<link rel="stylesheet" href="{{ linguist_asset('css/style.css') }}"> <script type="text/javascript" src="{{ linguist_asset('js/my_js.js') }}"></script>
安全资产
<link rel="stylesheet" href="{{ secure_linguist_asset('css/style.css') }}"> <script type="text/javascript" src="{{ secure_linguist_asset('js/my_js.js') }}"></script>
队列
要在队列中实现本地化,您需要在排队项中运行 Linguist->localize($theLocaleYouWant)
。
许可证
MIT