Translation
in package
Handles static text translations
You can set the current locale, load the translation for the current locale and get text for the current locale with the help of this class.
Related configuration values:
- app.root_path - for the ~ symbol in the translations' folder path
- translation.all - the all known translation locales seperated with commas, for example: "hu, en"
- translation.default - if no locale was set this will be the default, for example: "en"
Tags
Table of Contents
- CONFIG_ALL = 'translation.all'
- The configuration name of all of the known translation
- CONFIG_DEFAULT = 'translation.default'
- The configuration name of the default translation
- DEFAULT_LOCALE = 'en'
- The default locale
- $allLocales : array<string|int, mixed>
- All of the known translations
- $config : Config
- $data : array<string|int, mixed>
- The loaded translations in [namespace => [id => text]] format
- $folders : array<string|int, mixed>
- The folders for all of the translations in [namespace => path] format
- $hasMultiLocales : bool
- Is it has a multi locale config?
- $locale : string
- The current locale
- __construct() : mixed
- Sets the `$locale`, the `$allLocales` and `$hasMultiLocales` members via the `$config`
- add() : void
- Adds a folder path for a namespace
- allLocales() : array<string|int, mixed>
- Returns with all of the known locales
- get() : string
- Returns with the text by namespace and text id for the current locale
- hasMultiLocales() : bool
- Does the application has multi locales?
- locale() : string
- Returns with the current locale
- setLocale() : void
- Sets the current locale
Constants
CONFIG_ALL
The configuration name of all of the known translation
public
mixed
CONFIG_ALL
= 'translation.all'
CONFIG_DEFAULT
The configuration name of the default translation
public
mixed
CONFIG_DEFAULT
= 'translation.default'
DEFAULT_LOCALE
The default locale
public
mixed
DEFAULT_LOCALE
= 'en'
Properties
$allLocales
All of the known translations
protected
array<string|int, mixed>
$allLocales
= []
$config
protected
Config
$config
$data
The loaded translations in [namespace => [id => text]] format
protected
array<string|int, mixed>
$data
= []
$folders
The folders for all of the translations in [namespace => path] format
protected
array<string|int, mixed>
$folders
= []
$hasMultiLocales
Is it has a multi locale config?
protected
bool
$hasMultiLocales
= false
$locale
The current locale
protected
string
$locale
= 'en'
Methods
__construct()
Sets the `$locale`, the `$allLocales` and `$hasMultiLocales` members via the `$config`
public
__construct(Config $config) : mixed
Parameters
- $config : Config
Return values
mixed —add()
Adds a folder path for a namespace
public
add(string $namespace, string $folder) : void
Parameters
- $namespace : string
-
The name of the namespace
- $folder : string
-
The folder path for the namespace, it can contain a ~ symbol for the
app.root_path
Return values
void —allLocales()
Returns with all of the known locales
public
allLocales() : array<string|int, mixed>
Return values
array<string|int, mixed> —get()
Returns with the text by namespace and text id for the current locale
public
get(string $id[, array<string|int, mixed> $params = [] ]) : string
For translation you have to have multi locale config within your config.ini.php, for example:
translation.all = en, hu translation.default = en
then you have to add at least one namespace with a folder path for example in your App::init() method:
class MyApp extends App { // ... public function init() { $translation = $this->get(Translation::class); $translation->addFolder('test', '~/folder/within/the/app/root/folder'); } // ... }
In the given folder you have to have the files en.ini
and hu.ini
. Both of the files have to have the
text IDs and the translations. The en.ini
could look like:
welcome = "Hello {name}!"
and then you can use it in your code:
echo $translation->get('test:welcome', ['name' => 'Joe']);
or in your view with the tr
helper function:
<?= tr('test:welcome', ['name' => 'Joe']); ?>
the result will be with 'en' current locale:
Hello Joe!
If the translation doesn't exists, the result will be the $id
between # symbols:
#test:welcome#
Parameters
- $id : string
-
The id of the translated text in 'namespace:text_id' format
- $params : array<string|int, mixed> = []
-
The parameters for the variables in the text in ['name' => 'value'] format
Return values
string —The translated text with replaced variables
hasMultiLocales()
Does the application has multi locales?
public
hasMultiLocales() : bool
Return values
bool —True if the application has multiple known locales
locale()
Returns with the current locale
public
locale() : string
Return values
string —The current locale
setLocale()
Sets the current locale
public
setLocale(string $locale) : void
Parameters
- $locale : string
-
The current locale