unicate/translation

File based translation for use in Views.

v1 2020-05-29 14:45 UTC

This package is auto-updated.

Last update: 2024-11-10 02:04:37 UTC


README

Universal translation mechanism based on an associative array. Can be easily used directly in native PHP pages or integrated in templates systems like Plates.

Installation

Use composer:

composer require unicate/translation

Usage

<?php

require_once "vendor/autoload.php";
$data = [
    'en' => [
        'test.token1' => 'Token Text EN 1',
        'test.token2' => 'Token Text EN 2',
    ],
    'de' => [
        'test.token1' => 'Token Text DE 1',
        'test.token2' => 'Token Text DE 2',
    ],
];

$translation = new \Unicate\Logger\Translation($data);
$translation->setLang('en');
$text = $translation->translate('test.token1');
echo $text; // 'Token Text EN 1'

$translation = new \Unicate\Logger\Translation($data, 'de');
$text = $translation->translate('test.token1');
echo $text; // 'Token Text DE 1'

Translations in File...

// data.php
<?php
return [
   'en' => [
       'test.token1' => 'Token Text EN 1',
       'test.token2' => 'Token Text EN 2',
   ],
   'de' => [
       'test.token1' => 'Token Text DE 1',
       'test.token2' => 'Token Text DE 2',
   ],
];

Other PHP file...

$data = require 'data.php';
$translation = new \Unicate\Logger\Translation($data);
$translation->setLang('en');
$text = $translation->translate('test.token1');
echo $text; // 'Token Text EN 1'