2017 © Pedro PelĂĄez
 

library locale

A better way to handle multilanguage

image

diego1araujo/locale

A better way to handle multilanguage

  • Sunday, October 12, 2014
  • by diego1araujo
  • Repository
  • 1 Watchers
  • 8 Stars
  • 338 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

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., (*2)

Installation

Edit composer.json file to require this package., (*3)

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

Next, run the composer update command:, (*4)

composer update

Open up app/config/app.php, (*5)

Find the providers key and add a new item to the array, (*6)

'Diego1araujo\Locale\ServiceProvider',

Find the aliases key and add a new item to the array, (*7)

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

Finally, publish the config file, (*8)

php artisan config:publish diego1araujo/locale

Config

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

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

Of course, feel free to change both of them., (*11)

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

Data

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

Like this:, (*14)

English app/lang/en/message.php, (*15)

return array(

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

);

Portuguese app/lang/pt/message.php, (*16)

return array(

    'welcome' => 'Bem-vindo Ă  minha pĂĄgina',

);

Usage

Define the set method as a prefix value in route group, (*17)

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, (*18)

Lang::get('message.welcome')

Retrieving the current language, (*19)

Locale::get();

Building url's with the current language, (*20)

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., (*21)

English app/lang/en/message.php, (*22)

return array(

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

);

Portuguese app/lang/pt/message.php, (*23)

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

The Versions

12/10 2014

dev-master

9999999-dev

A better way to handle multilanguage

  Sources   Download

BSD-2-Clause

The Requires

 

by Diego Araujo

laravel language translation multilanguage