TranslationService
in package
Translation service for multi-language support.
Singleton that lazily loads translation files from lang/{locale}/{section}.php. Provides key lookup with fallback chain: requested locale -> English -> raw key name.
Tags
Table of Contents
Properties
- $cache : array<string|int, mixed>
- $cacheBuilt : bool
- $instance : TranslationService|null
- $loaded : array<string|int, mixed>
- $locale : string
- $sections : array<string|int, mixed>
- $supported : array<string|int, mixed>
Methods
- get() : string
- Get a translated string by key.
- getFromSection() : string
- Get a translated string from a specific section.
- getInstance() : TranslationService
- Get the singleton instance.
- getLocale() : string
- Get the current locale.
- getSupportedLocales() : array<string|int, mixed>
- Get the list of supported locales.
- isSupported() : bool
- Check if a locale is supported.
- reset() : void
- Reset the singleton instance (for testing).
- setLocale() : void
- Set the current locale.
- __construct() : mixed
- Private constructor to enforce singleton pattern.
- applyReplacements() : string
- Apply placeholder replacements to a translated string.
- buildCache() : void
- Build the flattened cache for the current locale.
- findKeyInLocale() : string|null
- Find a key across all sections for a given locale.
- loadSection() : array<string|int, mixed>
- Load a translation section file.
Properties
$cache
private
array<string|int, mixed>
$cache
= []
Flattened cache of all keys for current locale
$cacheBuilt
private
bool
$cacheBuilt
= \false
Whether the cache has been built for the current locale
$instance
private
static TranslationService|null
$instance
= \null
Singleton instance
$loaded
private
array<string|int, mixed>
$loaded
= []
Loaded translations keyed by "{locale}.{section}"
$locale
private
string
$locale
= 'en'
Current locale code
$sections
private
static array<string|int, mixed>
$sections
= ['common', 'storefront', 'client', 'admin', 'emails', 'flash']
Translation sections to search in order
$supported
private
static array<string|int, mixed>
$supported
= ['en', 'es', 'pl']
Supported locale codes
Methods
get()
Get a translated string by key.
public
get(string $key[, array<string|int, mixed> $replacements = [] ]) : string
Searches all sections for the key. Supports :placeholder replacements. Fallback chain: current locale -> English -> raw key name.
Parameters
- $key : string
-
Translation key
- $replacements : array<string|int, mixed> = []
-
Key-value pairs for placeholder substitution
Return values
string —Translated string
getFromSection()
Get a translated string from a specific section.
public
getFromSection(string $section, string $key[, array<string|int, mixed> $replacements = [] ]) : string
Parameters
- $section : string
-
Section name (e.g., 'common', 'storefront')
- $key : string
-
Translation key
- $replacements : array<string|int, mixed> = []
-
Placeholder substitutions
Return values
stringgetInstance()
Get the singleton instance.
public
static getInstance() : TranslationService
Return values
TranslationServicegetLocale()
Get the current locale.
public
getLocale() : string
Return values
stringgetSupportedLocales()
Get the list of supported locales.
public
static getSupportedLocales() : array<string|int, mixed>
Return values
array<string|int, mixed>isSupported()
Check if a locale is supported.
public
static isSupported(string $locale) : bool
Parameters
- $locale : string
Return values
boolreset()
Reset the singleton instance (for testing).
public
static reset() : void
setLocale()
Set the current locale.
public
setLocale(string $locale) : void
Parameters
- $locale : string
-
Locale code (e.g., 'en', 'es')
Tags
__construct()
Private constructor to enforce singleton pattern.
private
__construct() : mixed
applyReplacements()
Apply placeholder replacements to a translated string.
private
applyReplacements(string $value, array<string|int, mixed> $replacements) : string
Parameters
- $value : string
-
The translated string
- $replacements : array<string|int, mixed>
-
Key-value pairs (keys should include : prefix)
Return values
stringbuildCache()
Build the flattened cache for the current locale.
private
buildCache() : void
findKeyInLocale()
Find a key across all sections for a given locale.
private
findKeyInLocale(string $key, string $locale) : string|null
Parameters
- $key : string
-
Translation key
- $locale : string
-
Locale code
Return values
string|nullloadSection()
Load a translation section file.
private
loadSection(string $locale, string $section) : array<string|int, mixed>
Parameters
- $locale : string
-
Locale code
- $section : string
-
Section name
Return values
array<string|int, mixed> —Translation key-value pairs