Download the PHP package beyaty/laravel-translatable without Composer
On this page you can find all versions of the php package beyaty/laravel-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download beyaty/laravel-translatable
More information about beyaty/laravel-translatable
Files in beyaty/laravel-translatable
Package laravel-translatable
Short Description A Laravel package for multilingual models
License MIT
Informations about the package laravel-translatable
Laravel-Translatable
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.
If you want to store translations of your models into the database, this package is for you.
- Demo
- Installation
- Configuration
- Documentation
- Support
Laravel compatibility
Laravel | Translatable |
---|---|
5.0.x | 5.x |
4.2.x | 4.4.x |
4.1.x | 4.4.x |
4.0.x | 4.3.x |
Demo
Getting translated attributes
Saving translated attributes
Filling multiple translations
Installation in 4 steps
Step 1: Install package
Add the package in your composer.json by executing the command.
Next, add the service provider to app/config/app.php
Step 2: Migrations
In this example, we want to translate the model Country
. We will need an extra table country_translations
:
Step 3: Models
- The translatable model
Country
should use the traitDimsav\Translatable\Translatable
. - The convention for the translation model is
CountryTranslation
.
The array $translatedAttributes
contains the names of the fields being translated in the "Translation" model.
Step 4: Configuration
Laravel 4.*
Laravel 5.*
With this command, initialize the configuration and modify the created file, located under app/config/packages/dimsav/laravel-translatable/translatable.php
.
Note: There isn't any restriction for the format of the locales. Feel free to use whatever suits you better, like "eng" instead of "en", or "el" instead of "gr". The important is to define your locales and stick to them.
Configuration
The translation model
The convention used to define the class of the translation model is to append the keyword Translation
.
So if your model is \MyApp\Models\Country
, the default translation would be \MyApp\Models\CountryTranslation
.
To use a custom class as translation model, define the translation class (including the namespace) as parameter. For example:
Documentation
Please read the installation steps first, to understand what classes need to be created.
Available methods
Magic properties
To use the magic properties, you have to define the property $translatedAttributes
in your
main model:
Fallback locales
If you want to fallback to a default translation when a translation has not been found, enable this in the configuration
using the use_fallback
key. And to select the default locale, use the fallback_locale
key.
Example:
You can also define per-model the default for "if fallback should be used", by setting the $useTranslationFallback
property:
FAQ
I need some example code!
Examples for all the package features can be found in the code used for the tests.
I need help!
Got any question or suggestion? Feel free to open an Issue.
I want to help!
You are awesome! Watched the repo and reply to the issues. You will help offering a great experience to the users of the package. #communityWorks
I am getting collisions with other trait methods!
Translatable is fully compatible with all kinds of Eloquent extensions, including Ardent. If you need help to implement Translatable with these extensions, see this example.
Why do I get a mysql error while running the migrations?
If you see the following mysql error:
Then your tables have the MyISAM engine which doesn't allow foreign key constraints. MyISAM was the default engine for mysql versions older than 5.5. Since version 5.5, tables are created using the InnoDB storage engine by default.
How to fix
For tables already created in production, update your migrations to change the engine of the table before adding the foreign key constraint.
For new tables, a quick solution is to set the storage engine in the migration:
The best solution though would be to update your mysql version. And always make sure you have the same version both in development and production environment!