Examples
Real-world code samples for WPZylos I18n.
Admin Page Translations
class SettingsPage
{
private Translator $translator;
public function render(): void
{
?>
<div class="wrap">
<h1><?= $this->translator->translate('Plugin Settings') ?></h1>
<form method="post" action="options.php">
<?php settings_fields('my_plugin_options'); ?>
<table class="form-table">
<tr>
<th scope="row">
<?= $this->translator->translate('API Key') ?>
</th>
<td>
<input type="text" name="api_key" />
<p class="description">
<?= $this->translator->translate('Enter your API key from the dashboard.') ?>
</p>
</td>
</tr>
</table>
<?php submit_button($this->translator->translate('Save Settings')); ?>
</form>
</div>
<?php
}
}
Notification Messages
class NotificationService
{
public function getSuccessMessage(string $action, int $count): string
{
return match($action) {
'delete' => $this->translator->plural(
'%d item deleted successfully.',
'%d items deleted successfully.',
$count
),
'update' => $this->translator->plural(
'%d item updated successfully.',
'%d items updated successfully.',
$count
),
default => $this->translator->translate('Action completed successfully.'),
};
}
}
Form Validation Messages
class ValidationMessages
{
public function getMessages(): array
{
return [
'required' => $this->translator->translate('This field is required.'),
'email' => $this->translator->translate('Please enter a valid email address.'),
'min' => $this->translator->translate('Must be at least :min characters.'),
'max' => $this->translator->translate('Must not exceed :max characters.'),
'confirmed' => $this->translator->translate('The confirmation does not match.'),
'unique' => $this->translator->translate('This value already exists.'),
];
}
}
Email Templates
class WelcomeEmail
{
public function getSubject(): string
{
return $this->translator->translate('Welcome to Our Plugin!');
}
public function getBody(string $name): string
{
return sprintf(
$this->translator->translate('Hello %s, thank you for installing our plugin.'),
$name
);
}
}
Menu Labels
class AdminMenu
{
public function register(): void
{
add_menu_page(
$this->translator->translate('My Plugin'), // Page title
$this->translator->translate('My Plugin'), // Menu title
'manage_options',
'my-plugin',
[$this, 'renderPage'],
'dashicons-admin-generic'
);
add_submenu_page(
'my-plugin',
$this->translator->translate('Settings'),
$this->translator->translate('Settings'),
'manage_options',
'my-plugin-settings',
[$this, 'renderSettings']
);
}
}