Documentation

Translator
in package

Loads translations from per-namespace JSON files and resolves dot-notated keys with parameter interpolation.

Keys follow the format "namespace.key", e.g. "common.save". The namespace maps to a JSON file: locale/{lang}/common.json. Namespaces are loaded lazily on first access.

Tags
since
3.0.0

Table of Contents

Properties

$loaded  : array<string, array<string, string>>
Cache of loaded namespace translations.
$locale  : string
Active locale code (e.g. "en", "es").
$localePath  : string
Base path to the locale directory.

Methods

__construct()  : mixed
getAvailableLocales()  : array<string|int, string>
Get list of available locale codes.
getLocale()  : string
Get the active locale.
getNamespaceTranslations()  : array<string, string>
Get all translations for a namespace in the active locale.
setLocale()  : void
Set the active locale.
translate()  : string
Translate a dot-notated key with optional parameter interpolation.
loadNamespace()  : array<string, string>
Load a namespace file for a locale, caching the result.
lookup()  : string|null
Look up a single key in a specific locale and namespace.
resolveKey()  : array{0: string, 1: string}
Split a dot-notated key into namespace and sub-key.

Properties

$loaded

Cache of loaded namespace translations.

private array<string, array<string, string>> $loaded = []

Keyed by "{locale}.{namespace}" => [key => translation].

$locale

Active locale code (e.g. "en", "es").

private string $locale

$localePath

Base path to the locale directory.

private string $localePath

Methods

__construct()

public __construct(string $localePath[, string $locale = 'en' ]) : mixed
Parameters
$localePath : string

Absolute path to the locale/ directory

$locale : string = 'en'

Active locale code (default: "en")

getAvailableLocales()

Get list of available locale codes.

public getAvailableLocales() : array<string|int, string>

A locale is considered available if its directory contains a common.json file.

Return values
array<string|int, string>

Locale codes (e.g. ["en", "es", "zh"])

getLocale()

Get the active locale.

public getLocale() : string
Return values
string

getNamespaceTranslations()

Get all translations for a namespace in the active locale.

public getNamespaceTranslations(string $namespace) : array<string, string>

Used to inject translations into the frontend.

Parameters
$namespace : string

Namespace name (e.g. "common")

Return values
array<string, string>

Key-value translation pairs

setLocale()

Set the active locale.

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

Locale code (e.g. "es", "zh")

translate()

Translate a dot-notated key with optional parameter interpolation.

public translate(string $key[, array<string, string|int> $params = [] ]) : string
Parameters
$key : string

Dot-notated key (e.g. "common.save")

$params : array<string, string|int> = []

Interpolation parameters

Return values
string

Translated string, or the raw key if not found

loadNamespace()

Load a namespace file for a locale, caching the result.

private loadNamespace(string $locale, string $namespace) : array<string, string>
Parameters
$locale : string

Locale code

$namespace : string

Namespace name

Return values
array<string, string>

Key-value pairs from the JSON file

lookup()

Look up a single key in a specific locale and namespace.

private lookup(string $locale, string $namespace, string $subKey) : string|null
Parameters
$locale : string

Locale code

$namespace : string

Namespace name

$subKey : string

Key within the namespace

Return values
string|null

Translation or null if not found

resolveKey()

Split a dot-notated key into namespace and sub-key.

private resolveKey(string $key) : array{0: string, 1: string}

"common.save" => ["common", "save"] "admin.dashboard.title" => ["admin", "dashboard.title"] "orphan" => ["common", "orphan"]

Parameters
$key : string

Dot-notated key

Return values
array{0: string, 1: string}

[namespace, subKey]


        
On this page

Search results