PHPackages                             ricardosierra/translation - 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. ricardosierra/translation

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

ricardosierra/translation
=========================

An easy automatic database driven translator for Laravel 5.\*

v3.2.0(5y ago)17524MITPHPPHP &gt;=7.2

Since Feb 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ricardosierra/translation)[ Packagist](https://packagist.org/packages/ricardosierra/translation)[ Docs](https://sierratecnologia.com/packages/translation/)[ Fund](https://ricasolucoes.com.br/open-source/support-us)[ GitHub Sponsors](https://github.com/ricasolucoes)[ RSS](/packages/ricardosierra-translation/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (73)Used By (4)

Laravel Translation
===================

[](#laravel-translation)

[![Travis CI](https://camo.githubusercontent.com/e76d5d5d7b0d9e3c04a67cb71f3332a273efa82cbf221df165e805da8ab5a1b3/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7269636172646f7369657272612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/ricardosierra/translation)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/de044347dbdb8eecf7d5331b7b1c83a4ac4654ac97cf6bd9d2167679ed3a627b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7269636172646f7369657272612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ricardosierra/translation/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/209de0de39a3b4549c5b036cd840c56aea0e20997b529603426a7dc6a6c3e449/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7269636172646f7369657272612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ricardosierra/translation)[![Total Downloads](https://camo.githubusercontent.com/0abbf23cafe5f7cfccab0d0376372bed88d4430006ef3aef6deaa9f5420cd334/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7269636172646f7369657272612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ricardosierra/translation)[![License](https://camo.githubusercontent.com/cf6a305732ba55a7731f1659dcc78103f2c8b25ac0e2b8ecc8f2537a2b85e959/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7269636172646f7369657272612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ricardosierra/translation)

Description
-----------

[](#description)

Translation is a developer friendly, database driven, automatic translator for Laravel 5.\*. Wouldn't it be nice to just write text regularly on your application and have it automatically translated, added to the database, and cached at runtime? Take this for example:

Controller:

```
public function index(Request $request)
{
    return view('home.index');
}

```

View:

```
@extends('layout.default')

{{ _t('Welcome to our home page') }}

```

Seen:

```
Welcome to our home page

```

When you visit the page, you won't notice anything different, but if you take a look at your database, your default application locale has already been created, and the translation attached to that locale.

Now if we set locale to something different, such as French (fr), it'll automatically translate it for you.

Controller:

```
public function index(Request $request)
{
    Translation::setLocale('fr');

    return view('home.index');
}

```

View:

```
@extends('layout.default')

{{ _t('Welcome to our home page') }}

```

Seen:

```
Bienvenue sur notre page d'accueil

```

We can even use placeholders for dynamic content:

View:

```
{{ _t('Welcome :name, to our home page', ['name' => 'John']) }}

```

Seen:

```
Bienvenue John , à notre page d'accueil

```

Notice that we didn't actually change the text inside the view, which means everything stays completely readable in your locale to you (the developer!), which means no more managing tons of translation files and trying to decipher what text may be inside that dot-notated translation path:

```
{{ trans('page.home.index.welcome') }}

```

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

[](#installation)

Require the translation package

```
composer require ricardosierra/translation

```

Add the service provider to your `config/app.php` config file (Laravel 5.8 and lower)

```
Translation\TranslationServiceProvider::class,

```

Add the facade to your aliases in your `config/app.php` config file (Laravel 5.8 and lower)

```
'Translation' => Translation\Facades\Translation::class,

```

Publish the migrations

```
php artisan vendor:publish --provider="Translation\TranslationServiceProvider"

```

Run the migrations

```
php artisan migrate

```

Your good to go!

Usage
-----

[](#usage)

Anywhere in your application, either use the the shorthand function (can be disabled in config file)

```
_t('Translate me!')

```

Or

```
Translation::translate('Translate me!')

```

This is typically most useful in blade views:

```
{{ _t('Translate me!') }}

```

And you can even translate models easily by just plugging in your content:

```
{{ _t($post->title) }}

```

Or use placeholders:

```
{{ _t('Post :title', ['title' => $post->title]) }}

```

In your `locales` database table you'll have:

```
| id | code |  name  | display_name | lang_code |
   1    en    English      NULL          NULL

```

In your `translations` database table you'll have:

```
| id | locale_id | translation_id | translation |
  1        NULL         NULL        'Translate me!'

```

To switch languages for the users session, all you need to call is:

```
Translation::setLocale('fr') // Setting to French locale

```

Locales are automatically created when you call the `Translation::setLocale($code)` method, and when the translate function is called, it will automatically create a new translation record for the new locale, with the default locale translation. The default locale is taken from the laravel `app.php` config file.

Now, once you visit the page you'll have this in your `locales` table:

```
| id | code | name | display_name | lang_code |
   1    en    English     NULL         NULL
   2    fr    French      NULL         NULL

```

And this in your `translations` table:

```
| id | locale_id | translation_id | translation |
   1        1         NULL        'Translate me!'
   2        2          1          'Traduisez-moi !'

```

You can now update the translation on the new record and it will be shown wherever it's called:

```
_t('Translate me!')`

```

###### Need to translate a single piece of text without setting the users default locale?

[](#need-to-translate-a-single-piece-of-text-without-setting-the-users-default-locale)

Just pass in the locale into the third argument inside the translation functions show above like so:

View:

```
{{ _t('Our website also supports russian!', [], 'ru') }}

{{ _t('And french!', [], 'fr') }}

```

Seen:

```
Наш сайт также поддерживает России !

Et françaises !

```

This is great for showing users that your site supports different languages without changing the entire site language itself. You can also perform replacements like usual:

View:

```
{{ _t('Hello :name, we also support french!', ['name' => 'John Doe'], 'fr') }}

```

Seen:

```
Bonjour John Doe , nous soutenons aussi le français !

```

Performing this will also create the locale in your database, save the translation, and cache it in case you need it again.

You must provide you're own way of updating translations (controllers/views etc) using the eloquent models provided.

Injecting Translation
---------------------

[](#injecting-translation)

As of `v1.3.4` you can now inject the `Translation` contract into your controllers without the use of a facade:

```
