A easy package to manage database translations for Laravel
composer require jva91/translation
or manually add in composer.json
"require": {
"jva91/translation": "0.1.x-dev"
}
After add this line run composer install
This package package contains auto discovery. When auto discovery is disabled register the service provider in config/app.php
'providers' => [
// ...
Jva91\Translation\TranslationServiceProvider::class,
]
This package publishes a config file.
php artisan vendor:publish --provider="Jva91\Translation\TranslationServiceProvider" --tag="config"
This package contains a default migration this will be executed with
php artisan migrate
This package contains a default model
Jva91\Translation\Models\Translation
The package can be used by using the delivered trait Jva91\Translation\Traits\TranslationsTrait
class FakeModel extends Model
{
use TranslationsTrait;
public static $transFields = ['name'];
// ...
$transFields
are the columns in the table. This column must be an integer so the trait can store his translation.key integer in this column. There can be multiple columns in this array.
The trait uses the config('app.locale')
setting. The extra translatable locales can be set in the config file translation.php
'locales' => [
'en_GB'
],
To get translation with the default app.locale
$fakeModel->name
To get translation with a default locale
$fakeModel->name_en_GB
To get the translation key integer from the model
$fakeModel->getOriginalTranslationKey('name')
To delete a translation for a specific locale
$fakeModel->deleteForLocale('name', 'nl_NL');
This package is covered by unit tests
- Console command to delete unused translations from table