Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Localization for large websites

Is there possibly a better way to store large amounts of text for localization in laravel? It would be easy if my entire page was just pure text, but several of my pages have complex layouts and I need to add multiple strings to wrap the text around content such as images/links/media.

This is a pain if I ever need to italicize/bold or do any sort of HTML for the text as well as I need to break them into sections to be able to do that.

An example of what I'm using:

return array(
    'exchange_rate' => array(
        'title' => 'Exchange Rate',
        'p1' => 'Disclaimer', 
        'p2' => 'Currency rate displayed is subject to change.',
        'p3' => 'View All Rates to Date'
    ),

The first array is the page, the second is the content of the page. I often have to go multiple arrays deeper for more complex layouts such as:

return array(
'exchange_rate' => array(
    'title' => 'Exchange Rate',
    'p1' => 'Disclaimer', 
    'p2' => 'Currency rate displayed is subject to change.',
    'p3' => 'View All Rates to Date',
    'table1' => array(
        'title' => 'Currency Table',
        'row1' => array(
           'l1' => 'Current Rate'
        ),
        'row2' => array('etc')
    )
);

Am I doing this right? Is there a better way to format my language files so I can work around the layouts in my views? I'm just curious how large websites manage localization.

Any help is greatly appreciated, thanks!!

EDIT: I'm also aware that you can add placeholders inside your localization arrays such as:

'title' => ':title'

But adding place-holders for all of my links, images, and media on one page could get messy. Laravel also doesn't support HTML inside the language arrays so I can't just plop in content inside the language files. - Yes it does

As it stands now there seems to be two different ways to go here.

  1. Continue breaking them into small sections to format text differently.
  2. Paste the page text into one 'content' array per page, use Waavi to transfer them to the database, and then format them correctly using a WYSIWYG editor on the website itself to format the database entry. (Though this has issues as well because then you can't use blade templating as Lang::get() returns only safe text)

EDIT (Feb 10th 2015):

After lots searching, I've created a package as well that suits my needs. It completely removes the need for any text arrays in laravel. It will automatically add text to the database and translate it to your set locale. Plus, you don't need to manage and decipher dot-notated translation paths.

https://github.com/stevebauman/translation

like image 654
Steve Bauman Avatar asked Jan 24 '14 21:01

Steve Bauman


People also ask

What is Laravel localization?

You will learn how to work with translation files, perform pluralization, create a language switcher, and more with laravel localization and multi-language examples. Localization is the second phase of the Laravel internationalization (i18n) process. In Laravel i18n, an application is designed to fit various languages and cultures.

How to create a multi-language website in Laravel?

Creating a multi-language website requires two steps. Firstly, you need to detect user local language setting and change it bu user choice. Secondly, you need to translate messages and strings into user local language, in which we use Laravel localization.

How to detect user language setting in Laravel?

Secondly, you need to translate messages and strings into user local language, in which we use Laravel localization. In order to detect user language setting, we need to create a language middleware. this middleware checks for locale setting in the user session.

How do I change the Welcome to our website in Laravel?

All you need to do is replace the “Welcome to our website” text with the following code: { { __ ('Welcome to our website') }}. This will instruct Laravel to display “Welcome to our website” by default and look for translations of this string if a non-English language is set (we’ll get to this later on).


1 Answers

I have written a package to generate lang files for you. Write your templates, controllers, ... without boring with translations. When you have finished to code, just execute php artisan localisation:missing and it will generate all needed files in your lang/*/ directories.

It will synchronize translations :

  • keep the currently used translatations
  • create new translatations
  • remove or move on bottom obsolete translations

The package is available here : https://github.com/potsky/laravel-localization-helpers

In daily usage, it is the same workflow as po files. Let me know if your life is easier now with translations in Laravel awesome framework !

like image 71
Potsky Avatar answered Oct 02 '22 09:10

Potsky