Package Installation

Laravel I18n package can be installed via Composer, with this command:

composer require richan-fongdasen/laravel-i18n

Preparing the database

If you're planning to store the supported languages in a database table, you need to publish the migration first:

php artisan vendor:publish --provider="RichanFongdasen\I18n\ServiceProvider" --tag="migrations"

After that, you need to run the published migrations.

php artisan migrate

Publishing the languages.json

As an alternative to storing the supported languages in a database table, you can also store the supported languages in a JSON file. First, you need to publish the JSON example with this command:

php artisan vendor:publish --provider="RichanFongdasen\I18n\ServiceProvider" --tag="languages.json"

Then, you can manage the languages data in storage/i18n/languages.json

Publishing the config file

It is not required to publish the config file. But it is necessary if you intend to override the default configuration.

php artisan vendor:publish --provider="RichanFongdasen\I18n\ServiceProvider" --tag="config"

This is the default content of the config file:

config/i18n.php
return [

    /*
    |--------------------------------------------------------------------------
    | Language repository driver
    |--------------------------------------------------------------------------
    |
    | Define your language repository driver here. The available options are:
    | database and json.
    |
    */

    'driver' => 'json',

    /*
    |--------------------------------------------------------------------------
    | Language repository datasource
    |--------------------------------------------------------------------------
    |
    | Define your language repository datasource here.
    |   - Define your database table name, when you're using database driver.
    |   - Define your json file path, when you're using json driver.
    |
    */

    'language_datasource' => storage_path('i18n/languages.json'),
//    'language_datasource' => 'languages',

    /*
    |--------------------------------------------------------------------------
    | Language model
    |--------------------------------------------------------------------------
    |
    | Define the model class name to manage the language resources.
    |
    */

    'language_model' => \RichanFongdasen\I18n\Eloquent\Models\Language::class,

    /*
    |--------------------------------------------------------------------------
    | Cache duration
    |--------------------------------------------------------------------------
    |
    | Define how long we should cache the language dataset in seconds.
    |
    */

    'cache_duration' => 60 * 60 * 24,

    /*
    |--------------------------------------------------------------------------
    | Language key
    |--------------------------------------------------------------------------
    |
    | Define which language key in datasource that we should use.
    | Available options are:
    |   - language, ie: en, es, de, etc.
    |   - ietfCode, ie: en-US, en-UK, de-DE, etc.
    |
    */

    'language_key' => 'language',

    /*
    |--------------------------------------------------------------------------
    | API query key
    |--------------------------------------------------------------------------
    |
    | Define the query parameter name which will be used as language selector
    | in every API request.
    | e.g: http://localhost:8000/api/articles?lang=en
    |
    */

    'api_query_key' => 'lang',

    /*
    |--------------------------------------------------------------------------
    | Language negotiator class
    |--------------------------------------------------------------------------
    |
    | Define your language negotiator class here.
    | The class should implement LanguageNegotiator contract / interface.
    |
    */

    'negotiator' => \RichanFongdasen\I18n\Negotiators\BrowserNegotiator::class,

    /*
    |--------------------------------------------------------------------------
    | Locale URL segment number
    |--------------------------------------------------------------------------
    |
    | Define which url segment number that will be used to put the current
    | locale information. URL segment is started with '1'.
    | e.g: http://my-application.app/en/home
    |
    */
   
    'locale_url_segment' => 1,

    /*
    |--------------------------------------------------------------------------
    | Translation table suffix
    |--------------------------------------------------------------------------
    |
    | Define your preferred suffix to be appended to your database's
    | translation table name.
    |
    */

    'translation_table_suffix' => 'translations',

    /*
    |--------------------------------------------------------------------------
    | Translation model class name
    |--------------------------------------------------------------------------
    |
    | Define the model class name to store the translation values.
    |
    */

    'translation_model' => \RichanFongdasen\I18n\Eloquent\Models\Translation::class,

    /*
    |--------------------------------------------------------------------------
    | Enable Store to the cache
    |--------------------------------------------------------------------------
    |
    | Toggle store locale to the cache
    |
    */

    'enable_cache' => env('I18N_ENABLE_CACHE', true),

];
Edit this page on GitHub Updated at Wed, May 25, 2022