framework-php

Nette Translate: a comprehensive guide to internationalization in Nette Framework

Nette Translate is a component of the Nette Framework that provides robust internationalization (i18n) and localization (l10n) support for PHP applications. It helps you deliver...

Mara Ellison
Nette Translate: a comprehensive guide to internationalization in Nette Framework

What Nette Translate is and why it matters for multilingual applications

Nette Translate is a component of the Nette Framework that provides robust internationalization (i18n) and localization (l10n) support for PHP applications. It helps you deliver content in multiple languages by managing translations, resolving locales, and formatting messages for different regions. This guide explains how the translate Nette integration works, how to set it up, and how to use it effectively in real-world projects.

How Nette Translate integrates into the Nette ecosystem

Nette Translate is designed to work seamlessly with other Nette components, such as Dependency Injection and Application modules. It is typically configured as a translator service that presenters and templates can rely on. The extension integrates with the routing and request lifecycle to select the correct language early, enabling consistent localization across UI, forms, and API responses.

Translator service setup and DI binding

In a Nette application, the translator is usually added to the DI container with a specific locale and fallback rules. You can bind implementations of Nette\Localization\Translator and related helpers so they are available everywhere. This centralized setup simplifies switching languages and managing plural forms, dates, and domain-specific dictionaries.

Supported features: locales, plural forms, and dictionary management

Nette Translate handles language identification, fallback chains, and plural rule selection based on CLDR data. It supports domain-based translations, allowing you to split messages by module or feature. You can manage dictionaries in NEON or PHP array formats and organize them by locale, making large projects easier to maintain.

Key data points at a glance

Attribute Verified Detail Source Type
Primary interface Nette\Localization\Translator Framework API
Locale resolution From request, session, or fallback chain Configuration
Plural rules Based on CLDR for each locale Framework logic
Dictionary formats NEON, PHP arrays Nette conventions
Typical use cases UI labels, forms, route prefixes, emails Common practice

Setting up translators and message domains

To use Nette translate, you first configure available locales and set a default language in the NEON configuration. You then define message domains to separate concerns, such as frontend and backend or feature-specific bundles. This modular approach keeps translations manageable and allows different teams to work on different dictionaries without conflicts.

Basic configuration example

In your config.neon, you typically register the translator extension, set supported locales, and define fallback rules. You can also specify which locale should be used when a user does not have a preference. These settings are read by the translator service and applied during the application bootstrap, ensuring the correct language is selected before rendering.

Using translations in presenters and templates

In presenters, you can translate strings by calling the translator with a message ID and optional parameters. In Latte templates, you use the |translate filter or the {_phrase} macro to render translated text. Both approaches support parameter substitution and pluralization, letting you build dynamic sentences that remain grammatically correct across languages.

Practical usage patterns

  • Use consistent message IDs across modules to avoid duplication.
  • Leverage domains to separate admin, frontend, and API translations.
  • Store locale metadata in the session to remember user preference.
  • Combine translator usage with proper HTML escaping for security.
  • Test plural forms and fallbacks for edge-case locales.

Troubleshooting and best practices

Missing translations should fall back to a default language, but you should monitor logs to identify gaps. Keep dictionaries small and focused, and document conventions for translators. Avoid hardcoding language assumptions in business logic; instead, rely on the translator service to resolve text at render time. Regular reviews of translation coverage help maintain quality as your application grows.

FAQ

Reader questions

How does Nette pick the right language for a request?

Nette determines locale from the request, session, or a configured fallback chain. The translator service resolves the locale early in the lifecycle, ensuring consistent language selection across the app.

Can I use Nette Translate without the full framework?

Yes, Nette\Localization\Translator can be used as a standalone component in other PHP projects, provided you set up the necessary dependencies and configuration manually.

What plural rules does Nette support?

Nette uses CLDR plural rules per locale, covering one, two, few, many, and other forms. This ensures correct pluralization for languages with complex grammatical number systems.