astrotomic / laravel-translatable
A Laravel package for multilingual models
Fund package maintenance!
Gummibeer
SarahSibert
Issuehunt
forest.astrotomic.info
Installs: 5 353 810
Dependents: 106
Suggesters: 3
Security: 0
Stars: 1 250
Watchers: 20
Forks: 158
Open Issues: 28
Requires
- php: ^8.0
- illuminate/contracts: ^9.0 || ^10.0 || ^11.0
- illuminate/database: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/legacy-factories: ^1.0.4
- laravel/pint: ^1.0
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^10.0
- dev-main
- v12.x-dev
- v11.15.1
- v11.15.0
- v11.14.1
- v11.14.0
- v11.13.0
- v11.12.1
- v11.12.0
- v11.11.0
- v11.10.0
- v11.9.1
- v11.9.0
- v11.8.3
- v11.8.2
- v11.8.1
- v11.8.0
- 11.7.1
- v11.7.0
- v11.6.1
- v11.6.0
- v11.5.2
- v11.5.1
- v11.5.0
- v11.4.0
- v11.3.0
- v11.2.1
- v11.2.0
- v11.1.3
- v11.1.2
- v11.1.1
- v11.1.0
- v11.0.0
- v10.0.0
- v9.5.0
- v9.4.0
- v9.3.0
- v9.2.0
- v9.1.0
- v9.0
- v8.1
- v8.0
- v7.3
- v7.2.1
- v7.2
- v7.1
- v7.0
- v6.1
- v6.0.1
- v6.0
- v5.6.1
- v5.6
- v5.5.1
- v5.5
- v5.4
- v5.3
- v5.2
- v5.1.2
- v5.1.1
- v5.1
- v5.0.1
- v5.0
- v4.5
- v4.4
- v4.3.2
- v4.3.1
- v4.3
- v4.2.1
- v4.2
- v4.1.1
- v4.1
- v4.0.1
- v4.0.0
- v3.0.1
- v3.0.0
- v2.0.0
- v1.0.1
- dev-dependabot/composer/phpunit/phpunit-tw-11.4
- dev-issue-121
- dev-issue-13
This package is auto-updated.
Last update: 2024-12-07 08:44:01 UTC
README
If you want to store translations of your models into the database, this package is for you.
This is a Laravel package for translatable models. Its goal is to remove the complexity in retrieving and storing multilingual model instances. With this package you write less code, as the translations are being fetched/saved when you fetch/save your instance.
The full documentation can be found at GitBook.
Installation
composer require astrotomic/laravel-translatable
Quick Example
Getting translated attributes
$post = Post::first(); echo $post->translate('en')->title; // My first post App::setLocale('en'); echo $post->title; // My first post App::setLocale('de'); echo $post->title; // Mein erster Post
Saving translated attributes
$post = Post::first(); echo $post->translate('en')->title; // My first post $post->translate('en')->title = 'My cool post'; $post->save(); $post = Post::first(); echo $post->translate('en')->title; // My cool post
Filling multiple translations
$data = [ 'author' => 'Gummibeer', 'en' => ['title' => 'My first post'], 'fr' => ['title' => 'Mon premier post'], ]; $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post
Filling multiple translations wrapped
You may define a wrapper property when creating new translations. Set the translations_wrapper
property in translatable config file:
'translations_wrapper' => 'translations',
Then just wrap multiple locales using that property:
$data = [ 'author' => 'Gummibeer', 'translations' => [ 'en' => ['title' => 'My first post'], 'fr' => ['title' => 'Mon premier post'], ], ]; $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post
Tutorials
- How To Add Multilingual Support to Eloquent
- How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application
- How to Add Multi-Language Models to Laravel QuickAdminPanel
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details. You could also be interested in CODE OF CONDUCT.
Security
If you discover any security related issues, please check SECURITY for steps to report it.
Credits
- Tom Witkowski current maintainer
- Dimitrios Savvopoulos original author
- David Llop refactoring testsuite
- Caneco artwork
- All Contributors
Versions
Treeware
You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.
It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy trees at offset.earth/treeware
Read more about Treeware at treeware.earth