Skip to content

marttosc/laravel-multilang

 
 

Repository files navigation

Laravel 5.x MultiLanguage

Build Status Code Coverage Code Quality Latest Stable Version Total Downloads Downloads Month License

This is a very useful package to integrate multi language (multi locale) functionality in Laravel 5.x. It includes a ServiceProvider to register the multilang and Middleware for automatic modification routes like http://site.com/en/your-routes.

This package uses database for storing translations (it caches data on production environment for improving performance) Also package automatically adds in database missing keys (on the local environment only).

Table of Contents

Installation

Install this package through Composer.

Edit your project's composer.json file to require longman/laravel-multilang

Create composer.json file:

{
    "name": "yourproject/yourproject",
    "type": "project",
    "require": {
        "longman/laravel-multilang": "~1.0.0"
    }
}

And run composer update

Or run a command in your command line:

composer require longman/laravel-multilang

After updating composer, add the MultiLangServiceProvider to the providers array in config/app.php

Longman\LaravelMultiLang\MultiLangServiceProvider::class,

And add facade to the alias array in config/app.php

'MultiLang' => Longman\LaravelMultiLang\Facades\MultiLang::class,

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider="Longman\LaravelMultiLang\MultiLangServiceProvider"

After run multilang migration command

php artisan multilang:migration

Its creates multilang migration file in your database/migrations folder. After you can run

php artisan migrate

Also if you want automatically change locale depending on url (like http://site.com/en/your-routes) you must add middleware in app/Http/Kernel.php

I suggest add multilang after CheckForMaintenanceMode middleware

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Longman\LaravelMultiLang\Middleware\MultiLang::class,
];

In your RoutesServiceProvider modify that:

MultiLang::routeGroup(function($router) {
    require app_path('Http/routes.php');
});

or directly in app/Http/routes.php file add multilang group:

MultiLang::routeGroup(function($router) {
    // your routes and route groups here
});

If you want managing texts, add in routes file:

MultiLang::manageTextsRoutes();

Or if you want only translating strings without modification urls and routes, you must manually set locale in your app like:

App::setLocale('en');

Usage

In application you can use t() helper function like:

$string = t('Your translatable string');

You can use markers for dynamic texts and pass any data like

$string = t('The :attribute must be a date after :date.', ['attribute' => 'Start Date', 'date' => '7 April 1986']);

which will be return The Start Date must be a date after 7 April 1986.

In blade templates you can use just @t() notation like

@t('Your translatable string')

which is equivalent to {{ t('Your translatable string') }}

Also you can use lang_url() helper function for appending current lang marker in urls automatically.

$url = lang_url('users'); // which returns /en/users depending on your language (locale)

Note: Texts will be selected after firing Laravel's RouteMatched event. Therefore texts unavailable on artisan commands

TODO

write more tests

Troubleshooting

If you like living on the edge, please report any bugs you find on the laravel-multilang issues page.

Contributing

Pull requests are welcome. See CONTRIBUTING.md for information.

License

Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.

Credits

Full credit list in CREDITS

About

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

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • PHP 92.9%
  • HTML 6.0%
  • Shell 1.1%