This is a package for translating the contents of different Laravel projects.
Run the following command to pull in the latest version:
composer require jobmetric/laravel-translation
Undergoing continuous enhancements, this package evolves each day, integrating an array of diverse features. It stands as an indispensable asset for enthusiasts of Laravel, offering a seamless way to harmonize their projects with translation database models.
In this package, you can employ it seamlessly with any model requiring database translation.
Now, let's delve into the core functionality.
php artisan migrate
Meet the HasTranslation
class, meticulously designed for integration into your model. This class automates essential tasks, ensuring a streamlined process for:
In the first step, you need to connect this class to your main model.
use JobMetric\Translation\HasTranslation;
class Post extends Model
{
use HasTranslation;
}
When you add this class, you will have to implement TranslationContract
to your model.
use JobMetric\Translation\Contracts\TranslationContract;
class Post extends Model implements TranslationContract
{
use HasTranslation;
}
Now you have to use the translationAllowFields function and you have to add it to your model.
use JobMetric\Translation\Contracts\TranslationContract;
class Post extends Model implements TranslationContract
{
use HasTranslation;
public function translationAllowFields(): array
{
return [
'title',
'body',
];
}
}
This function is for you to declare what translation fields you need for this model, and you should return them here as an
array
.
Now, you can use the HasTranslation
class to translate your model. The following example demonstrates how to create a new post with translations:
$post = Post::create([
'status' => 'published',
]);
$post->translate('en', [
'title' => 'Post title',
'body' => 'Post body',
]);
$post->translate('de', [
'title' => 'Post Titel',
'body' => 'Post Inhalt',
]);
$post->translate('fr', [
'title' => 'Titre de la publication',
'body' => 'Corps de poste',
]);
$post->translate('fa', [
'title' => 'عنوان پست',
'body' => 'متن پست',
]);
You could also do this inside a
foreach
, it was more for show off.
You can also use the translate
method to update the translations:
$post->translate('de', [
'title' => 'Post Titel',
'body' => 'Post Inhalt',
]);
translation has one relationship
translation has many relationships
scope locale for select translation relationship
Post::translationTo('en')->get();
// or
Post::where('status', 'published')->translationTo('en')->get();
scope locale for select translations relationship
Post::translationsTo('en')->get();
// or
Post::where('status', 'published')->translationsTo('en')->get();
store translates for the model
$post->translate('en', [
'title' => 'Post title',
'body' => 'Post body',
]);
load translation after model loaded
$post = Post::query()->where('id', 1)->first()->withTranslation('en', 'title');
load translations after model loaded
$post = Post::query()->where('id', 1)->first()->withTranslations();
check model has translation field
$post->hasTranslationField('title');
get translation for the model
$post->getTranslation('title');
get translations for the model
$post->getTranslations();
forget translation for the model
$post->forgetTranslation('title', 'en');
forget translations for the model
$post->forgetTranslations('en');
This package contains several events for which you can write a listener as follows
Event | Description |
---|---|
TranslationStoredEvent |
This event is called after storing the translation. |
TranslationForgetEvent |
This event is called after forgetting the translation. |
Thank you for considering contributing to the Laravel Translation! The contribution guide can be found in the CONTRIBUTING.md.
The MIT License (MIT). Please see License File for more information.