Documentation

TranslationInterface

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 - used 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
see
Config

Table of Contents

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

Methods

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