loading...

Translations in PHP projects

ironangel2000 profile image ironAngel2000 ・2 min read

Sometimes it is necessary to translate masks, forms or other modules into other languages.

The classic way is creating a separate .php file for each language and define a constant for each term. The advantage of this variant is that constants are available in every class and in each namespace.

The disadvantage is, if a lot of constants have to be defined, the list becomes confusing and a translator does not necessarily get along with the constants.

A small project that is quick and easy to integrate can solve this problem.

"Sophokles Translation"

Sophokles Translation is a library that can be conveniently installed and used through Composer.

The installation works as follows:

1. composer require sophokles/translation

2. Run composer install

The new classes are now available through the Composer autoloader.

require_once '/vendor/autoload.php';

The classes themselves work in the namespace "\Sophokles\Translation\".

The classes works with separate JSON formated files, in which the termes are translated. Becaus JSON ist stored in UTF-8 codepage it can be used for all languages. Also translaters can read the files and make easy the transition with a normal text editor.

An example of such a file:

{
    "languages":{
        "source":"en",
        "target":"de"
    },
    "translations":[
        {
            "source":"save",
            "target":"speichern"
        },
        {
            "source":"cancel",
            "target":"abbrechen"
        },
        {
            "source":"delete",
            "target":"löschen"
        }, 
        {
            "source":"edit",
            "target":"bearbeiten"
        },
        {
            "source":"close",
            "target":"schließen"
        }
    ]
}

The "languages" section defines the source language as well as the “target” section the target language. The translations are entered in the "translations" section.

In PHP the JSON file can be conveniently integrated:

\Sophokles\Translation\translation::registerFile('/translations/lang.de.json');

Of course, several files can be included, so that you can manage the translations modular in the project.

The output of the translations is handled by the class \Sophokles\Translation\lang. First you have to define the target language (the default language is "en"):

\Sophokles\Translation\lang::setLanguage('de');

To output a term, use the call:

\Sophokles\Translation\lang::get([TERM]);

If the class finds the translation of the term, it is output, otherwise the term is printed in its source. In this way you do not have to ride with cryptic keys, you can work directly in the source language with the correct words. This makes the source code more readable for other programmers.

Sophocles Translation also includes a debugging mode. Simply define the constant "SOPHOKLES_DEBUG":

define ('SOPHOKLES_DEBUG', true);

If the constant has been set to "true", then the system outputs a tilde "~" after each term for which no translation is found.

The library is available for Composer at

https://packagist.org/packages/sophokles/translation

or for developers

https://github.com/ironAngel2000/sophokles-translation

Posted on by:

Discussion

pic
Editor guide
 

I found Symfony translation component as a mighty, robust tool for translations.

 

Sure, but if you just want to include a small library and do not need the full framework, then Sophokles / translate is very fast.

 

You can install it via composer as a stand alone component to any project.
Anyway i don’t mind using other libs if do the job properly.

Sophokles/translate ist a component of a CMS I actualy Develop. I think I will public in future some libraries of it