A better way to handle multilanguage
364
8
2
Package Data
Maintainer Username: diego1araujo
Maintainer Contact: diego77araujo@gmail.com (Diego Araujo)
Package Create Date: 2013-10-11
Package Last Update: 2014-10-12
Home Page:
Language: PHP
License: BSD-2-Clause
Last Refreshed: 2024-12-24 03:03:52
Package Statistics
Total Downloads: 364
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 8
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Build Status

Locale - A better way to handle multilanguage.

The goal of this package is offer a simple way to make a multilanguage for Laravel 4/5.

Installation

Edit composer.json file to require this package.

"require": {
	...
	"diego1araujo/locale": "dev-master"
}

Next, run the composer update command:

composer update

Open up app/config/app.php

Find the providers key and add a new item to the array

'Diego1araujo\Locale\ServiceProvider',

Find the aliases key and add a new item to the array

'Locale' => 'Diego1araujo\Locale\Facade',

Finally, publish the config file

php artisan config:publish diego1araujo/locale

Config

To see which languages are available, open up app/config/packages/diego1araujo/locale/config.php

By default, the main language is en (english) and the available item contains the list of languages.

Of course, feel free to change both of them.

NOTE: The main language isn't appended in url, only others (from available item).

Data

Go to app/lang/[language] and create a new file(s) for each language (Files must have the same name for all languages)

Like this:

English app/lang/en/message.php

return array(

	'welcome' => 'Welcome to my page',

);

Portuguese app/lang/pt/message.php

return array(

	'welcome' => 'Bem-vindo à minha página',

);

Usage

Define the set method as a prefix value in route group

Route::group(array('prefix' => Locale::set()), function()
{
	Route::get('/', function()
	{
		return View::make('home');
	});

	Route::get('example', function()
	{
		return View::make('example');
	});
});

Printing a message

Lang::get('message.welcome')

Retrieving the current language

Locale::get();

Building url's with the current language

Locale::url('about'); // [site-domain]/[current-language]/about

One route for many languages

You can use a single route for a multiple language page. I suggest you to place all pages in your file translation.

English app/lang/en/message.php

return array(

	'menu-page' => 'Page',
	'slug-page' => 'page',

);

Portuguese app/lang/pt/message.php

return array(

	'menu-page' => 'Página',
	'slug-page' => 'pagina',

);
Route::group(array('prefix' => Locale::set()), function()
{
	Route::get(Lang::get('message.slug-page'), ['as' => 'page', 'uses' => 'HomeController@page']);
}