laminas/laminas-i18n
Laminas I18n provides internationalization tools for Laminas apps, including translation services, locale and pluralization support, and integration with view helpers and validators. Use it to build multilingual, locale-aware PHP applications.
Begin by installing the package via Composer: composer require laminas/laminas-i18n. After installation, configure the translator in your application’s service configuration (e.g., config/global.php or config/modules.config.php for Laminas MVC). Set up translation loaders (e.g., Csv, Ini, PhpArray, or Mo) and register translation files grouped by locale and text domain (e.g., messages-en_US.csv). Use the translator service—retrieved via ServiceManager—to translate strings: $translator->translate('Hello world!', 'messages'). For quick prototyping, the Translator service supports a default text domain (default), reducing boilerplate. First use case: localizing a simple form label or error message across languages.
validation, user, marketing) to avoid key collisions and improve maintainability.PhpArray or Mo for production (fastest), Csv/Ini for development (human-editable).translatePlural() for dynamic, locale-aware plural forms:
echo $translator->translatePlural(
'%count% apple',
'%count% apples',
$count,
'messages',
['%count%' => $count]
);
__invoke() factories or use the TranslatorAwareInterface in forms and view helpers.Locale::acceptFromHttp() with custom logic (e.g., user preference fallback, subdomain routing) for robust locale resolution:
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$translator->setLocale($locale ?: 'en_US');
%s with ICU plural rules—stick to {count} style for translatePlural(). Use MessageFormatter manually if sticking with gettext-style %d/%s is required (less recommended).Cache or use compiled PhpArray files to avoid parsing overhead on every request. Configure caching via translator.translate_options.cache.translate() call includes the domain, except when using the default domain.Ini or Csv loaders (they’re sensitive to trailing slashes).Translator\LoaderInterface to integrate custom sources (e.g., database, API). For example, Locale\LocalePluginManager can be extended to add custom locale detection strategies.translator.translate_options.debug (set to true or an array of keys) to output raw translation keys in the response for missing entries.How can I help you explore Laravel packages today?