Installation:
composer require goc/locale-bundle
Add to config/bundles.php:
return [
// ...
GardenOfConcepts\LocaleBundle\GardenOfConceptsLocaleBundle::class => ['all' => true],
];
Configuration:
Define locales in config/packages/goc_locale.yaml:
goc_locale:
default_locale: 'en'
locales: ['en', 'de', 'fr']
First Use Case: Localize a number in Twig:
{{ 1234.56|goc_number(locale='de') }} {# Outputs: 1.234,56 #}
Resources/doc/twig.rst: Twig filter documentation.DependencyInjection/GardenOfConceptsLocaleExtension.php: Bundle configuration.Twig/GardenOfConceptsLocaleExtension.php: Core Twig filters.Locale-Aware Number Formatting:
{{ 1000|goc_number(locale='fr') }} {# 1 000 #}
{{ 0.5|goc_percent(locale='de') }} {# 50 % #}
Dynamic Locale Switching: Use a user locale from session:
{% set userLocale = app.session.get('user_locale', 'en') %}
{{ 123.45|goc_currency(locale=userLocale, currency='EUR') }}
Date/Time Localization:
{{ '2023-12-25'|date('Y-m-d')|goc_date(locale='ja') }} {# 2023年12月25日 #}
Country Code Conversion:
{{ 'US'|goc_country_name(locale='de') }} {# Vereinigte Staaten #}
goc_number/goc_currency in form themes for localized labels.JsonResponse):
return new JsonResponse([
'price' => $product->price->format('de')
]);
goc_locale.yaml:
locales: ['en', 'de']
fallback_locale: 'en'
Missing intl Extension:
Class 'IntlDateFormatter' not found.intl extension (sudo apt-get install php-intl on Ubuntu).Unsupported Locales:
goc_currency fail for unsupported locales (e.g., ar).Twig Cache Invalidation:
php bin/console cache:clear
Currency Symbols:
goc_currency uses locale-specific symbols (e.g., € for de). Override via currency parameter:
{{ 100|goc_currency(locale='en', currency='USD') }} {# $100 #}
use IntlDateFormatter;
var_dump(IntlDateFormatter::getAvailableLocales());
en_US instead of en). Test with:
{{ 'en_US'|goc_country_name }}
Custom Formatters:
Extend GardenOfConcepts\LocaleBundle\Twig\GardenOfConceptsLocaleExtension to add new filters.
Override Defaults:
Configure locale-specific rules in config/packages/goc_locale.yaml:
goc_locale:
number_formatter:
decimal_separator: ','
grouping_separator: '.'
Address/Salutation (Future): Monitor the repo for updates or fork to implement missing features (e.g., address formatting).
{% set cachedPrice = 100|goc_currency(locale=userLocale) %}
IntlDateFormatter directly in PHPUnit for deterministic tests:
$formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'dd.MM.yyyy', IntlDateFormatter::GREGORIAN);
How can I help you explore Laravel packages today?