Getting started

Instantiate

$translator = new Translator();

Define root path for translations

$translator->setTranslationsPath('path/to/locales');

(optional) Define if filename has to be used has namespace

$translator->useFilenameAsNamespace(true);

(optional) Define namespaces priority

$translator->setNamespacePriority('firstNs','secondNs','otherNs');

Define primary and fallback language (name of the folders where library load files)

$translator->setLanguagePrimary($language);
$translator->setLanguageFallback($fallback);

Add additional language

$translator->addLanguage('it');

Usage examples

1
2
3
4
5
$result = $translator->_('definitionKey');
$result = $translator->_('definitionKey', ['param1' => 'test']);
$result = $translator->_('definitionKey', ['param1' => 'test'], 'context');
$result = $translator->_('definitionKey', ['param1' => 'test'], 'context', 'specificLanguage');
$result = $translator->_('namespace:definitionKey', ['param1' => 'test'], 'context', 'specificLanguage');

The TranslatorInterface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php

declare(strict_types=1);

namespace I18Next;

interface TranslatorInterface
{
    /**
     * Translates the given message.
     *
     * @param string      $key
     * @param array|null  $parameters Array of parameters used to translate message
     * @param string|null $context    The context of the request
     * @param string|null $locale     The locale or null to use the default
     *
     * @return string The translated string
     */
    public function _(string $key, ?array $parameters = null, ?string $context = null, ?string $locale = null): string;
}