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

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

alaaeta/translation
===================

Library for translate keys

v1.4.6(2y ago)0974MITPHP

Since Jul 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Alaa-eta/translation)[ Packagist](https://packagist.org/packages/alaaeta/translation)[ RSS](/packages/alaaeta-translation/feed)WikiDiscussions main Synced 1mo ago

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

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

[](#translation)

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

[](#description)

Translation is a developer friendly, database driven, automatic translator for Laravel 9. 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:

The recommended way to install through Composer:

```
composer require alaaeta/translation

```

Controller:

```
public function index()
{
    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 Arabic (ar), it'll automatically translate it for you.

Controller:

```
public function index()
{
    app()->setLocale('ar');

    return view('home.index');
}

```

View:

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

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

```

Seen:

```
Welcome to our home page

```

We can even use placeholders for dynamic content:

View:

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

```

Seen:

```
Welcome John, to our home page

```

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

[](#installation)

Publish the config

```
php artisan vendor:publish --tag=translation-config

```

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 `translations` database table you'll have:

```
| id | key | value | language_code |
  1        'Translate me!'         'Translate me!'        'en'

```

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

```
app()->setLocale('en') // Setting to Englisg locale

```

Locales are automatically created when you call the ` app()->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.

this in your `translations` table:

```
| id | key | value | language_code |
  1        'Translate me!'         'Translate me!'        'en'
  1        'Translate me!'         'Translate me!'        'ar'

```

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 english!', [], 'en') }}

{{ _t('And arabic!', [], 'ar') }}

```

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'], 'ar') }}

```

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:

```
use Alaaeta\Translation\Contracts\Translation;

class BlogController extends Controller
{
    /**
     * @var Translation
     */
    protected $translation;

    /**
     * Constructor.
     *
     * @param Translation $translation
     */
    public function __construct(Translation $translation)
    {
        $this->translation = $translation;
    }

    /**
     * Returns all blog entries.
     *
     * @return Illuminate\View\View
     */
    public function index()
    {
        $title = $this->translation->translate('My Blog');

        $entries = Blog::all();

        return view('pages.blog.index', compact('title', 'entries'));
    }
}
```

Models
------

[](#models)

By default, translation model are included and selected inside the configuration file. If you'd like to use your own models you must create them and implement their trait. Here's an example:

The Translation Model:

```
use Stevebauman\Translation\Traits\TranslationTrait;
use Illuminate\Database\Eloquent\Model;

class Translation extends Model
{
    use TranslationTrait;

    /**
     * The locale translations table.
     *
     * @var string
     */
    protected $table = 'translations';

    /**
     * The fillable locale translation attributes.
     *
     * @var array
     */
    protected $fillable = [
        'key',
        'value',
        'language_code',
    ];

}

```

Once you've created translation model, insert them into the `translation.php` configuration file:

```
|--------------------------------------------------------------------------
| Translation Model
|--------------------------------------------------------------------------
|
|  The translation model is used for storing translations.
|
*/

'translation' => App\Models\Translation::class,

```

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' => \Stevebauman\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

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Every ~25 days

Recently: every ~0 days

Total

17

Last Release

988d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c001a8acff928119b05a860baa4b1b7a0cf656183d8f93e07416566bee71c97b?d=identicon)[Alaa-eta](/maintainers/Alaa-eta)

---

Top Contributors

[![Alaa-eta](https://avatars.githubusercontent.com/u/13734668?v=4)](https://github.com/Alaa-eta "Alaa-eta (15 commits)")

### Embed Badge

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

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

###  Alternatives

[symfony/translation

Provides tools to internationalize your application

6.6k836.5M2.1k](/packages/symfony-translation)[nesbot/carbon

An API extension for DateTime that supports 281 different languages.

169661.4M4.8k](/packages/nesbot-carbon)[joedixon/laravel-translation

A tool for managing all of your Laravel translations

717911.4k11](/packages/joedixon-laravel-translation)[illuminate/translation

The Illuminate Translation package.

6936.4M495](/packages/illuminate-translation)[lajax/yii2-translate-manager

Translation management extension for Yii 2

227578.8k13](/packages/lajax-yii2-translate-manager)[larswiegers/laravel-translations-checker

Make sure your laravel translations are checked and are included in all languages.

256423.2k2](/packages/larswiegers-laravel-translations-checker)

PHPackages © 2026

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