, (*1)
Laravel Database Translations
For Laravel <= 5.4 use version 0.2.x, (*2)
For Laravel >= 5.5 use version 1.x, (*3)
Description
This package extends the functionality of spatie/laravel-translatable
and will allow you to manage your translation strings in the database while using the file based translations as a fallback., (*4)
The fallback priority is as follows:, (*5)
Database current locale -> Database fallback locale -> File system current locale -> File system fallback locale, (*6)
This is intentional as this package is meant to be a replacement for the file based language strings, (*7)
Usage
You can use the Laravel trans('group.key') or __('group.key') helpers to display your translations., (*8)
The blade @lang('key') directive also behaves the same way., (*9)
Namespacing works the same as before too, (*10)
In a Service Provider app('translator')->addNamespace('namespace', 'path/to/namespaced/folder/lang');, (*11)
and trans('namespace::group.key'), __('namepace::group.key') or @lang('namepace::group.key') to display, (*12)
By default the translations are cached for 1 day with the key {$locale}.{$namespace}.{$group}, caching can be
disabled in the config file., (*13)
The cache for a translation group is refreshed when one of the lines is updated via the translation repository., (*14)
Importing translation files
To load your current translation files in to the database run php artisan trans-db:load {locale} where {locale}
is a language code, (*15)
For example php artisan trans-db:load en will load all the files for 'en' and php artisan trans-db:load es will
load all the files for 'es', (*16)
Note: this is a catch all function and will include any files in language namespaces you have added to your service
providers as well as any in resources/lang, (*17)
Adding more translation keys
The included utility command php artisan trans-db:load is non-destructive so add your new key to the appropriate
translation file and run the command again, your new key will appear in the database whilst preserving your modified
translations, (*18)
Working with translations
Add: use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository; and inject it into your __construct() or method., (*19)
-
To retrieve all of the translations from the database: $translationRepository->all($related = [], $perPage), (*20)
-
Get a group of keys by namespace and group: $translationRepository->getItems($namespace, $group), (*21)
-
Get a specific key $translationRepository->getItem($namespace, $group, $key), (*22)
There are 2 included methods for updating translations via the translations repository., (*23)
-
$translationRepository->updateTranslationById($id, $locale, $value, $overwrite), (*24)
-
$translationRepository->updateTranslation(Translation $line, $locale, $value, $overwrite), (*25)
The second method requires an instance of the translation model., (*26)
The optional 4th parameter controls whether the new translation will overwrite the old one if it exists, default is true, (*27)
Requirements
PHP >=7.0
Laravel ~5.5
spatie/laravel-translatable ^2.1
Installation
-
As always: back up your database - I am not responsible for any data loss, (*28)
-
Install the package via Composer:, (*29)
composer require mike-zange/laravel-database-translation, (*30)
-
Comment the following Service Provider in config/app.php:, (*31)
Illuminate\Translation\TranslationServiceProvider::class,, (*32)
The new service provider is auto-loaded in laravel 5.5, (*33)
-
Publish the configuration file database.translations.php, (*34)
php artisan vendor:publish --provider="MikeZange\LaravelDatabaseTranslation\TranslationServiceProvider", (*35)
-
Make sure you have a locale set either via app()->setLocale() or in config/app.php, (*36)
-
Edit the config at config/database.translations.php to your requirements, the defaults should be okay for most uses, (*37)
-
Run php artisan migrate to create the database table, (*38)
To Do
- Allow configuration of caching time