letkode/locale-bundle
Composer 安装命令:
composer require letkode/locale-bundle
包简介
Locale provider, listener and translatable field applier for Symfony applications
README 文档
README
Locale provider, listener and translatable field applier for Symfony applications.
Installation
composer require letkode/locale-bundle
Symfony Flex will register the bundle automatically. If not using Flex, add it manually:
// config/bundles.php return [ Letkode\LocaleBundle\LetkodeLocaleBundle::class => ['all' => true], ];
Configuration
Create config/packages/letkode_locale.yaml:
letkode_locale: default_locale: '%env(APP_DEFAULT_LOCALE)%' supported_locales: '%env(json:APP_SUPPORTED_LOCALES)%'
Add the corresponding .env variables:
APP_DEFAULT_LOCALE=en APP_SUPPORTED_LOCALES=["en","es"]
Contents
LocaleProvider
Resolves the active locale for the current request. Inject it in services that need locale-awareness.
public function __construct(private readonly LocaleProvider $localeProvider) {} public function doSomething(): void { $locale = $this->localeProvider->getLocale(); // 'en' | 'es' | ... }
LocaleListener
Kernel event listener (priority 20) that sets $request->setLocale() on every request. Reads from:
_localequery parameterAccept-Languageheader (first matching supported locale)- Configured
default_localeas fallback
TranslatableFieldApplier
Applies translated values from a translations jsonb map to entity fields for the active locale.
// Entity has: public array|null $translations = null; (via HasTranslationsTrait) // translations = {"en": {"name": "Module"}, "es": {"name": "Módulo"}} $this->translatableFieldApplier->apply($entity, ['name']); // $entity->name is now set to the locale-specific value
Requirements
- PHP
^8.4 - Symfony
^7.0 || ^8.0
License
MIT — see LICENSE.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-19