Agile Toolkit Translation library¶
Based on I18Next library¶
Based on i18next http://i18next.com
Interpolate direct with ATK4 - Agile Toolkit Models https://www.agiletoolkit.org/
install with composer :
`
composer require abbadon1334/atk4-i18next-php
`
All documentation about the original library can be found here : http://i18next.com/
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;
}
|
I18Next¶
\I18Next
Namespaces¶
Exception¶
\I18Next\Exception
Classes¶
-
class
I18Next\Exception\
TranslationSyntaxError
¶ Source: src/I18Next/Exception/TranslationSyntaxError.php#9 Parent: atk4\core\Exception
-
class
I18Next\Exception\
LanguageNamespaceMisuse
¶ Source: src/I18Next/Exception/LanguageNamespaceMisuse.php#7 Parent: I18Next\Exception\Misusage
-
class
I18Next\Exception\
LanguageAlreadyPresent
¶ Source: src/I18Next/Exception/LanguageAlreadyPresent.php#9 Parent: atk4\core\Exception
-
class
I18Next\Exception\
LanguageLocaleNotExists
¶ Source: src/I18Next/Exception/LanguageLocaleNotExists.php#9 Parent: atk4\core\Exception
-
class
I18Next\Exception\
Misusage
¶ Source: src/I18Next/Exception/Misusage.php#9 Parent: atk4\core\Exception
-
class
I18Next\Exception\
LanguagePathNotDefined
¶ Source: src/I18Next/Exception/LanguagePathNotDefined.php#9 Parent: atk4\core\Exception
-
class
I18Next\Exception\
NotFoundKey
¶ Source: src/I18Next/Exception/NotFoundKey.php#10 Parent: atk4\core\Exception
Implements: Throwable
Locale¶
\I18Next\Locale
Namespaces¶
\I18Next\Locale\Processor
-
class
I18Next\Locale\Processor\
Key
¶ Source: src/I18Next/Locale/Processor/Key.php#10 Parent: I18Next\Locale\Processor\AbstractProcessor
public processKey($key, $context, $counter)
private processWithCounter($key, $counter)
private process($key)
private processWithNamespaceWithCounter($key, $counter)
private processWithNamespace($key)
private getKeyNamespace($key)
private getOnlyKeyFromNamespacedKey($key, $namespace)
-
class
I18Next\Locale\Processor\
Value
¶ Source: src/I18Next/Locale/Processor/Value.php#12 Parent: I18Next\Locale\Processor\AbstractProcessor
public processValue($found_key, $parameters)
private processValueInterpolate($found_key, $parameters)
private processValueReplaceParameter($found_key, $index, $parameters)
private processValueNested($found_key, $parameters)
-
class
I18Next\Locale\Processor\
AbstractProcessor
¶ Source: src/I18Next/Locale/Processor/AbstractProcessor.php#12
-
protected static property
I18Next\Locale\Processor\AbstractProcessor::$
translations
¶ Source: src/I18Next/Locale/Processor/AbstractProcessor.php#15 Type: \I18Next\Locale\Translations
-
public
I18Next\Locale\Processor\AbstractProcessor::
__construct
($translations)¶ - AbstractProcessor constructor.
Source: Parameters: - $translations (
I18Next\Locale\Translations
)
- $translations (
Classes¶
-
class
I18Next\Locale\
Processor
¶ Source: src/I18Next/Locale/Processor.php#13
public __construct($translations)
public process($key, $parameters, $context)
private getKeyNamespace($key)
private getCountFromParameters($parameters)
private getContextFromParameters($parameters, $context)
private getNamespaceFromParameters($parameters)
private processKeyForNamespaces($key, $keyNamespaceFromParameters)
-
public
I18Next\Locale\Processor::
__construct
($translations)¶ - Processor constructor.
Source: Parameters: - $translations (
I18Next\Locale\Translations
)
- $translations (
-
public
I18Next\Locale\Processor::
process
($key, $parameters=null, $context=null)¶ Source: Parameters: - $key (string)
- $parameters (array | null)
- $context (string | null)
Returns: string | null
-
class
I18Next\Locale\
Translations
¶ Source: src/I18Next/Locale/Translations.php#19 Used traits: atk4\core\ConfigTrait
public load($path, $use_filename_as_namespace, $namespace_priority)
private loadSingle($path)
public useFilenameAsNamespace($enabled)
private setNamespacePriority($namespace_priority)
private processKeyVariation($processorClass, $key, $value)
private afterReadProcessForKeyCounters()
private afterReadProcessForKeyDeepInline()
private afterReadAddNamespaceIfNeeded($config, $namespace)
public useNamespaces()
public getNamespaceRanked()
public setLoaderFormat($format)
-
protected static property
I18Next\Locale\Translations::$
namespace_priority
¶ Source: src/I18Next/Locale/Translations.php#27 Type: string[]
-
protected static property
I18Next\Locale\Translations::$
use_filename_as_namespace
¶ Source: src/I18Next/Locale/Translations.php#30 Type: bool
-
public
I18Next\Locale\Translations::
load
($path, $use_filename_as_namespace, ...$namespace_priority)¶ Source: Parameters: - $path (string)
- $use_filename_as_namespace (bool)
- …$namespace_priority (string | null)
Throws:
-
public
I18Next\Locale\Translations::
useFilenameAsNamespace
($enabled)¶ Source: Parameters: - $enabled (bool | null)
-
public
I18Next\Locale\Translations::
useNamespaces
()¶ Source: src/I18Next/Locale/Translations.php#197 Returns: bool
-
public
I18Next\Locale\Translations::
getNamespaceRanked
()¶ Source: src/I18Next/Locale/Translations.php#205 Returns: string[]
-
public
I18Next\Locale\Translations::
setLoaderFormat
($format)¶ Source: Parameters: - $format (string)
atk4¶
\I18Next\atk4
Traits¶
-
trait
I18Next\atk4\
TranslatorI18NextTrait
¶ Source: src/I18Next/atk4/TranslatorI18NextTrait.php#10
-
protected static property
I18Next\atk4\TranslatorI18NextTrait::$
translator
¶ Source: src/I18Next/atk4/TranslatorI18NextTrait.php#13 Type: \I18Next\TranslatorInterface
-
public
I18Next\atk4\TranslatorI18NextTrait::
setTranslator
($translator)¶ Source: Parameters: - $translator (
I18Next\TranslatorInterface
)
Returns: - $translator (
-
public
I18Next\atk4\TranslatorI18NextTrait::
_
($message, $parameters=null, $domain=null, $locale=null)¶ - Translates the given message.
Source: Parameters: - $message (string) The message to be translated
- $parameters (array | null) Array of parameters used to translate message
- $domain (string | null) The domain for the message or null to use the default
- $locale (string | null) The locale or null to use the default
Throws: Returns: string The translated string
Interfaces¶
TranslatorInterface¶
-
interface
I18Next\
TranslatorInterface
¶ Source: src/I18Next/TranslatorInterface.php#7
Summary¶
Methods¶
-
public
I18Next\TranslatorInterface::
_
($key, $parameters=null, $context=null, $locale=null)¶ - Translates the given message.
Source: Parameters: - $key (string)
- $parameters (array | null) Array of parameters used to translate message
- $context (string | null) The context of the request
- $locale (string | null) The locale or null to use the default
Returns: string The translated string
Classes¶
Locale¶
-
class
I18Next\
Locale
¶ Source: src/I18Next/Locale.php#14
Summary¶
Properties¶
Methods¶
-
public
I18Next\Locale::
__construct
($code)¶ - Locale constructor.
Source: Parameters: - $code (string)
-
public
I18Next\Locale::
load
($path, $use_filename_as_namespace, ...$namespace_priority)¶ Source: Parameters: - $path (string)
- $use_filename_as_namespace (bool)
- …$namespace_priority (string | null)
Throws:
-
public
I18Next\Locale::
process
($key, $parameters=null, $context=null)¶ Source: Parameters: - $key (string)
- $parameters (array | null)
- $context (string | null)
Returns: string | null
-
public
I18Next\Locale::
setLoaderFormat
($format)¶ Source: Parameters: - $format (string)
TranslatorStatic¶
-
class
I18Next\
TranslatorStatic
¶ Source: src/I18Next/TranslatorStatic.php#13
Summary¶
public static instance()
public static setTranslationsPath($path)
public static setNamespacePriority($namespace_priority)
public static useFilenameAsNamespace($enabled)
public static setLanguagePrimary($language_code)
public static addLanguage($code)
public static setLanguageFallback($fallback_code)
public static _($key, $parameters, $context, $locale)
public static register()
Properties¶
Methods¶
-
public static
I18Next\TranslatorStatic::
instance
()¶ Source: src/I18Next/TranslatorStatic.php#18
-
public static
I18Next\TranslatorStatic::
setTranslationsPath
($path)¶ Source: Parameters: - $path (string)
Throws:
-
public static
I18Next\TranslatorStatic::
setNamespacePriority
(...$namespace_priority)¶ Source: Parameters: - …$namespace_priority (mixed)
Throws:
-
public static
I18Next\TranslatorStatic::
useFilenameAsNamespace
($enabled)¶ Source: Parameters: - $enabled (bool | null)
Throws:
-
public static
I18Next\TranslatorStatic::
setLanguagePrimary
($language_code)¶ Source: Parameters: - $language_code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public static
I18Next\TranslatorStatic::
addLanguage
($code)¶ Source: Parameters: - $code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public static
I18Next\TranslatorStatic::
setLanguageFallback
($fallback_code)¶ Source: Parameters: - $fallback_code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public static
I18Next\TranslatorStatic::
_
($key, $parameters=null, $context=null, $locale=null)¶ Source: Parameters: - $key (string)
- $parameters (array | null)
- $context (string | null)
- $locale (string | null)
Returns: string
-
public static
I18Next\TranslatorStatic::
register
()¶ - Register global function _.
Source: src/I18Next/TranslatorStatic.php#109
Translator¶
-
class
I18Next\
Translator
¶ Source: src/I18Next/Translator.php#14 Implements: I18Next\TranslatorInterface
Summary¶
public setTranslationsPath($path)
public setNamespacePriority($namespace_priority)
public useFilenameAsNamespace($enabled)
public setLanguagePrimary($language_code)
public addLanguage($code)
public setLanguageFallback($fallback_code)
public _($key, $parameters, $context, $locale)
private getLocale($locale)
Properties¶
Methods¶
-
public
I18Next\Translator::
setTranslationsPath
($path)¶ - Set the root path of locales folder.
Source: Parameters: - $path (string)
Throws:
-
public
I18Next\Translator::
setNamespacePriority
(...$namespace_priority)¶ - Set namespace priority.
Source: Parameters: - …$namespace_priority (mixed)
Throws:
-
public
I18Next\Translator::
useFilenameAsNamespace
($enabled)¶ - Set if filenames will be used as namespaces.
Source: Parameters: - $enabled (bool | null)
Throws:
-
public
I18Next\Translator::
setLanguagePrimary
($language_code)¶ - Set Primary Language.
Source: Parameters: - $language_code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public
I18Next\Translator::
addLanguage
($code)¶ - Add Extra languages.
Source: Parameters: - $code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public
I18Next\Translator::
setLanguageFallback
($fallback_code)¶ - Set fallback language.
Source: Parameters: - $fallback_code (string)
Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws: Throws:
-
public
I18Next\Translator::
_
($key, $parameters=null, $context=null, $locale=null)¶ -
Source: src/I18Next/Translator.php#147