This package allows you to use a database to store your translations. This is useful if you want to allow your users to edit translations.
It does not contain any UI, but it provides a model with which you can create your own UI. On top of that it is easy to extend and add more translation sources.
You can install the package via composer following official Spatie - Laravel Translation Loader documentation.
Package doesn't need any special configuration. You can use the trans()
helper as you normally would.
Notice: It does not work correctly with JSON files and __()
helper.
Once you set up the package and try to use the trans()
helper, it will first check if the translation exists in the database. If it does, it will return the translation from the database. If it doesn't, it will return the translation from the language files. This allows you to build a translation editor in your application and use:
To create new translations in your database - this package provides a model LanguageLine
:
use Spatie\TranslationLoader\LanguageLine; LanguageLine::create([ 'group' => 'validation', 'key' => 'required', 'text' => ['en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld'],]);
It accepts the following parameters:
group
- the group of the translation (e.g. validation
or auth
)key
- the key of the translation (e.g. required
or failed
)text
- an array of translations for each language (e.g. ['en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld']
)That's it. Now if you call trans('validation.required')
it will return the translation from the database.
This package mainly helps you add different drivers for your translations. By default, it's a database driver, but you can also use a .csv file by creating your own driver.
Repository: https://github.com/LaravelDaily/laravel11-localization-course/tree/lesson/packages/spatie