API Reference
Translator
WPZylos\Framework\I18n\Translator
Translation wrapper that provides localized strings using the plugin's text domain. Always uses $context->textDomain() - never hardcoded.
__construct(ContextInterface $context)
Create a new translator.
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin context providing text domain |
$translator = new Translator($context);
translate(string $text): string
Translate a string. Wraps WordPress __() with the plugin's text domain.
| Parameter | Type | Description |
|---|---|---|
$text | string | Text to translate |
Returns: string - Translated text.
$label = $translator->translate('Settings');
echo(string $text): void
Translate a string, escape with esc_html(), and echo it directly.
| Parameter | Type | Description |
|---|---|---|
$text | string | Text to translate |
$translator->echo('Save Changes');
sprintf(string $text, mixed ...$args): string
Translate a string and apply sprintf() formatting.
| Parameter | Type | Description |
|---|---|---|
$text | string | Text with placeholders |
...$args | mixed | Replacement values |
Returns: string - Translated and formatted text.
$msg = $translator->sprintf('Hello, %s!', $name);
plural(string $single, string $plural, int $number): string
Translate a plural string. Wraps WordPress _n() with the plugin's text domain.
| Parameter | Type | Description |
|---|---|---|
$single | string | Singular form |
$plural | string | Plural form |
$number | int | Count for determining form |
Returns: string - Translated text.
$text = $translator->plural('One item', '%d items', $count);
translateWithContext(string $text, string $context): string
Translate a string with disambiguation context. Wraps WordPress _x() with the plugin's text domain.
| Parameter | Type | Description |
|---|---|---|
$text | string | Text to translate |
$context | string | Translation context |
Returns: string - Translated text.
$noun = $translator->translateWithContext('Post', 'noun');
$verb = $translator->translateWithContext('Post', 'verb');
textDomain(): string
Get the plugin's text domain.
Returns: string - The text domain.
$domain = $translator->textDomain(); // e.g. 'my-plugin'
esc(string $text): string
Translate and escape for safe HTML output. Wraps WordPress esc_html__().
| Parameter | Type | Description |
|---|---|---|
$text | string | Text to translate |
Returns: string - Escaped translated text.
$safe = $translator->esc('Settings');
escAttr(string $text): string
Translate and escape for use in HTML attributes. Wraps WordPress esc_attr__().
| Parameter | Type | Description |
|---|---|---|
$text | string | Text to translate |
Returns: string - Escaped translated text.
$attr = $translator->escAttr('Click here');
I18n
WPZylos\Framework\I18n\I18n
Handles loading translation files for a plugin.
__construct(ContextInterface $context)
Create a new i18n loader.
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin context |
$i18n = new I18n($context);
load(): bool
Load translations. Calls load_plugin_textdomain() with the correct paths. Translation files are loaded from {plugin}/resources/lang/. Only loads once; subsequent calls return true immediately.
Returns: bool - true if loaded successfully.
$success = $i18n->load();
static loadFor(ContextInterface $context): bool
Static convenience method. Creates an instance and loads translations in one call.
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin context |
Returns: bool - true if loaded successfully.
I18n::loadFor($context);
isLoaded(): bool
Check if translations have been loaded.
Returns: bool
$i18n->isLoaded(); // true/false
getMoFilePath(string $locale): string
Get the full path to the .mo file for a specific locale.
| Parameter | Type | Description |
|---|---|---|
$locale | string | Locale code (e.g. 'de_DE') |
Returns: string - Full path to the .mo file.
$path = $i18n->getMoFilePath('de_DE');
// -> /path/to/plugin/resources/lang/my-plugin-de_DE.mo
I18nServiceProvider
WPZylos\Framework\I18n\I18nServiceProvider extends WPZylos\Framework\Core\ServiceProvider
Registers the translation system in the application container.
register(ApplicationInterface $app): void
Registers translation services:
Translator::classsingleton (using$app->context())'translator'alias bound to the Translator singletonI18n::classsingleton (using$app->context())
| Parameter | Type | Description |
|---|---|---|
$app | ApplicationInterface | Application instance |
// After registration:
$translator = $app->make(Translator::class);
$translator = $app->make('translator');
$i18n = $app->make(I18n::class);
boot(ApplicationInterface $app): void
Hooks into WordPress init action to automatically load translations.
| Parameter | Type | Description |
|---|---|---|
$app | ApplicationInterface | Application instance |
Internally:
add_action('init', function () {
$i18n = $this->make(I18n::class);
$i18n->load();
});