ngocnh/translator
Composer 安装命令:
composer require ngocnh/translator
包简介
Laravel translator for multilingual Eloquent objects.
README 文档
README

This package gives you an easy way to translate Eloquent models into multiple languages.
// Display the default title for an Eloquent object.
echo $foo->title;
// Change the current language to Swedish.
App::setLocale('sv');
// Display the translated title in Swedish.
echo $foo->title;
Installation
Require this package, with Composer, in the root directory of your project.
composer require ngocnh/translator
Add the service provider to config/app.php in the providers array.
'Ngocnh\Translator\TranslatorServiceProvider'
Configuration
Laravel Translator requires configuration. To get started, you'll need to publish all vendor assets:
php artisan vendor:publish
This will create a config/translator.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.
This also creates a default locales migration in your database/migrations directory.
Locale Eloquent Model
This option locale is your full namespaced path for the Locale Eloquent object.
Locale Identifier Column
This option column is the column in your locales table which you want to compare the current set locale in your application. This column is compared with the App::getLocale() method to fetch the translations.
Fallback Support
This option fallback check whether you want to use the fallback translations if the current translation doesn't exist.
Documentation
Below we have examples of migrations, models, seeds and templates. There also is an example application that you can use as reference.
Migrations
Here's an example of the localisations migration.
Schema::create('locales', function(Blueprint $table)
{
$table->increments('id');
$table->string('language', 2); // en, sv, da, no, etc.
$table->timestamps();
});
This example migration comes out of the box with this package. When you run endor:publish a default locales migration will be added to you database/migrations directory.
Add the Laravel migration for the base table which you want to translate.
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('thumbnail');
$table->timestamps();
});
Add the Laravel migration for the translatable relation table.
Schema::create('article_translations', function(Blueprint $table)
{
$table->increments('id');
// Translatable attributes
$table->string('title');
$table->string('content');
// Translatable attributes
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('locale_id')->unsigned()->index();
$table->foreign('locale_id')->references('id')->on('locales')->onDelete('cascade');
$table->unique(['article_id', 'locale_id']);
$table->timestamps();
});
Models
Firstly you'll need to setup the Locale Eloquent model. Then add the Locale model path to the configuration file.
<?php
namespace Acme\Locales;
use Illuminate\Database\Eloquent\Model;
class Locale extends Model
{
/**
* @var array
*/
protected $fillable = ['language'];
}
Here's an example of a translatable Laravel Eloquent model. Remember to fill the $fillable array the translatable attributes.
<?php
namespace Acme\Articles;
use Illuminate\Database\Eloquent\Model;
use Ngocnh\Translator\Translatable;
use Ngocnh\Translator\Contracts\Translatable as TranslatableContract;
class Article extends Model implements TranslatableContract
{
use Translatable;
/**
* @var array
*/
protected $fillable = ['title', 'content', 'thumbnail'];
/**
* @var string
*/
protected $translator = 'Acme\Articles\ArticleTranslation';
/**
* @var array
*/
protected $translatedAttributes = ['title', 'content'];
}
The ArticleTranslation basically is an empty Eloquent object.
<?php
namespace Acme\Articles;
use Illuminate\Database\Eloquent\Model;
class ArticleTranslation extends Model {}
Seed
Before you start to populate your database with translations you'll need to add languages to the locales table that you want to support. Below is an example seeder.
<?php
use Acme\Locales\Locale;
use Illuminate\Database\Seeder;
class LocaleTableSeeder extends Seeder
{
public function run()
{
$languages = ['en', 'sv', 'no'];
foreach ($languages as $language)
{
Locale::create(compact('language'));
}
}
}
Templates
That's it! You're done. Now you can do:
<h1>{{ $article->title }}</h1>
<img src="{{ $article->thumbnail }}">
<p>{{ $article->content }}</p>
If you want to fetch a specific translation that isn't the current one you can specify it in the translate method as in the example below.
<h1>{{ $article->translate('sv')->title }}</h1>
<img src="{{ $article->thumbnail }}">
<p>{{ $article->translate('sv')->content }}</p>
Example
If you want a working example you can visit the example respository for this package. It's a Laravel 5 application that utilies the translator package.
License
Laravel Translator is licensed under The MIT License (MIT).
ngocnh/translator 适用场景与选型建议
ngocnh/translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translation」 「translations」 「language」 「translator」 「laravel」 「multilingual」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ngocnh/translator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ngocnh/translator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ngocnh/translator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
A custom URL rule class for Yii 2 which allows to create translated URL rules
Set Links with a specific language parameter
统计信息
- 总下载量: 1.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-19