Structuring Website Translation Files

I encountered this problem several times when creating websites. As an example, I will explain the use of PHP and Laravel, but this problem is a common multi-level platform. This has already been discussed in several questions ( post1 , post2 , post3 , post4 and some others), but the messages really did not get a good answer.

Question: What is the best way to structure translated content within language files?

I am currently using Laravel (I am not mentioning the version because Laravel 4 and Laravel 5 have similar localization functionality, at least fairly similar to the purple of this topic).

Localization structures content according to language files (en, es, de, fr ...), inside which there can be several .php files containing a return statement that returns a multi-level structure of the dictionary.

/lang /en messages.php /es messages.php 

and the files contain something like this:

 <?php return [ 'example1' => 'example message for value exaple-key', 'example2' => [ 'sub-example' => 'example message for example1.sub.example', ], ]; 

and the call to this is done by doing something like this:

 //Laravel 5 trans('messages.example1'); //outputs 'example message for value exaple-key' trans('messages.example2.sub-example'); //outputs 'example message for example1.sub.example' //Laravel 4 Lang::get('messages.example1'); //outputs 'example message for value exaple-key' Lang::get('messages.example2.sub-example'); //outputs 'example message for example1.sub.example' 

Several grouping methods come to mind:

  • website content

    example: homepage.php, page1.php, page2.php...

  • by logical domain:

    example: auth.php, validation.php, pagination.php...

  • by html:

    example: buttons.php, popup_messages.php, form_data.php...

  • by live broadcast:

    example: simple_words.php, phrases.php... and contain content like 'password-to-short' => 'your password is to long'

  • Some hybrids / combinations mentioned above

They all have some obvious advantages and disadvantages, and I will not try to use int, but the 5th option is most likely the best solution, but there is still the problem of where to draw the line to get minimal duplication of phrases and content.

The problem with user rights is the solution to the problem of the first uppercase characters in some cases and lowercase in other cases, as well as punctuation characters at the ends.

I have made a reaserch regarding this issue, but there are no definitive recommendations and / or good examples available to study.

All opinions are welcome.

+7
php web laravel laravel-4 translation
source share
3 answers

I tend to group functionality in my Laravel applications into standalone components. For example, I recently recently worked on the email functionality for an application, so put the service provider's class, models, and service classes in a folder in the application / email .

With this in mind, I organize my translations in a similar way. Thus, although the lines were not translated in this project, if I were, I would have created the resources / assets / lang / en / email.php file and translated the lines for the email component there.

So, in another project, my directory structure might look like this:

  • /resources
    • /languages
      • / en
        • auth.php
        • email.php
        • events.php
        • news.php
        • pagination.php
        • passwords.php
        • validation.php

Hope this helps.

+1
source share

In my experience, there is no reason to have different groups besides trying to use your translations elsewhere. I usually place all of my project messages in a group called app , and for each of my shared libraries I use a separate group name (because I can use them in other projects). An example of a login error message on my website would be

 trans('app.username_and_password_do_not_match') 

and if it is in a third-party library named Auth will

 trans('auth.username_and_password_do_not_match') 

And don't forget to write the full message as your message key, rather than using short names (e.g. app.login.fail ). thus, you do not need to check the contents of the website for each translation.

I did not quite understand your last problem, so you can explain it a bit.

0
source share

I would go with option # 4, so you would have something like this:

 /lang/ /en messages.php words.php /fr message.php words.php /de messages.php words.php 

This does a few things:

  • He separates everything very clearly. You know what language to find. And you know that in the language-related file.
  • The above simplifies future maintenance as you may find material.
  • It provides you with language files that can be translated separately.
  • It puts all messages in one clearly defined place.

It should be noted that if your application received REALLY large and REALLY international, you can use ISO language codes instead. For example, European Portuguese (pt_PT) and Brazilian Portuguese are different, and with a global audience, you probably want to get around both.

0
source share

All Articles