LanguageTranslator
in package
implements
Translator
FinalYes
Table of Contents
Interfaces
Properties
- $cache : array<string|int, mixed>
- $context : LocaleContext
- $defaultLocale : string
- $translationsPath : string
Methods
- __construct() : mixed
- translate() : string
- Translate a key with optional parameters
- getNestedValue() : string|null
- Get nested array value using dot notation
- loadMessages() : array<string|int, mixed>
- Load messages for a specific locale Caches result to avoid re-reading files
Properties
$cache
private
array<string|int, mixed>
$cache
$context read-only
private
LocaleContext
$context
$defaultLocale
private
string
$defaultLocale
$translationsPath
private
string
$translationsPath
Methods
__construct()
public
__construct(LocaleContext $context[, string $translationsPath = __DIR__ . '/../../../resources/lang/' ]) : mixed
Parameters
- $context : LocaleContext
- $translationsPath : string = __DIR__ . '/../../../resources/lang/'
translate()
Translate a key with optional parameters
public
translate(string $key[, array<string|int, mixed> $params = [] ]) : string
Examples:
- trans('auth.invalid_credentials')
- trans('announcement.not_found')
- trans('errors.database.connection', ['host' => 'localhost'])
Parameters
- $key : string
-
Dot-notation key (e.g., 'auth.invalid_credentials')
- $params : array<string|int, mixed> = []
-
Parameters to interpolate in the message
Return values
string —Translated message
getNestedValue()
Get nested array value using dot notation
private
getNestedValue(array<string|int, mixed> $array, string $key) : string|null
Examples:
- getNestedValue(['auth' => ['invalid' => 'msg']], 'auth.invalid') → 'msg'
- getNestedValue(['auth' => [...]], 'auth.missing') → null
Parameters
- $array : array<string|int, mixed>
-
Array to search
- $key : string
-
Dot-notation key (e.g., 'auth.invalid_credentials')
Return values
string|nullloadMessages()
Load messages for a specific locale Caches result to avoid re-reading files
private
loadMessages(string $locale) : array<string|int, mixed>
Parameters
- $locale : string
-
Locale code (e.g., 'en', 'pl')
Return values
array<string|int, mixed> —Translation array