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

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

ixudra/translation
==================

Custom PHP translation library for the Laravel framework - developed by Ixudra

5.7.0(4y ago)22.1k23MITPHPPHP ^7.2|^8.0

Since Nov 25Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ixudra/translation)[ Packagist](https://packagist.org/packages/ixudra/translation)[ Docs](https://ixudra.be)[ RSS](/packages/ixudra-translation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (18)Used By (3)

ixudra/translation
==================

[](#ixudratranslation)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3b78078bba0ec877984fb5f6e880caaabdafcaa6e6804bd182830a317f459977/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6978756472612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ixudra/translation)![license](https://camo.githubusercontent.com/e7ace1deb8ad19fafe90698caa0276870964583a81e2099c48bd2fcbc5d7035d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6978756472612f7472616e736c6174696f6e2e737667)[![Total Downloads](https://camo.githubusercontent.com/9d38d94bfb0c5d6b43360e59cbd398311255d14c4285ea4b135c4121a79151a1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6978756472612f7472616e736c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ixudra/translation)

Custom PHP translation library for the Laravel framework - developed by [Ixudra](https://ixudra.be).

This package can be used by anyone at any given time, but keep in mind that it is optimized for my personal custom workflow. It may not suit your project perfectly and modifications may be in order.

> Note before posting an issue: When posting an issue for the package, always be sure to provide as much information regarding the problem as possible.

Why use this package?
---------------------

[](#why-use-this-package)

The Laravel internationalization system is an amazing feature that makes translating messages in different languages incredibly easy. Unfortunately, I found that it has one big downside: since a lot of error messages (particularly with regards to model validation) are very similar, it encourages duplication in language files. This is particularly frustrating when adding a new language to your application. This package attempts to address that issue by applying a well-known programming construct: recursion.

This package provides functionality to include placeholder values in your translation messages that will be identified, translated and replaced at runtime. This makes the Laravel translation system more dynamic and reduces overall duplication in language files.

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

[](#installation)

Pull this package in through Composer.

```
    {
        "require": {
            "ixudra/translation": "5.*"
        }
    }
```

or run in terminal: `composer require ixudra/translation`

> If you want to use this package with Laravel 4, change the above line to `"ixudra/translation": "1.*"` instead.

### Laravel 5.5+ Integration

[](#laravel-55-integration)

Laravel's package discovery will take care of integration for you.

### Laravel 5.\* Integration

[](#laravel-5-integration)

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

```
    'providers'       => array(

        //...
        Ixudra\Translation\TranslationServiceProvider::class,

    ),
```

Add the facade to your app.php file

```
    'facades'       => array(

        //...
        'Translate'       => Ixudra\Translation\Facades\Translation::class,

    ),
```

### Laravel 4.\* Integration

[](#laravel-4-integration)

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

```
    'providers'     => array(

        //...
        'Ixudra\Translation\TranslationServiceProvider',

    ),
```

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

```
    'facades'       => array(

        //...
        'Translation'          => 'Ixudra\Translation\Facades\Translation',

    ),
```

Usage
-----

[](#usage)

Once included, the package can easily be used in any Laravel application. In order to translate a message, simply use the facade and pass in the key to the translation value which is stored in your language files:

```
    // Translate the message in the app default locale
    Translate::message('your.key.goes.here');

    // Translate the message in a given locale
    Translate::message('your.key.goes.here', $locale);
```

The package will automatically scan language files both your application and the package itself to find a match for this key. It is important to know that the package will give priority to application keys to allow the user to specify custom message instead of those provided by the package. If no value can be found for the provided key, the package will simply return the key itself, just as the Laravel `Lang` facade would.

### Translating models

[](#translating-models)

Since translation messages are commonly used for models, this package provides a specific method to make this even more easier:

```
    // Translate the message in the app default locale
    Translate::model('user.create.success');

    // Translate the message in a given locale
    Translate::model('user.create.success', $locale);
```

When this method gets called, the package will:

- look for the translation for the `user` model in the `lang/models.php` file
- look for the translation for the `create.success` key in the `lang/model.php` file
- translate the `create.success` message with the translated `user` model value as parameter.

The result of this interaction is (by default): `The user has been created successfully.`

### Recursive translations

[](#recursive-translations)

Recursive translations work similar to the other methods mentioned above. Upon finding the translation key, the package will then scan the value for recursion identifiers that need to be translated. These are highlighted by adding `##` to the front and back of the translation key (e.g. `##common.submit##`).

For example, see the following values in the `lang/admin.php` file that you can find in the package:

```
