API Translation Translator - evansims/openfga-php GitHub Wiki
Translation service for OpenFGA SDK messages. This implementation provides centralized message translation with parameter substitution without external dependencies. It supports multiple locales, domain-based organization, and automatic fallback to default locale when translations are missing. The service uses a singleton pattern to maintain translation state across the application lifecycle and supports parameter substitution using %parameter% placeholder format for compatibility with existing message definitions.
Table of Contents
OpenFGA\Translation
- TranslatorInterface (interface)
Implements Translation\TranslatorInterface
public function addResource(
string $format,
string $resource,
string $locale,
string $domain = 'messages',
): void
Add a translation resource to the translator. Registers a translation file with the translator for a specific locale and domain. This allows the translator to load and use translations from various file formats and organize them by locale and domain for better maintainability.
Name | Type | Description |
---|---|---|
$format |
string |
The file format (for example, 'yaml', 'json', 'php') |
$resource |
string |
The path to the translation file |
$locale |
string |
The locale code (for example, 'en', 'es', 'fr') |
$domain |
string |
The translation domain (defaults to 'messages') |
void
Implements Translation\TranslatorInterface
public function getDefaultLanguage(): Language
Get the current default language. Returns the Language enum representing the current default locale.
Language
— The current default language
Implements Translation\TranslatorInterface
public function getDefaultLocale(): string
Get the current default locale.
string
— The current default locale code
Implements Translation\TranslatorInterface
public function has(Messages $message, string|null $locale = NULL): bool
Check if a translation exists for the given message. Determines whether a specific message has been translated in the given locale. This is useful for conditional messaging or fallback handling when translations may not be available for all locales.
Name | Type | Description |
---|---|---|
$message |
Messages |
The message enum case to check |
$locale |
string | null
|
Locale to check (defaults to configured locale) |
bool
— True if translation exists, false otherwise
Implements Translation\TranslatorInterface
public function reset(): void
Reset the translator instance. Clears the current translator instance and forces reinitialization on next use. This is particularly useful for testing scenarios where you need to start with a clean slate or when dynamically switching translation configurations.
void
Implements Translation\TranslatorInterface
public function setDefaultLanguage(Language $language): void
Set the default language for translations. Configures the default language that is used for all translation operations when no specific language is provided. This method provides type-safe language configuration using the Language enum.
Name | Type | Description |
---|---|---|
$language |
Language |
The language to set as default |
void
Implements Translation\TranslatorInterface
public function setDefaultLocale(string $locale): void
Set the default locale for translations. Configures the default locale that is used for all translation operations when no specific locale is provided. This setting affects the behavior of all translation methods and determines fallback behavior.
Name | Type | Description |
---|---|---|
$locale |
string |
The locale code (for example, 'en', 'es', 'fr') |
void
Implements Translation\TranslatorInterface
public function trans(Messages $message, array<string, mixed> $parameters = [], Language|null $language = NULL): string
Translate a message using a Messages enum case with Language enum. Provides type-safe message translation using both the Messages and Language enums for maximum compile-time safety and better developer experience. This is the preferred method for translating SDK messages.
Name | Type | Description |
---|---|---|
$message |
Messages |
The message enum case to translate |
$parameters |
array< string, mixed>
|
|
$language |
Language | null
|
Language to use (defaults to configured language) |
string
— The translated and parameterized message
Implements Translation\TranslatorInterface
public function transKey(string $key, array<string, mixed> $parameters = [], string|null $locale = NULL): string
Translate a message using a translation key string. Provides direct translation access using string keys instead of the Messages enum. This method is useful for dynamic translations or when integrating with external translation keys that are not defined in the Messages enum.
Name | Type | Description |
---|---|---|
$key |
string |
The translation key to look up |
$parameters |
array< string, mixed>
|
|
$locale |
string | null
|
Locale to use (defaults to configured locale) |
string
— The translated and parameterized message, or the key itself if no translation found