Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring Website Translation files

I faced this problem several times while building websites. I will explain the using PHP and Laravel as an example but this problem is a common amoung multiple platforms. This was already addressed in a few questions (post1, post2,post3, post4 and some others) but the posts didn't really get a good answer.

The question is: What is the best way of structuring translated content inside of language files?

I'm currently using Laravel (I'm not mentioning the version because both Laravel 4 and Laravel 5 have similar localisation functionalities, at least similar enough for the purpouses of this topic).

The localisation structures the content accross language files (en, es,de, fr...) inside which there can be multiple .php files that contain a return statement that returns a multi-level dictionary structure.

/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 calling of 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'

A few methods of grouping come to mind:

  1. by website content

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

  2. by logical domain:

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

  3. by html:

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

  4. by straight traslation:

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

  5. Some hybrid/combination of the ones mentioned before

All of these have some obvious benefits and drawbacks and I won't try to go int that but the 5th option is most likely the best solution but there's still the problem of where to draw the line to get minimal duplication of phrases and content.

Annother problem is how to solve the problem of uppercase first characters in some cases and lowercase in other cases as well as punctuation characters at the ends.

I did reaserch regarding this problem but there are no definitive guidelines and/or good examples available to learn from.

All opinions are welcome.

like image 994
Traveller Avatar asked Nov 05 '15 16:11

Traveller


People also ask

What is the best way to translate website content?

If you'd like to translate an entire website at once, you can use Google Translate. Putting a URL in Google Translate to translate the entire site. Select the target language from above the text box on the right. Once you click on the link, you should see a fully translated version of the website.

What are the three 3 main types of translators?

Generally, there are three types of translator: compilers. interpreters. assemblers.


1 Answers

I tend to group functionality in my Laravel apps into self-contained ‘components’. For example, I’ve been working on email campaign functionality for an application recently so put the service provider class, models, service classes in a folder at app/Email.

Bearing this in mind, I organise my translations in a similar fashion. So even though on this project we’re not translating strings, if we were I would create a resources/assets/lang/en/email.php file, and put translated strings for the email component in there.

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

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

Hope this helps.

like image 130
Martin Bean Avatar answered Oct 08 '22 22:10

Martin Bean