Download the PHP package webmunkeez/i18n-bundle without Composer
On this page you can find all versions of the php package webmunkeez/i18n-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webmunkeez/i18n-bundle
More information about webmunkeez/i18n-bundle
Files in webmunkeez/i18n-bundle
Package i18n-bundle
Short Description Internationalization for Symfony.
License MIT
Homepage https://github.com/yannissgarra/i18n-bundle
Informations about the package i18n-bundle
WebmunkeezI18nBundle
This bundle unleashes internationalization on Symfony applications.
Installation
Use Composer to install this bundle:
Add the bundle in your application kernel:
Configuration
The bundle prepends sane framework.default_locale/framework.translator/framework.set_content_language_from_locale defaults so a fresh app works out of the box, but enabled_locales itself has no default — every consuming application must declare it explicitly.
Usage
Languages
\Webmunkeez\I18nBundle\Repository\LanguageRepositoryInterface (backed by LanguageDependencyInjectionRepository, built from enabled_locales) exposes findAll(), findOneByLocale(string $locale) (throws LanguageNotFoundException), findOneDefault() (the first enabled locale) and localeExists(string $locale): bool. Each Language::getName() uses Symfony\Component\Intl\Locales, so region-specific entries get a distinct, region-aware name — fr_FR and fr_CA both configured side by side come out as "Français (France)" and "Français (Canada)" rather than colliding on a single "Français".
LocaleRequestListener runs on every request: if a _locale query parameter is present and enabled, it becomes the request locale and a \Webmunkeez\I18nBundle\Model\Language is stored as the current-language request attribute; otherwise it falls back to whatever current-language is already set (e.g. by the multi-site listener below) or to findOneDefault().
The #[Webmunkeez\I18nBundle\Validator\Constraint\Locale] constraint validates that a string property is one of the enabled locales:
Language-aware objects
Any translation-like class can implement \Webmunkeez\I18nBundle\Model\LanguageAwareInterface (getLocale() + getLanguage()/setLanguage()) to get its Language resolved and cached lazily:
The language() Twig function resolves it (and caches the result back onto the object, so it's only looked up once):
It also accepts a raw locale string directly — useful when you don't have a LanguageAwareInterface object at hand (this call is never cached, since there's nowhere to store the result):
\Webmunkeez\I18nBundle\Serializer\Normalizer\LanguageAwareNormalizer does the same resolution automatically when serializing any LanguageAwareInterface object.
The language_name() Twig function returns a language's autonym (its name in its own language) from any ISO code, regardless of enabled_locales — unlike language(), it never fails on a language that isn't configured. It accepts both a plain language code and a full locale (region part is ignored), and returns null if the code isn't a valid language:
Pass a second argument to get the name translated into another language instead of its autonym:
Translations
A translatable entity implements \Webmunkeez\I18nBundle\Model\TranslationAwareInterface (getTranslations()/getTranslation(string $locale), throwing TranslationNotFoundException) over a collection of \Webmunkeez\I18nBundle\Model\TranslationInterface (itself just LocaleAwareInterface, typically also implementing LanguageAwareInterface):
TranslatorAware
\Webmunkeez\I18nBundle\Translation\TranslatorAwareInterface/TranslatorAwareTrait give any service $this->trans() and $this->getTranslatorCatalogue(), wired automatically by an idempotent compiler pass (it only injects the translator if you haven't already configured your own setTranslator() call on that service, so your own explicit wiring is never silently overridden):
Multi-site
If your application serves several sites/locales behind different hosts and/or path prefixes, declare them under webmunkeez_i18n.sites (each entry validates that its locale, if set, is part of enabled_locales). position is required, must be at least 1 and must be unique across sites — it's purely a display ordering: findAll()/findAllLocalized() and the sites() Twig function sort by it, from the smallest position to the largest, regardless of declaration order. It has no effect on routing: findOneByUrl() (used by SiteRequestListener to resolve the current request) always matches against sites in declaration order, so the catch-all-last rule below still applies independently of position.
path is a plain literal path prefix (not a regex) — a request matches when its URI starts with path followed by / or the end of the string, so /api matches /api and /api/anything but not /apiary. Omitting path entirely matches any path on that host (an explicit path: null is rejected, same as an empty string — leave the key out instead), so declare the catch-all site for a given host last — regardless of its position, which only affects display order.
host can be omitted the same way to match any host — useful when every request lands on the same Symfony project regardless of subdomain (e.g. a wildcard DNS setup with subdomain1.example.com, subdomain2.example.com, etc. all pointing at the same app) and the routing should only depend on the path, not which subdomain was used:
SiteRequestListener runs before LocaleRequestListener and resolves the current request into either a \Webmunkeez\I18nBundle\Model\Site (host + path, no locale) or a \Webmunkeez\I18nBundle\Model\LocalizedSite (also LanguageAwareInterface) via \Webmunkeez\I18nBundle\Repository\SiteRepositoryInterface::findOneByUrl(), throwing SiteNotFoundException (converted to a 404) if nothing matches. The resolved site is stored as the current-site request attribute, and for a matched LocalizedSite the request locale and current-language are set immediately — before LocaleRequestListener even runs. The listener is a no-op entirely (no site resolution attempted) when no site is configured.
The sites() Twig function lists every configured site (SiteRepositoryInterface::findAll()) — useful for rendering a language/site switcher:
Ago filter
The ago Twig filter formats a \DateTimeInterface (\DateTime or \DateTimeImmutable) as a human-readable relative time, translated through the date_interval.* message keys. It only accepts dates in the past — a future date throws an \InvalidArgumentException:
Exceptions
LanguageNotFoundException,SiteNotFoundException,TranslationNotFoundExceptionall extend\Webmunkeez\CQRSBundle\Exception\ModelNotFoundException(from the requiredwebmunkeez/cqrs-bundledependency), so they're automatically converted to a 404NotFoundHttpExceptionby itsModelNotFoundExceptionListener.
All versions of i18n-bundle with dependencies
symfony/config Version ^7.4
symfony/dependency-injection Version ^7.4
symfony/expression-language Version ^7.4
symfony/intl Version ^7.4
symfony/property-access Version ^7.4
symfony/property-info Version ^7.4
symfony/serializer Version ^7.4
symfony/string Version ^7.4
symfony/translation Version ^7.4
symfony/uid Version ^7.4
symfony/validator Version ^7.4
twig/twig Version ^3
webmunkeez/cqrs-bundle Version ^3.0