Documentation

Translation
in package
implements TranslationInterface

Interfaces, Classes, Traits and Enums

TranslationInterface
Handles static text translations

Table of Contents

CONFIG_ALL  = 'translation.all'
The configuration name of all 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 the known translations
$config  : ConfigInterface
$data  : array<string|int, mixed>
The loaded translations in [namespace => [id => text]] format
$folders  : array<string|int, mixed>
The folders for all the translations in [namespace => path] format
$hasMultiLocales  : bool
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 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 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 the known translations

protected array<string|int, mixed> $allLocales = []

$data

The loaded translations in [namespace => [id => text]] format

protected array<string|int, mixed> $data = []

$folders

The folders for all the translations in [namespace => path] format

protected array<string|int, mixed> $folders = []

$hasMultiLocales

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(ConfigInterface $config) : mixed
Parameters
$config : ConfigInterface
Return values
mixed

add()

Adds a folder path for a namespace

public add(string $namespace, string $folder) : void
Parameters
$namespace : string
$folder : string
Return values
void

allLocales()

Returns with all 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

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 App extends AbstractApp {
  // ...
  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 exist, the result will be the $id between # symbols:

#test:welcome#
Parameters
$id : string
$params : array<string|int, mixed> = []
Return values
string

hasMultiLocales()

Does the application has multi locales?

public hasMultiLocales() : bool
Return values
bool

locale()

Returns with the current locale

public locale() : string
Return values
string

setLocale()

Sets the current locale

public setLocale(string $locale) : void
Parameters
$locale : string
Return values
void

Search results