simexis / multi-language
laravel 5.x 的多语言支持
Requires
- php: >=5.5.9
- illuminate/support: >=5.0.0
README
注意:此包默认使用 ISO 639-1 双字母代码作为区域设置的语言: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
此包的灵感来源于: Laravel-Translatable 升级 Laravel 的本地化模块 Laravel 5 翻译管理器
- 这是一个用于可翻译模型的 Laravel 包。
- 读取所有翻译文件并将它们保存到数据库中。
- 本地前缀 URL 修改器。示例:login/ => /it/login . / => /it
安装
在您的 composer.json 中添加此包并运行 composer update(或直接运行 composer require simexis/multi-language
)
simexis/multi-language
更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组中
'providers' => [ Simexis\MultiLanguage\MultiLanguageServiceProvider::class, ]
您需要运行发布和迁移。
$ php artisan vendor:publish
$ php artisan migrate
路由已添加到 ServiceProvider 中。您可以在配置中设置路由的分组参数。您可以更改路由的前缀或过滤器/中间件。如果您想进行完全自定义,您可以扩展 ServiceProvider 并重写 map()
函数。
此示例将使翻译管理器在 http://yourdomain.com/multilanguage
可用
使用方法
设置当前区域。
App::setLocale('ru');
然后包将与 ru
区域一起工作。
使用翻译部分
Web 界面
当您通过按钮或命令导入了翻译后,您可以在 Web 界面中查看它们(在控制器定义的 URL 上)。您可以点击翻译并弹出编辑字段。只需点击保存,它就会被保存 :) 如果某个区域尚不存在翻译,您也可以直接编辑它以创建它。
您还可以使用 Web 界面上的按钮来追加、替换、截断和清除翻译。
您还可以使用以下命令。
导入命令
导入命令将在 resources/lang 中搜索并加载所有字符串到数据库中,以便您可以轻松管理它们。
$ php artisan translator:append
注意:默认情况下,只有新字符串被添加。数据库中已存在的翻译保持不变。如果您想用文件中的值替换所有值,请使用替换命令: php artisan translator:replace
删除数据库中的所有翻译。
$ php artisan translator:truncate
清除不存在的翻译。
$ php artisan translator:clear
使用 URL 本地前缀修改器
要将 http://yourdomain.com/page
转换为 http://yourdomain.com/en/page
,只需使用路由或 URL 函数即可。示例 url('page') 显示 http://yourdomain.com/en/page
,其中 en
是 App::getLocale()
使用可翻译模型
演示
获取翻译属性
$greece = Country::where('code', 'gr')->first(); echo $greece->translate('en')->name; // Greece App::setLocale('en'); echo $greece->name; // Greece App::setLocale('de'); echo $greece->name; // Griechenland
保存翻译属性
$greece = Country::where('code', 'gr')->first(); echo $greece->translate('en')->name; // Greece $greece->translate('en')->name = 'abc'; $greece->save(); $greece = Country::where('code', 'gr')->first(); echo $greece->translate('en')->name; // abc
填充多个翻译
$data = [ 'code' => 'gr', 'en' => ['name' => 'Greece'], 'fr' => ['name' => 'Grece'], ]; $greece = Country::create($data); echo $greece->translate('fr')->name; // Grece
步骤 1:迁移
在此示例中,我们想翻译模型 Country
。我们需要一个额外的表 country_translations
Schema::create('countries', function (Blueprint $table) { $table->increments('id'); $table->string('code'); $table->timestamps(); }); Schema::create('country_translations', function (Blueprint $table) { $table->increments('id'); $table->integer('country_id')->unsigned(); $table->string('name'); $table->char(config('multilanguage.locale_key'), 2)->index('idx_locale'); $table->unique(['country_id',config('multilanguage.locale_key')]); $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade'); $table->foreign(config('multilanguage.locale_key')) ->references(config('multilanguage.locale_key')) ->on('languages')->onDelete('cascade')->onUpdate('cascade'); });
步骤 2:模型
- 可翻译模型
Country
应该使用 特性Simexis\MultiLanguage\Traits\Translatable
。 - 翻译模型的约定是
CountryTranslation
。
// models/Country.php class Country extends Model { use \Simexis\MultiLanguage\Traits\Translatable; public $translatedAttributes = ['name']; protected $fillable = ['code', 'name']; /** * The database table used by the model. * * @var string */ protected $table = 'countries'; } // models/CountryTranslation.php use Illuminate\Database\Eloquent\Model; class CountryTranslation extends Model { public $timestamps = false; protected $fillable = ['name']; /** * The database table used by the model. * * @var string */ protected $table = 'country_translations'; }
数组 $translatedAttributes
包含在 "Translation" 模型中翻译的字段名称。
配置
翻译模型
定义翻译模型类时使用的约定是在关键字 Translation
后附加。
所以如果您的模型是 \MyApp\Models\Country
,默认的翻译将是 \MyApp\Models\CountryTranslation
。
要使用自定义类作为翻译模型,请将翻译类(包括命名空间)定义为参数。例如
<?php namespace App\Models; class Country extends Model { use \Simexis\MultiLanguage\Traits\Translatable; public $translatedAttributes = ['name']; protected $fillable = ['code', 'name']; /** * The database table used by the model. * * @var string */ protected $table = 'countries'; //convention for the translation model is `CountryTranslation`. public $translationModel = 'MyApp\Models\CountryAwesomeTranslation'; }