Localization
Resolving aktuální locale, načítání překladů, fallback strategie a použití jazyka během runtime.
Overview
Localization is registered by LocalizationServiceProvider. Primary runtime contracts are TranslatorInterface and LocaleResolverInterface. Router and URL generation can use localization URL strategy config for localized route names and prefixes.
Configuration
Localization is configured in app/Config/Localization.php under localization.* keys.
return [
'localization' => [
'default_locale' => 'cs',
'fallback_locale' => 'en',
'supported_locales' => ['cs', 'en'],
'url' => [
'enabled' => true,
'include_default_locale' => false,
'localized_route_name_prefix' => 'localized.',
'locale_parameter' => 'locale',
'route_prefix' => '/{locale}',
],
],
];
Translator behavior
FileTranslator loads translation groups from framework files (src/Language/{locale}/{group}.php) and overlays app files (app/Language/{locale}/{group}.php). Key lookup order for get() is: resolved locale -> fallback locale -> raw key string.
Fallbacks and missing keys
If translation key is missing in both resolved locale and fallback locale, translator returns the original key (for example homepage.title).
Locale resolution
LocaleResolver resolves locale in this order: runtime translator locale override, localization.default_locale, then localization.fallback_locale; all must be inside localization.supported_locales (non-empty list). Invalid config throws runtime exception.
Controller usage
Controllers can use translator directly ($this->translator()->get(...)) or convenience method ($this->trans(...)).
$title = $this->translator()->get('home.hero.title');
$button = $this->trans('home.hero.primary_cta');
Dependency injection usage
In services, inject TranslatorInterface (or LocaleResolverInterface when only locale resolution is needed).
use Lemonade\Framework\Localization\TranslatorInterface;
public function __construct(
private readonly TranslatorInterface $translator,
) {}
View usage
Views should use the explicit $helpers object shared by the view lifecycle.
<?= e($helpers->lang('home.hero.title')) ?>
<?= e($helpers->lang('home.hero.primary_cta')) ?>
Localized routes and URLs
Router supports localizedGroup(...). During bootstrap, framework configures localized route name prefix, locale parameter and localized path prefix from localization.url.*. UrlGenerator::localizedRoute(...) uses LocaleUrlStrategyInterface and falls back to non-localized route when localized variant does not exist.
$router->localizedGroup(static function (Router $router): void {
$router->getNamed('home', '/', 'HomeController@index');
});
$url = $this->url()->localizedRoute('home');