Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
152 lines (130 sloc) 4.15 KB


The translation provides i18n with user-extensible translation formatting.


composer require webiik/translation


$translation = new \Webiik\Translation\Translation($arr);

$translation->add('greeting', 'Hello {name}!');
echo $translation->get('greeting', ['name' => 'Kitty']);

$translation->add('greeting', 'Ahoj {name}!');
echo $translation->get('greeting', ['name' => 'Kitty']);



setLang(string $lang): void

setLang() sets current lang of translation. This lang is used for setting and getting values to/from Translation class.




add(string $key, string $val): void

add() adds translation by key. Read more about supported translation formats.

$translation->add('greeting', 'Hello {name}!');


addArr(array $translation, &$context = false): void

addArr() adds translations from array.

Note about resolving the key conflicts:

Arrays values - New value that is an array is merged with old value that is an array. If array key is a string, value of the new key replaces value of the old key.

Mixed values - New value that is a different type than old value, replaces old value. e.g. New string value replaces old array value and vice-versa.

$translation->addArr(['greeting' => 'Hello {name}!']);



get(string $key, $context = null)

get() gets translation by key. Key supports dot notation. If key is missing it returns empty string. After calling, all missing keys and contexts can be obtained with method getMissing().

$translation->get('greeting', ['name' => 'Kitty']);


getAll($context = null): array

getAll() gets all translations. After calling, all missing contexts can be obtained with method getMissing().

$translation->getAll(['name' => 'Kitty']);


getMissing(): array

getMissing() returns array of all missing keys and contexts from callings of methods get() and getAll().

$missing = $translation->$arr->getMissing();

Translation Formatting

Translations can contain special formatting which help to update translation values on the fly.

Basic Syntax


You can add any variable to translation with folded brackets.

$translation->add('greeting', 'Hello {name}!');
echo $translation->get('greeting', ['name' => 'Kitty']);
// Hello Kitty!

Plural Syntax

{variableName, Plural, =int {message}...}

Sometimes a translation depends on some specific count. Int represents that count, allowed values are: -int, int-int, int+

$translation->add('playful-cats', '{numCats, Plural, =0 {No cat wants} =1 {One cat wants} =2-10 {{numCats} cats want} =11+ {A lot of cats want}} to play.');
echo $translation->get('playful-cats', ['numCats' => 2]);
// 2 cats want to play.

Select Syntax

{variableName, Select, =string {message}...}

Sometimes a translation depends on some specific value. In the select syntax, string represents that value.

$translation->add('cat-gender', '{gender, Select, =tomcat {He is {gender}} =cat {She is {gender}}}.');
echo $translation->get('cat-gender', ['gender' => 'tomcat']);
// He is tomcat.

Custom Formatter Syntax

{variableName, FormatterClassName, formatter syntax}

You can write your own formatter. Every custom formatter must:

  • be compatible with the syntax above
  • implement Webiik\Translation\Parser\ParserInterface.php
  • use namespace Webiik\Translation\Parser

Look at Select formatter to get better insight.


You can’t perform that action at this time.