zemistr/l10n

Localization, plurals and simple translator

v1.5.0 2015-04-18 19:18 UTC

This package is not auto-updated.

Last update: 2020-07-10 18:32:48 UTC


README

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage Total Downloads License

l10n

Localization, plurals and translator

About

l10n is a package combining many languages from ISO 639-1 (112/185), all Plurals from Mozilla Developer Network and simple translator.

Packagist

l10n is available on Packagist.org, just add the dependency to your composer.json.

{
  "require" : {
    "zemistr/l10n": "1.*"
  }
}

or run Composer command:

php composer.phar require zemistr/l10n

Usage without composer

<?php
require('src/l10n.php');

Basic translator usage with PluralRule

<?php
// first we create a new instance of plural rule
$plural = new \l10n\Plural\PluralRule1();

// create new instance of Translator with selected plural rule
$translator = new \l10n\Translator\Translator($plural);

// add translations
//   $translator->setText($key, $text, $plural = 0);
$translator->setText('statistics.users', '%n% person'); // 0 or nothing for singular
$translator->setText('statistics.users', '%n% people', 1);

// get translations
//   $translator->translate($key, $n = 1, array $parameters = array());
//
// or for singular
//   $translator->translate($key, array $parameters = array());
echo $translator->translate('statistics.users', 0) . '<br>'; // 0 people
echo $translator->translate('statistics.users', 1) . '<br>'; // 1 person
echo $translator->translate('statistics.users', 50) . '<br>'; // 50 people
echo $translator->translate('statistics.users', 100) . '<br>'; // 100 people

// we can use %variables% in translations
// in default is available variable %n% for number $n (singular/plural number)

$translator->setText('user', 'I am %firstname% %lastname%');

echo $translator->translate('user', ['%firstname%' => 'John', '%lastname%' => 'Doe']); // I am John Doe

Basic translator usage with Language

<?php
// first we create a new instance of class based on IPlural
$language = new \l10n\Language\EnglishLanguage();

// create new instance of Translator with selected language
$translator = new \l10n\Translator\Translator($language);

// add translations
//   $translator->setText($key, $text, $plural = 0);
$translator->setText('statistics.users', '%n% person'); // 0 or nothing for singular
$translator->setText('statistics.users', '%n% people', 1);

// get translations
//   $translator->translate($key, $n = 1, array $parameters = array());
//
// or for singular
//   $translator->translate($key, array $parameters = array());
echo $translator->translate('statistics.users', 0) . '<br>'; // 0 people
echo $translator->translate('statistics.users', 1) . '<br>'; // 1 person
echo $translator->translate('statistics.users', 50) . '<br>'; // 50 people
echo $translator->translate('statistics.users', 100) . '<br>'; // 100 people

// we can use %variables% in translations
// in default is available variable %n% for number $n (singular/plural number)

$translator->setText('user', 'I am %firstname% %lastname%');

echo $translator->translate('user', ['%firstname%' => 'John', '%lastname%' => 'Doe']); // I am John Doe

(c) Martin Zeman (Zemistr), 2015 (http://zemistr.eu)