Steve Bauman
Steve Bauman

Reputation: 8668

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

Upvotes: 16

Views: 14502

Answers (5)

Nik Chankov
Nik Chankov

Reputation: 6047

Although the author created his own package for translation, I would add another one: laravel-gettext

Coming from CakePHP world where the translation is handled with .po files in my laravel projects I would use the extension above, because:

  1. I've used to use the syntax, for me it's sort of natural to use it (I have code snippets for creating it)
  2. GNU Gettext is a translation standard and there are editors for translating the files (PoEdit)
  3. it's easier to maintain visioning and adding new labels into the file

The extension is not mine, and I haven't used it so far, but I will in a first occurrence.

Upvotes: 2

Potsky
Potsky

Reputation: 328

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 !

Upvotes: 10

Andreas
Andreas

Reputation: 8019

I don't think there's an ideal solution here, it depends on the size of the project. Perhaps just have different views for each language and then call View::make dynamically - something like this:

$view = 'pages.'.Lang::getLocale().'.mypage';
if (View::exists($view)) return View::make($page);
else App::abort(404);

Upvotes: 2

Savageman
Savageman

Reputation: 9477

Well,

Large websites probably use some sort of CMS to allow editing the texts for the different languages.

I'm not sure about Laravel in particular, but you want the user to be able to edit the texts. There's not so much solutions for that you will either need:

  1. an online interface in your website
  2. or a script to export/import the translations offline.

The most common format for this is .po files, some softwares even allow you to import / export files containing PHP array directly. This won't work with your format though, because your files are multi-dimensional array.

If you go this way, this PHP PoParser class which can both read and write .po files.

Upvotes: 1

user1130272
user1130272

Reputation:

You can always use a Package called Waavi translation

Keeping a project's translations properly updated is cumbersome. Usually translators do not have access to the codebase, and even when they do it's hard to keep track of which translations are missing for each language or when updates to the original text require that translations be revised.

This package allows developers to leverage their database and cache to manage multilanguage sites, while still working on language files during development and benefiting from all the features Laravel's Translation bundle has, like pluralization or replacement.

Upvotes: 2

Related Questions