FacturaScripts includes a powerful translation system that supports multiple languages and locales. The Translator class (Core/Translator.php) manages translations with parameter substitution and fallback support.
use FacturaScripts\Core\Tools;// Get translator with default language$i18n = Tools::lang();// Get translator for specific languageuse FacturaScripts\Core\Translator;$i18n = new Translator('es_ES');
// Translate a string$text = $i18n->trans('accept');// Returns: "Aceptar" (in Spanish)$text = $i18n->trans('cancel');// Returns: "Cancelar" (in Spanish)// If translation not found, returns the key$text = $i18n->trans('non-existent-key');// Returns: "non-existent-key"
Translations can include placeholders for dynamic values:
// Translation with single parameter$text = $i18n->trans('account-bad-parent', ['%codcuenta%' => '999']);// Returns: "La cuenta 999 tiene asociada una cuenta padre equivocada."// Translation with multiple parameters$text = $i18n->trans('user-greeting', [ '%name%' => 'John', '%role%' => 'Administrator']);
Parameter keys must start and end with %. The values must be strings or numbers.
Each translation file is a JSON object mapping keys to translated strings:
{ "accept": "Accept", "cancel": "Cancel", "save": "Save", "delete": "Delete", "account-bad-parent": "The account %codcuenta% has an incorrect parent account.", "user-greeting": "Hello %name%, you are logged in as %role%."}
{ "accept": "Aceptar", "cancel": "Cancelar", "save": "Guardar", "delete": "Eliminar", "account-bad-parent": "La cuenta %codcuenta% tiene asociada una cuenta padre equivocada.", "user-greeting": "Hola %name%, has iniciado sesión como %role%."}
// In your plugin controller or model$text = $this->toolBox()->i18n()->trans('my-plugin-setting');// With parameters$text = $this->toolBox()->i18n()->trans('my-plugin-welcome', [ '%name%' => 'MyPlugin']);
Translate a string in a specific language without changing the current language:
// Get translation in Spanish regardless of current language$text = $i18n->customTrans('es_ES', 'welcome-message');// With parameters$text = $i18n->customTrans('es_ES', 'user-greeting', [ '%name%' => 'John']);
During development, you can track which translations are missing:
$i18n = new Translator('es_ES');// Request translations$i18n->trans('existing-key');$i18n->trans('missing-key');$i18n->trans('another-missing-key');// Get list of missing translations$missing = $i18n->getMissingStrings();// Returns: ['missing-key@es_ES' => 'missing-key', 'another-missing-key@es_ES' => 'another-missing-key']// Get all used translations$used = $i18n->getUsedStrings();