ae Soluciones Web
About and download

Laravel Translator

Laravel Translator is the most complete and easiest to use Laravel manager for dealing with multiple locales and translations.

Extending Laravel's Router and URLGenerator allows us to deal with multiple locales in Laravel's way. And the Translator Repository allows us to deal with translations for those locales.

It is worth to say that, if we want, we can reserve a non-prefixed URL for our application's main locale. Thus, from all supported locales, we would have a clearer URL only for our main locale ( and prefixed ones for the rest of them (e.g.,

Code Examples

//Set a route
Route::get('apple', [
  'locales' => ['en', 'es'],
  'as' => 'apple_path',
  'uses' => 'fruitsController@apple' 

//Get a URL from a route name

//Get a translated text
echo tt(''); 

//Get a title for current locale
$article = App\Article::find(1);
echo $article->title;


Laravel 5.*


  • Routes and urls manager.
  • Complete repository for translate your whole application.
  • Eloquent extension for translate attributes inside your models.
  • Schema support.
  • Cache optimization.


Adding repository files to your proyect.

The best way to install Laravel Translator is with Composer. To install the most recent version, run the following command.

composer require aewebsolutions/laravel-translator


Then, you must add two lines in config/app.php. First, add a new provider to $providers array:


Second, for a proper access to TranslatorRespository, you must add a new facade to $aliases array:

'Translator' => Translator\Facades\TranslatorFacade::class,

Optionally, if you wish to use our Schema support for an easily translatable tables creation, you need to replace Schema facade. So, comment or remove Laravel's and add the new one:

//'Schema'    => Illuminate\Support\Facades\Schema::class,
'Schema'    => Translator\Facades\Schema::class,


Some files need be copied from vendor directory. Just run the following command:

php artisan vendor:publish --provider="Translator\TranslatorServiceProvider"

This command will publish next files:

  • app\Translation.php: an Eloquent model to deal with translations table,
  • database\migrations\2016_01_01_000000_create_translations_table.php: a migration file that will create translations table,
  • config/translation.php: a configuration file.


A translations table must be created in database. Running Artisan's migrate command would be enough. But if you need to add extra columns to translations table,
you may do it before running command. Just add them to Schema::up in migration file. Also, you may need to add them too to App\Translation::$fillablearray property.

Then, run:

php artisan migrate

Extending Router

Add a new line to App\Http\Kernel in order to extend Laravel's Router.

class Kernel extends HttpKernel
    use \Translator\Traits\KernelRouterExtender;


Configuration settings can be found in config/translator.php with plenty information.For basic usage, you must add all locales supported by application in $locales_available array. E.g., if application supports en, fr and es locales, array will look like this:

'locales_available' => [  'es', 'en' , 'fr' ],

Main locale takes its value from application's default locale (see locale in config/app.php file). So, do not forget to set it correctly.




This router is an extension of Laravel's, thus you will find original features exactly like you know them.

All working routes must have at least one locale available.

Route::get('apple', [
  'locales' => 'en',  
  'uses' => 'fruitsController@apple'

You can associate, not just a single locale, but a group of them too. Also, if you need a route to be available for the whole group of supported locales, you can use the ´all´ keyword.

Route::get('apple', ['locales' => ['en', 'es'] ,  'uses' => 'fruitsController@apple'  ]);
Route::get('peach', ['locales' => 'all' ,  'uses' => 'fruitsController@peach'  ]);

Taking defaults settings, next URIs will be available in application for routes above.

  • /apple
  • /es/apple
  • /peach
  • /es/peach
  • /fr/peach

Requesting any of them, current locale will be set automatically.

It is worth to say that URIs are generated dynamically in order to optimize. Router does not have to lead with multiplied rules, but just with those that match with requested locale. Imagine 40 route rules and 10 languages; instead of an unnecessary route rule multiplication, you will have exactly what you need: 40 rules.


You can assign locales to a group, not just to a single route.

Route::group([ 'locales' =>  'en' , 'prefix' => 'fruits' ], function(){

    Route::get('apple/{color}', ['locales' => 'es', 'as' =>  'apple_path',  'uses' => 'fruitsController@apple' ]);

    Route::get('peach/{color}', [ 'as' =>  'peach_path',  'uses' => 'fruitsController@peach' ]);



To get a relative or absolute URL from a route name for the current locale, as usually, call either Laravel's route or URL::route methods:

    route('apple_path', ['color' => 'red']);

If you ask for another locale, use dot notation. For getting a fallback locale's route, pass a locale (or set true) as the four argument.

    route('es.apple_path', ['color' => 'red'] );
    route('es.apple_path', ['color' => 'red'], true, 'en' );

Also, you can get all URLs for all supported locales. Call either Route::routes or routes new methods:

$url = routes('apple_path', ['color' => 'red'] );
echo $url->es;
echo $url->en;

Laravel's URL::current has been modified. Now, you can pass as an optional first argument a locale.


Translator Repository

Laravel Translator helps you to translate your application easily, dealing with translations from database. Translations could be managed directly by App\Translation Eloquent model. But, you should use the provided repository in order to guarantee stability. Translator facade, probably, is all you need.


You can get a translated text using Translator::text method or, better, the tthelper. This works like Laravel's trans. The tt method accepts a locale (optionally), a group name and a needle as its first argument, using dot notation: Let's assume that current locale is 'en':

echo tt('');  // output: apple
echo tt(''); // manzana

Sometimes a text may have no translation for a locale available; so, main locale is shown. Turning false its third argument you can avoid this behavior.

echo tt(''); // apple
echo tt('', [], false); // NULL

As trans do, you can make replacements:

echo tt('messages.welcome');  //output: Hi, :name.
echo tt('messages.welcome', ['name' => 'John']);  //output: Hi, John.

Pluralization. Translator::choice works like Laravel's trans_choice (see Laravel Documentation), but with further arguments.

echo Translator::choice('', 5, ['color' => 'red'], false);  // red apples.
echo tt('');  // :color apple|apples.

Last, If you need to get all texts from a group.needle, use Transalor::texts

$texts = Transalor::texts('');
echo $texts->en; // apple
echo $texts->es; // manzana
echo $texts->fr; // NULL


You can create a text for an specific locale:

Translator::create('es.fruits.peach', 'durazno');

Or create multiple locales at the same time:

Translator::create('fruits.peach', [
  'es' => 'durazno', 
  'en' => 'peach',
  'fr' => 'pêche'

Also, you can add extra attributes. Of corse, extra columns attributes should have been added to translations table and should have been included in App\Translation::$fillable array property.

Translator::create('fruits.peach', [
  'es' => 'durazno',  
  'en' => 'peach',
  'fr' => 'pêche'
 ], [
  'type' => 'infotext',
  'description' => 'Prunus persica‘s fruit'


Updating a text

Update a text for a specific locale or a group of them, with or without extra attributes:

Translator::update('es.fruits.peach', 'melocotón');
Translator::update('fruits.peach', [
  'es' => 'melocotón',
  'en' => 'peach'
  'type' => 'information'
Updating a text‘s group name or needle

Groups and needle are sensitive attributes, this is, they cannot be updated lightly without making a mess. In short, there cannot be duplicates for a So, even if you try, Translator::update method will not allow you to change this attributes. Instead, you must use Translator::updateGroupNeedle.

// Change the whole group name:
Translator::updateGroupNeedle('fruits', 'juicy_fruits');

// Change the needle, but not the group:
Translator::updateGroupNeedle('fruits.peach', 'fruits.yellow_peach');

//Change a single group.needle:
Translator::updateGroupNeedle('fruits.peach', 'juicy_fruits.peach');


Deleting is also easy with provided respository:

//Delete the whole group

//Delete the group.needle for all locales

//Delete a group.needle for a specific locale

Translatable Models

Laravel Translator includes, not only Translator repository, but also an Eloquent extension to manage multiple languajes directly inside your Models. Suppose you need an Article model. It would be truly helpful if you were able to get properties like this:

$article = App\Article::find(1);
echo $article->title; 
// output would be 'My title' if locale were 'en', 
// but 'Mi título' if locale were 'es';

Creating a table

Translatable columns have a clear syntaxis: column_name_locale. In order to simplify creation, optionally you can use our Schema extension (rememeber include facade. See #Installation). All you have to do is call $table->localize(['column_name']) to multiply column_name for each locale available in application. Also, you can pass an array of locales as a second argument.

Schema::create('articles', function ($table) {

    $table->localize([ 'title', 'body' ]);

The Model

Two things must be done: to extend your model to Translator\Eloquent\Model and to fill $translatable protected property.

class Article extends Translator\Eloquent\Model
    protected $translatable = ['title', 'body'];

    protected $fillable = ['title', 'body'];


For the following examples, 'en' will be considered the current locale. Remember current locale is a dynamic property.

Now, you can get a translatable property on any of the following ways.

$article = App\Article::find(1);
echo $article->title; // output 'My Title'
echo $article->title_en; // output 'My Title'
echo $article->title_es;  // output 'Mi título'

$title = $article->trans('title');
echo $title->en; // output 'My Title'
echo $title->es; // output 'Mi título'

To update, insert or fill a row for current locale, you are able to do it like you usually do:

$article = App\Article::find(1);
$article->title = 'My Title in English';
$article->title_es = 'Mi título en español';

But if you need to manage several locales at once, you can do it with an array, just like this:

//Modify an article
$article = App\Article::find(1);
$article->title = [
    'es' => 'Mi título en español',
    'en' => 'My title in English'

//Insert an article
$article = new App\Article;
    'title' => [
        'es' => 'Mi título en español',
        'en' => 'My Title in English'


In order to reduce database queries, groups should be stored in cache. Just look inside conf/translator.php and make sure that $cache is set TRUE. Laravel Translation uses your application's cache settings.

If you are using (as you should) provided repository to create, update or delete translations, then cache does not need to be flushed manually. Just be sure that cache_auto_flush is set TRUE and cache will be automatically flushed only for compromised groups each time Translator's created, updated or deleted events are fired.


TranslatorRespository (Facade: Translator)

Return Method
string text($localeGroupNeedle, $replacements = [], $orDefault = true)
Get a text for a
object texts($groupNeedle, $replacements = [])
Get all texts for a group.needle.
string choice($localeGroupNeedle, $count = 1, $replacements = [], $orDefault = true)
Choice between two or more string options.
Collection getGroup($name)
Get a Collection from a group name.
Collection getLocale($locale = NULL, $group = NULL)
Get a Collection from a locale and, optionally, a group name.
void cacheFlush($group = NULL)
Remove a group or all groups from cache.
Collection get($localeGroupNeedle = NULL)
Get a Collection from a locale (optionally), a group name and a needle. By default, get current locale.
void delete($localeGroupNeedle)
Delete a whole group or a group.needle for a specific locale or for all locales.
void create($localeGroupNeedle, $texts, array $extra = [])
Insert a text for an specific locale or for all locales at once.
void update($localeGroupNeedle, $texts, array $extra = [])
Edit a text for an specific locale or for all locales at once.
void updateGroupNeedle($groupNeedle, $newGroupNeedle)
Edit the whole group name, edit the needle but not the group or edit a single group.needle row:
void created(Closure $callback)
Register a listener for created event. Callback's params: array $locales, $group, $needle.
void updated(Closure $callback)
Register a listener for updated event. Callback's params: array $locales, $group, $needle.
void deleted(Closure $callback)
Register a listener for deleted event. Callback's params: array $locales, $group, $needle.

TranslatorURL (Facade: URL)

TranslatorURL extends URLGenerator.

Return Method
string route($name, $parameters = [], $absolute = true, $default = NULL)
Get the URL to a named route and locale (current locale, by default). Use dot notation.
object routes($name, $parameters = [], $absolute = true)
Get an object with all URLs for all locales to a named route.
string current($locale = NULL, $absolute = true)
Get the current URL for current locale or for another supported locale.
array currentAll($absolute = true)
Get all current URLs for all locales available.
bool hasRouteLocale($route, $locale)
Verify if route has a locale.
bool hasLocale($routename, $locale)
Verify if a route' name has a locale.
string localize($uri, $locale, $absolute = true, $uriHasPrefix = true)
Generate an absolute or relative URL for a given URI and a locale.


Laravel Translator is licensed under the MIT License.

Copyright 2016 Ángel Espro


Error. Complete required fields and try again,
Error: message has not delivered. Please, try again or call us.