Download the PHP package mosab/translation without Composer
On this page you can find all versions of the php package mosab/translation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package translation
Translation Package
This package allows you to manage models and his translations in database.
Installation
-
This package can be used with Laravel 8.0 or higher. you can install the package via composer:
-
Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
- You should publish the migrations and seeder files:
-
if you want to publish only migrations files:
- if you want to publish only seeders files:
-
Run the migrations: After the migration have been published, you can create the tables for this package by running:
- Run the seeder
Automatically after executing seeding, your project will support Arabic and English languages. But, if you want to add a new language, you have to add this language in the following way:
-
add code for new language in TranslationsLanguageSeeder at $all_languages variable, like this:
- then you can add new language
Note:. you can manage Languages or modify it by dealing with translations_languages table directly.
- list the middleware class in the $middleware property of your app/Http/Kernel.php class:
Usage Instructions
Set Up Translatable Model Class
-
After determine the tables that you want to translate some column of them, modify the model for this table by inheriting TranslatableModel, like this:
- Add new protected variable called translatable, and the type of this variable must be array. You should add the columns you want to translate from this model in the translatable variable. for example:
Note:. you should not add the columns you want to translate in migration files. just add the another columns that have no translation. for example:
As the translatable columns are automatically stored at the translations table in database.
Inserting Translatable Models
When you need to insert new records, you should instantiate a new model instance and set attributes on the model. Then, call the save method on the model instance. Note that When you adding translation for a specific column, you must assign the value as:
- string value, then all translation values take the same value.
- or as array key => value. for example:
You may use the create method to "save" a new model, like this:
If you want to add validate for request you can use function called translation_rule(), this method forces the user to enter the value with its translations in all languages supported in the system. for example:
Updating Translatable Models
The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it and set any attributes you wish to update. Then, you should call the model's save method. When you update any translatable column, the old translation value delete from translations table and insert the new translation value in this table. for example:
You may use the update method to "update" a model, like this:
Deleting Translatable Models
To delete a model, you may call the delete method on the model instance: When you delete a model, his translations are automatically deleted from the translation table.
Retrieving Translatable Models
When you need to retrieve a model, you can do it by this way:
Then the model and his translation returned in this way:
In this package you can specify which language you want to return the translatable columns in, by sending the language code you want in the headers, like this:
By default if you don't send the accept-language header it will take English as the default language. All This is provided by RequestLanguage middleware.