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

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

josephnc/translation
====================

An automatic Laravel 5 translator

v1.0(6y ago)2191MITPHPPHP ^7.1

Since Aug 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/JosephNC/translation)[ Packagist](https://packagist.org/packages/josephnc/translation)[ RSS](/packages/josephnc-translation/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (8)Versions (2)Used By (0)

Translation
===========

[](#translation)

[![Travis CI](https://camo.githubusercontent.com/a17f4c10b1fbfd15707ebd4c85af02feb194e53cb6af4cbbd00e61287b9501dc/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6f736570686e632f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/josephnc/translation)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9f32677c594b5c8baa151cd3ca66754e692e2b370c319c41594164d3c78eadb1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a6f736570686e632f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/josephnc/translation/?branch=master)[![License](https://camo.githubusercontent.com/875b53d6c8eede337c65f6a3ff34390fab574434557a2418d544ae05431c566a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6f736570686e632f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/josephnc/translation)

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

[](#description)

Forked and updated from

Now the fastest automatic translator for laravel 5

For example:

Controller:

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

View:

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

{{ ___trans('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()
{
    App::setLocale('fr');

    // Or Translation::setLocale( 'fr' );

    return view('home.index');
}
```

View:

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

{{ __trans('Welcome to our home page') }}
```

Seen:

```
Bienvenue sur notre page d'accueil

```

We can even use placeholders for dynamic content:

View:

```
{{ __trans('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:

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

[](#installation)

Require the translation package

```
composer require josephnc/translation

```

Add the service provider to your `config/app.php` config file

```
JosephNC\Translation\TranslationServiceProvider::class,
```

Add the facade to your aliases in your `config/app.php` config file

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

Publish the migrations

```
php artisan vendor:publish --provider="JosephNC\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)

```
__trans('Translate')
```

Or

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

This is typically most useful in blade views:

```
{{ __trans('Translate') }}
```

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

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

Or use placeholders:

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

In your `translations` database table you'll have something like this:

```
| id |     text     |                  data                |
  1     'Translate'    {"en":"Translate","fr":"Traduire"}

```

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

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

// Or use this, they both work
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.

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

```
__trans('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:

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

```
{{ __trans('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:

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

Seen:

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

```

Routes
------

[](#routes)

Translating your site with a locale prefix couldn't be easier. First inside your `app/Http/Kernel.php` file, insert the locale middleware:

```
/**
 * The application's route middleware.
 *
 * @var array
	*/
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

    // Insert Locale Middleware
    'locale' => \JosephNC\Translation\Middleware\LocaleMiddleware::class
];
```

Now, in your `app/Http/routes.php` file, insert the middleware and the following Translation method in the route group prefix like so:

```
Route::group([
    'prefix'        => Translation::getRoutePrefix(),
    'middleware'    => ['locale'],
], function () {

    Route::get('home', function () {
        return view('home');
    });
});
```

You should now be able to access routes such as:

```
http://localhost/home
http://localhost/en/home
http://localhost/fr/home

```

Automatic Translation
---------------------

[](#automatic-translation)

Automatic translation is enabled by default in the configuration file. It utilizes this fantastic packages
[Stichoza Google Translate PHP](https://github.com/Stichoza/google-translate-php).
[Viniciusgava Google Translate PHP](https://github.com/viniciusgava/google-translate-php-client).

Using automatic translation will send the inserted text to google and save the returned text to the database. Once a translation is saved in the database, it is never sent back to google to get re-translated. This means that you don't have to worry about hitting a cap that google may impose. You effectively **own** that translation.

Questions / Concerns
--------------------

[](#questions--concerns)

#### Why are there underscores where my placeholders should be in my database translations?

[](#why-are-there-underscores-where-my-placeholders-should-be-in-my-database-translations)

When you add placeholders to your translation, and add the data to replace it, for example:

```
__trans('Hi :name', ['name' => 'John'])
```

Translation parses each entry in the data array to see if the placeholder actually exists for the data inserted. For example, in the translation field in your database, here is what is saved:

```
__trans('Hi :name', ['name' => 'John']) // Hi __name__

__trans('Hi :name', ['test' => 'John']) // Hi :name
```

Since the placeholder data inserted doesn't match a placeholder inside the string, the text will be left as is. The reason for the underscores is because google translate will try to translate text containing `:name`, however providing double underscores on both sides of the placeholder, prevents google from translating that specific word, allowing us to translate everything else, but keep placeholders in tact. Translation then replaces the double underscore variant of the placeholder (in this case `__name__`) at runtime.

#### If I update / modify the text inside the translation function, what happens to it's translations?

[](#if-i-update--modify-the-text-inside-the-translation-function-what-happens-to-its-translations)

If you modify the text inside a translation function, it will create a new record and you will need to translate it again. This is intended because it could be a completely different translation after modification.

For example using:

```
{{ __trans('Welcome!') }}
```

And modifying it to:

```
{{ __trans('Welcome') }}
```

Would automatically generate a new translation record.

#### Is there a maximum amount of text that can be auto-translated?

[](#is-there-a-maximum-amount-of-text-that-can-be-auto-translated)

The package use the [Viniciusgava's](https://github.com/viniciusgava/google-translate-php-client) Google translate client then falls back to [Stichoza's](https://github.com/Stichoza/google-translate-php) if any error occurred.

However, [Stichoza's](https://github.com/Stichoza/google-translate-php) new 3.0 update allows you to translate up to 4200 words per request (tested, possibly more allowed).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

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

2469d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e901d5895ead89ad8db1663bb8339a15cd139c57e615304c11ea9e3d703ddb01?d=identicon)[JosephNC](/maintainers/JosephNC)

---

Top Contributors

[![JosephNC](https://avatars.githubusercontent.com/u/28754106?v=4)](https://github.com/JosephNC "JosephNC (7 commits)")

---

Tags

automatic-translationgoogle-translate-apilaravellaravel-5-packagephp-frameworkphp7translationlaravellocalizationinternationalizationtranslatetranslationtranslatorlanglocalize

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/josephnc-translation/health.svg)

```
[![Health](https://phpackages.com/badges/josephnc-translation/health.svg)](https://phpackages.com/packages/josephnc-translation)
```

###  Alternatives

[herkod/laravel-tr

Laravel İçin Türkçe Çeviri Dosyaları

628.1k](/packages/herkod-laravel-tr)[longman/laravel-multilang

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

5514.4k1](/packages/longman-laravel-multilang)[smousss/laravel-globalize

Make Laravel projects translatable in a matter of seconds!

2266.3k](/packages/smousss-laravel-globalize)[jrmajor/fluent

Fluent localization system for PHP

2716.9k5](/packages/jrmajor-fluent)[zachleigh/laravel-lang-bundler

Create Laravel translations bundles.

2512.5k](/packages/zachleigh-laravel-lang-bundler)

PHPackages © 2026

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