Rohan
Rohan

Reputation: 13771

Laravel application language change?

I have recently developed and application in Laravel for a German client. Now the client wishes to know if the application can viewed in German instead of English. I have designed all the views of the front end using Blade in English of course. Now is there a way in which the views translate to the desired language? Is there a package or another way to accomplish this?

Upvotes: 3

Views: 12252

Answers (2)

Eduardo Russo
Eduardo Russo

Reputation: 4219

I'm using this composer package: https://github.com/mcamara/laravel-localization

But… it's not as simple as it was in Laravel 3.

You need to manually check user's lang and change the used language.

First, set the default locale in config/app.php:

'locale'        => 'en',

Then, create a new array of used languages in the same file (config/app.php):

'languages'     => array(
    'en' => 'en_US',
    'pt' => 'pt_BR',
    'pl' => 'pl_PL',
    'es' => 'es_LA',
    'ru' => 'ru_RU',
    'de' => 'de_DE',
    'nl' => 'nl_NL',
    'fi' => 'fi_FI',
    'it' => 'it_IT',
    'fr' => 'fr_FR',
),

In the route file, you'll verify the user language in a route group:

Route::group(
    array('prefix' => LaravelLocalization::setLocale(), //Set the language using the package
        'before' => 'LaravelLocalizationRedirectFilter', //Change the URL to match the language
    ),
    function () {
        Route::controller('upload', 'ImageUploadController');
        Route::controller('faviconit', 'CreateController');
        Route::controller('download/{folder?}', 'DownloadController');
        Route::controller('/', 'HomeController');
}
);

And I have a helper class called LocalizationHelper.php with this code:

<?php
class LocalizationHelper
{
    /**
     * To use localization in an easier way, it's set as 'pt' instead of 'pt_BR'. To get the correct Brazilian
     * localization in aviary, this function return 'pt_BR' when locale is set to 'pt'
     *
     * @return string The correct Aviary locale string
     */

    public static function getUserLanguageFromSupportedLanguages()
    {
        $userLanguages      = Request::getLanguages();
        $supportedLanguages = Config::get('app.languages');
        $userLanguage       = Config::get('app.locale');
        foreach ($userLanguages as $language) {
            $language = substr($language, 0, 2);

            if (false !== array_search($language, $supportedLanguages, true)) {
                $userLanguage = $language;
                break;
            }
        }

        return $userLanguage;
    }

    /**
     * Returns html with language selector. This code removes languages without URL.
     * @return \Illuminate\View\View
     */
    public static function getLanguageSelector()
    {
        //START - Delete in v1.0
        $languages = LaravelLocalization::getSupportedLocales();
        $active    = LaravelLocalization::getCurrentLocale();

        foreach ($languages as $localeCode => $language) {
            $langUrl = LaravelLocalization::getLocalizedURL($localeCode);

            // check if the url is set for the language
            if ($langUrl) {
                $language['url'] = $langUrl;
            } else {
                // the url is not set for the language (check lang/$lang/routes.php)
                unset($languages[$localeCode]);
            }
            // fill the active language with it's data
            if ($active == $localeCode)
                $native = $language['native'];
        }

        return View::make('templates.languagebar', compact('languages', 'active', 'native'));
    }
}

The selector setted on the code above is a blade file called in the header view:

        <li class="dropdown">
            {{ LocalizationHelper::getLanguageSelector() }}
        </li>

Then, you have to set the config file in config/packages/mcamara/laravel-localization/config.php.

Just copy the languages you'll use to the uncommented array.

There's a thing that sucks… you have to add the used language in some links like this:

{{ Form::open(array('url' => LaravelLocalization::getCurrentLocale() . '/faviconit', 'files' => true, 'class' => 'form-horizontal')) }}

Last, all you have to do is add the code to translate the texts:

<span class="help-block">
    {{ trans('faviconit.formFileHelp') }}</strong>
</span>

The language are located in the lang folder, with the country code.

Hope it helps you :)

Upvotes: 2

Joe
Joe

Reputation: 4728

Laravel provides localisation functionality baked in http://laravel.com/docs/4.2/localization though you are going to need to re-do your views to use the lang strings and add language detection to your controllers.

I would avoid "on-the-fly" translations as they are rarely reliable.

Upvotes: 4

Related Questions