spatie/laravel-translatable
Add multilingual fields to Eloquent models using a simple HasTranslations trait. Store translations as JSON on the model (no extra tables). Set/get translations per locale, switch app locale, fetch all translations, and even translate nested JSON keys via -> notation.
A small helper for making translations has been added for use in factories:
This is what a few possible usages look like:
/** [@var](https://github.com/var) $this \Illuminate\Database\Eloquent\Factories\Factory */
$this->translations('en', 'english')
// output: ['en' => 'english']
$this->translations(['en', 'nl'], 'english')
// output: ['en' => 'english', 'nl' => 'english']
$this->translations(['en', 'nl'], ['english', 'dutch'])
// output: ['en' => 'english', 'nl' => 'dutch']
The helper can also be used outside of factories using the following syntax:
\Illuminate\Database\Eloquent\Factories\Factory::translations('en', 'english');
// output: ['en' => 'english']
class UserFactory extends \Illuminate\Database\Eloquent\Factories\Factory {
public function definition(): array
{
return [
'bio' => $this->translations('en', 'english'),
];
}
}
How can I help you explore Laravel packages today?