Top Related Projects
[Deprecated] A Laravel package for multilingual models
Easy localization for Laravel
Manage Laravel translation files
Quick Overview
Astrotomic/laravel-translatable is a Laravel package that provides a trait for easy translation of Eloquent models. It allows you to store and retrieve translated content for your models in multiple languages, seamlessly integrating with Laravel's ORM.
Pros
- Easy integration with existing Laravel projects
- Supports multiple languages and fallback locales
- Provides a clean and intuitive API for working with translations
- Offers flexibility in database schema design
Cons
- May introduce additional complexity for simple projects
- Requires careful consideration of database structure and queries for optimal performance
- Limited to Laravel framework, not suitable for non-Laravel PHP projects
Code Examples
- Defining a translatable model:
use Astrotomic\Translatable\Translatable;
class Post extends Model
{
use Translatable;
public $translatedAttributes = ['title', 'content'];
}
- Saving translations:
$post = Post::create([
'en' => ['title' => 'My Post', 'content' => 'English content'],
'es' => ['title' => 'Mi Publicación', 'content' => 'Contenido en español'],
]);
- Retrieving translations:
$post = Post::first();
echo $post->translate('es')->title; // Outputs: Mi Publicación
echo $post->translate('en')->content; // Outputs: English content
- Querying translated content:
$posts = Post::whereTranslation('title', 'My Post')->get();
Getting Started
-
Install the package via Composer:
composer require astrotomic/laravel-translatable
-
Add the
Translatable
trait to your model and define translatable attributes:use Astrotomic\Translatable\Translatable; class Post extends Model { use Translatable; public $translatedAttributes = ['title', 'content']; }
-
Create migration for translations table:
Schema::create('post_translations', function (Blueprint $table) { $table->increments('id'); $table->integer('post_id')->unsigned(); $table->string('locale')->index(); $table->string('title'); $table->text('content'); $table->unique(['post_id', 'locale']); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); });
-
Use the model with translations in your application:
$post = Post::create([ 'en' => ['title' => 'English Title', 'content' => 'English content'], 'es' => ['title' => 'Título en Español', 'content' => 'Contenido en español'], ]);
Competitor Comparisons
[Deprecated] A Laravel package for multilingual models
Pros of laravel-translatable
- Longer history and more established in the Laravel community
- Supports older Laravel versions (5.4+)
- More comprehensive documentation with detailed examples
Cons of laravel-translatable
- Less frequent updates and maintenance
- Lacks some modern Laravel features and optimizations
- Slightly more complex setup process
Code Comparison
laravel-translatable:
use Dimsav\Translatable\Translatable;
class Product extends Model
{
use Translatable;
public $translatedAttributes = ['name', 'description'];
}
Astrotomic/laravel-translatable:
use Astrotomic\Translatable\Translatable;
class Product extends Model
{
use Translatable;
public $translatedAttributes = ['name', 'description'];
}
The basic implementation is very similar between the two packages. However, Astrotomic/laravel-translatable offers more advanced features and optimizations in its underlying code and additional methods.
Both packages provide robust solutions for handling translations in Laravel applications, but Astrotomic/laravel-translatable is generally considered more modern and actively maintained. It offers better performance and compatibility with newer Laravel versions, while laravel-translatable may be a better choice for projects using older Laravel versions or requiring extensive documentation.
Easy localization for Laravel
Pros of laravel-localization
- Provides URL-based localization, making it easier to manage multilingual routes
- Includes middleware for automatic language detection and switching
- Offers helper functions for language-specific redirects and URL generation
Cons of laravel-localization
- Focuses primarily on route localization, not content translation
- May require more manual configuration for complex localization scenarios
- Less integrated with Eloquent models compared to laravel-translatable
Code Comparison
laravel-localization:
Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
Route::get('welcome', function() {
return view('welcome');
});
});
laravel-translatable:
class Product extends Model
{
use \Astrotomic\Translatable\Translatable;
public $translatedAttributes = ['name', 'description'];
}
The laravel-localization example shows how to group routes with localized prefixes, while laravel-translatable demonstrates how to define translatable attributes in Eloquent models. laravel-localization is more focused on URL-based localization, whereas laravel-translatable provides a solution for translating model attributes directly.
Manage Laravel translation files
Pros of laravel-translation-manager
- Provides a user-friendly web interface for managing translations
- Supports automatic translation using external services like Google Translate
- Allows for easy import/export of translation files
Cons of laravel-translation-manager
- Focuses on managing static translations rather than dynamic content
- May require additional setup and configuration for complex translation scenarios
- Less suitable for projects requiring database-driven translations
Code Comparison
laravel-translation-manager:
use Barryvdh\TranslationManager\Models\Translation;
$translations = Translation::where('group', 'messages')->get();
foreach ($translations as $translation) {
echo $translation->key . ': ' . $translation->value;
}
laravel-translatable:
use App\Models\Post;
$post = Post::find(1);
$post->translate('fr')->title = 'Mon titre en français';
$post->save();
echo $post->translate('fr')->title;
The code examples demonstrate the different approaches of these packages. laravel-translation-manager focuses on managing static translations, while laravel-translatable is designed for translating database-driven content.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Introduction
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
Package | Laravel | PHP |
---|---|---|
v11.13 - v11.15 | 9.* / 10.* / 11.* | ^8.0 |
v11.12 - v11.12 | 8.* / 9.* / 10.* | ^8.0 |
v11.10 - v11.11 | 8.* / 9.* | ^8.0 |
v11.6 - v11.9 | 5.8.* / 6.* / 7.* / 8.* | >=7.2 |
v11.4 - v11.5 | 5.6.* / 5.7.* / 5.8.* / 6.* | >=7.1.3 |
v11.0 - v11.3 | 5.6.* / 5.7.* / 5.8.* | >=7.1.3 |
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
Top Related Projects
[Deprecated] A Laravel package for multilingual models
Easy localization for Laravel
Manage Laravel translation files
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot