Translations in PHP projects
Tags: tanslations, php, compser, json
Published: 2019-07-25 12:16:37


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:

```console
1. composer require sophokles/translation

2. Run composer install

```
The new classes are now available through the Composer autoloader.

```console
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:
```json
{
    "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:
```php
\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"):
```php
\Sophokles\Translation\lang::setLanguage('de');
```
To output a term, use the call:
```php
\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":
```php
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