ngocnh / translator
Laravel translator for multilingual Eloquent objects.
Requires
- php: >=5.5.9
- illuminate/support: 5.0.*|5.1.*
Requires (Dev)
This package is not auto-updated.
Last update: 2024-12-11 10:58:44 UTC
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).