Download the PHP package marksitko/laravel-deep-translatable without Composer
On this page you can find all versions of the php package marksitko/laravel-deep-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marksitko/laravel-deep-translatable
More information about marksitko/laravel-deep-translatable
Files in marksitko/laravel-deep-translatable
Package laravel-deep-translatable
Short Description Translates automatic eloquent model attributes via DeepL
License MIT
Homepage https://github.com/marksitko/laravel-deep-translatable
Informations about the package laravel-deep-translatable
Automatic translatables Eloquent models with DeepL
Laravel-Deep-Translatable is more than just an API Wrapper for DeepL. It contains a trait to make Eloquent models automatic translatable. So you can easily publish a Blog post (or whatever) and create multiple language versions of it quite automatically. If you need to adjust or refine the translation, just got for it by using the provided updateTranslation
method.
Installation
Install the package via composer:
Laravel-Deep-Translatable comes with package discovery and Laravel will register the service provider automatically. Just in case you wanna add it manually, you should provide it in config/app.php
Service provider
Next you should publish the configuration and migration.
Run migrations
Configuration
You have at least to provide a DeepL API key in your .env
file.
Read how to accessing the DeepL API
Optional configurations:
Usage
First implement the Translatable
Interface, add the UseDeepTranslations
Trait and define your translatable
keys.
Translate and persist
Call translateViaDictionary()
method on your model with your target language.
Translate without persisting
Retrieve all translations form a model
Update/Refine a translation
To refine an existing Eloquent translation, call updateTranslation()
method. It accepts three parameters.
- The key to refined.
- The target language to refined
- The refined translated string
Just use the API Client without Eloquent models
The third param in the translate method is an optional $options
array. Here you can define a specific source language or additional query options.
Translate value from stored json object
If you'r model has a json column which stored a value which you would like to translate, you can define the path with dot notaions in the $translatable
property.
How it works
If you call translateViaDictionary()
on your Eloquent model, each translation is stored in a special translation_dictionaries
table. The source text is stored as a key and each translation is stored as a json with the specified target language code. This concept was implemented to avoid requests for already translated strings.
For each translation call, the translation_dictionaries
table is first checked to see if a translation already exists for the specified target language. If a translation exists it will be returned from the database, if not it will be retrieved from DeepL. You can also use this method to keep your own dictionary for specific words.
Push translation calls to the queue
By default the translateViaDictionary()
method is executed synchronously. For one model or simple strings this is fine, but if you plan to perform bulk translations it is recommended to use the queue for translate and persisting models.
- Setup your queue
- In your
.env
file setDEEP_TRANSLATABLE_USE_QUEUE=true
- If you would use a specific queue, you can define the name by
DEEP_TRANSLATABLE_QUEUE_NAME=translating
Switch between translations
To swap translatables keys on your model, you just need to call your site with an additional query parameter which defines your target language code.
For example:
If your default site for a particular post would be:
https://yousite.test/posts/1
then call it up for the german translation as follows:
https://yousite.test/posts/1?lang=de
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.