PHPackages                             aewebsolutions/laravel-translator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. aewebsolutions/laravel-translator

ActiveLibrary[Localization &amp; i18n](/categories/localization)

aewebsolutions/laravel-translator
=================================

Laravel's package for managing multiple locales and translation

1.0.0(10y ago)0235[1 issues](https://github.com/aewebsolutions/laravel-translator/issues)MITPHPPHP &gt;=5.4.0

Since Apr 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aewebsolutions/laravel-translator)[ Packagist](https://packagist.org/packages/aewebsolutions/laravel-translator)[ Docs](https://github.com/aewebsolutions/laravel-translator/)[ RSS](/packages/aewebsolutions-laravel-translator/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel Translator
==================

[](#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 ([www.site.com/apple](http://www.site.com/apple)) and prefixed ones for the rest of them (e.g., [www.site.com/fr/apple](http://www.site.com/fr/apple)).

To know more about the developer, visit [www.aesolucionesweb.com.ar](http://www.aesolucionesweb.com.ar)

Code Examples
-------------

[](#code-examples)

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

//Get a URL from a route name
route('es.apple_path');

//Get a translated text
echo tt('fruits.apple');

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

Compatibility
-------------

[](#compatibility)

Laravel 5.\*

Features
--------

[](#features)

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

Installation
------------

[](#installation)

### Adding repository files to your proyect.

[](#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

```

### Integration

[](#integration)

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

```
Translator\TranslatorServiceProvider::class,

```

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,

```

### Publishing

[](#publishing)

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.

### Migration

[](#migration)

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::$fillable`array property.

Then, run:

```
php artisan migrate

```

### Extending Router

[](#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;
    //etc.
}
```

Configuration
-------------

[](#configuration)

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.

Usage
-----

[](#usage)

### Routing

[](#routing)

#### Basics

[](#basics)

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.

#### Groups

[](#groups)

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' ]);

});
```

### URL

[](#url)

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.

```
URL::current('es');
```

### Translator Repository

[](#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.

#### Getting

[](#getting)

You can get a translated text using `Translator::text` method or, better, the `tt`helper. 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: **locale.group.needle**. Let's assume that current locale is 'en':

```
echo tt('fruits.apple');  // output: apple
echo tt('es.fruits.apple'); // 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('fr.fruits.apple'); // apple
echo tt('fr.fruits.apple', [], 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('en.fruits.apple', 5, ['color' => 'red'], false);  // red apples.
echo tt('en.fruits.apple');  // :color apple|apples.
```

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

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

#### Creating

[](#creating)

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

[](#updating)

##### Updating a text

[](#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

[](#updating-a-texts-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 locale.group.needle. 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

[](#deleting)

Deleting is also easy with provided respository:

```
//Delete the whole group
Translator::delete('fruits');

//Delete the group.needle for all locales
Translator::delete('fruits.apple');

//Delete a group.needle for a specific locale
Translator::delete('es.fruits.apple');
```

### Translatable Models

[](#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. Would it 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

[](#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->increments('id');
    $table->text('body');
    $table->string('title');
    $table->timestamps();

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

#### The Model

[](#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'];

}
```

Now, you can **get** a translatable property like this:

```
$article = App\Article::find(1);
echo $article->title; // output 'My Title'

$title = $article->trans('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. 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'
];
$article->save();

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

Cache
-----

[](#cache)

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.

Methods
-------

[](#methods)

### TranslatorRespository (Facade: Translator)

[](#translatorrespository-facade-translator)

ReturnMethodstring**text**($localeGroupNeedle, $replacements = \[\], $orDefault = true)
 Get a text for a locale.group.needle.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-facade-url)

TranslatorURL extends URLGenerator.

ReturnMethodstring**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.License
-------

[](#license)

Laravel Translator is licensed under the [MIT License](http://opensource.org/licenses/MIT).

Copyright 2016 Ángel Espro

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3668d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/570963acc5f21421018e525caabc1f2cb3b4a4388e08c9dac2e81008152622d8?d=identicon)[aewebsolutions](/maintainers/aewebsolutions)

---

Top Contributors

[![aewebsolutions](https://avatars.githubusercontent.com/u/18664581?v=4)](https://github.com/aewebsolutions "aewebsolutions (48 commits)")

---

Tags

laravelroutertranslationlocale

### Embed Badge

![Health badge](/badges/aewebsolutions-laravel-translator/health.svg)

```
[![Health](https://phpackages.com/badges/aewebsolutions-laravel-translator/health.svg)](https://phpackages.com/packages/aewebsolutions-laravel-translator)
```

###  Alternatives

[codezero/laravel-localized-routes

A convenient way to set up, manage and use localized routes in a Laravel app.

543638.1k4](/packages/codezero-laravel-localized-routes)[laravel-lang/locales

Basic functionality for working with localizations

134.5M12](/packages/laravel-lang-locales)[opgginc/codezero-laravel-localized-routes

A convenient way to set up, manage and use localized routes in a Laravel app.

2770.1k1](/packages/opgginc-codezero-laravel-localized-routes)[longman/laravel-multilang

Package to integrate multi language (multi locale) functionality in Laravel 5.x

5514.4k1](/packages/longman-laravel-multilang)[awes-io/localization-helper

Package for convenient work with Laravel's localization features

3527.1k4](/packages/awes-io-localization-helper)[jrmajor/fluent

Fluent localization system for PHP

2716.9k5](/packages/jrmajor-fluent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
