PHPackages                             czukowski/i18n-nette - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. czukowski/i18n-nette

ActiveLibrary[Localization &amp; i18n](/categories/localization)

czukowski/i18n-nette
====================

Nette Framework adapter for I18n package

1.1.2(9y ago)32.5kMITPHPPHP &gt;=5.3.0

Since Jul 6Pushed 7y ago1 watchersCompare

[ Source](https://github.com/czukowski/I18n_Plurals_Nette)[ Packagist](https://packagist.org/packages/czukowski/i18n-nette)[ Docs](https://github.com/czukowski/I18n_Plural_Nette)[ RSS](/packages/czukowski-i18n-nette/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (6)Versions (5)Used By (0)

Introduction
============

[](#introduction)

This package will help you to do grammatically accurate translations in your Nette application (framework version 2.2+ supported; still working great in 2.4, yay!).

The *suggested* translation sources for Nette application are actually Neon files which may be located virtually anywhere in the application, for example, `app/i18n` folder, this will be used for the examples. Note that you are not limited in where you store your translations: also pure PHP files, databases and even all of them together may be used!

Installation and setup
----------------------

[](#installation-and-setup)

Add to your application using Composer:

`composer require czukowski/i18n-nette`

Add I18n service to your configuration file:

```
extensions:
    i18n: I18n\Nette\NetteExtension

i18n:
    defaultLang: cs       # Default fallback language
    directories:          # List of directories containing i18n files
        - %appDir%/i18n
    languages:            # List of languages available in the application,
        - cs              # it is useful for choosing default language from
        - en              # HTTP request headers
```

Place your translations into the i18n directory, like this:

- `en.neon` - English translations,
- `cs.neon` - Czech translations,
- `fr.neon` - General French translations,
- `fr/be.neon` - Belgium French translations that are different from general French,
- `fr/ch.neon` - Swiss French translations that are different from general French.

If you request the translation for 'fr-CH' locale, it'll look in the `fr/ch.neon` first, and failing that in the general `fr.neon`. If the translation wasn't found even there, the untranslated input string is returned.

The translation data structure is very similar to what you're used to in Neon configuration (may be either flat or nested, your choice!):

```
string: řetězec
section:
    string: 'řetězec v podsekci'
```

Some of the Nette controls are ready for translations, you just need to set the translator instance to them in your factories, for example (assuming `$this->translator` is an instance of the translator service provided by this package):

```
// Set translator to control (Nette\Forms\Controls\BaseControl):
$control->setTranslator($this->translator);
// Set translator to form (Nette\Forms\Form):
$form->setTranslator($this->translator);
// Set translator to template (Nette\Templating\Template or Nette\Bridges\ApplicationLatte\Template):
$template->setTranslator($this->translator);
```

After setting the translator to the templates, you'll be able to use the translation macro: `{_'translate this'}`. We'll get into the details on its usage later on.

Configuration options
---------------------

[](#configuration-options)

Configuration options available for this Nette extension:

- `defaultLang` - Default application language, ie translate to this language if no target language specified in translate function call (default value: 'en-us').
- `directories` - Directories containing application translations. May be more than one if application contains multiple modules, each adding to the translations list. Paths may contain template keys from config parameters section, eg `%appDir%/i18n` (no default value).
- `languages` - List of available languages. Useful when default language is set automatically from HTTP Request headers (no default value).
- `useNeonStyleParams` - If set to TRUE, wraps replacement parameter names into percent signs (eg. `param` becomes `%param%`), so that translation keys can look similarly to the template parameters in neon configuration file, while using bare parameter keys in the translate calls. Example: `{_'I have %count% strings to translate', $count, ['count' => $count]}`. Default value is FALSE, but if your application uses only neon files as translation sources, using this may look nicer. Changing this parameter mid-way will require to review all translate calls across your application, so choose wisely.
- `setLangFromRequestHeaders` - If set to TRUE, will automatically set default language from HTTP Request (the respective function can also be called manually). Only languages existing in the `languages` list will be set, according to priorities in the headers.
- `replaceLatteFactory` - If set to TRUE, replaces `latte.templateFactory` service with a new one, that implements a callback on template create. This callback may be used to inject translator to templates automatically (default value is FALSE, but if you use custom Latte Template Factory replacement, you may set it to TRUE safely).
- `latteFactoryClass` - This is a class name that will be used for the replacement template factory. This setting allows to override it and use another class that implements the same functionality, if needed (default value: `'I18n\Nette\TemplateFactory'`). This setting will have no effect, unless the `replaceLatteFactory` parameter is set to TRUE.
- `autoSetTranslatorToTemplates` - If set to TRUE, will inject translator to templates automatically, using the replacement Latte Template Factory. If `replaceLatteFactory` is set to TRUE, this parameter is also set to TRUE implicitly. The only valid use case to set this parameter is when another template factory is already replaced by another class, and it is still desired to auto-set translator to templates using `onCreateTemplate` callback.

How to make the translations work
---------------------------------

[](#how-to-make-the-translations-work)

You can find the information about the translation contexts, plural forms and even more in [the base package readme](https://github.com/czukowski/I18n_Plural#translation-contexts). *It is omitted here in order to avoid duplication*.

Translating in Nette templates
------------------------------

[](#translating-in-nette-templates)

Here are some examples, those are pretty self-explanatory:

```
// Basic translation.
{_'Welcome!'}
// Translation with context.
{_'New customer has been saved.', $customer->gender}
// Translation with parameters and context skipped.
{_'Hi, my name is :name', [':name' => $name]}
// All arguments present, including target language.
{_'You have :count messages', $count, [':count' => $count], 'cs'}

```

Note: if you use `useNeonStyleParams`, the translation could look like:

```
{_'You have %count% messages', $count, ['count' => $count], 'cs'}

```

API
---

[](#api)

The base package API is covered in its own readme.

### class I18n\\NetteTranslator

[](#class-i18nnettetranslator)

You are not required to use core object directly. This class is a Nette-compatible wrapper and it's the suggested usage in Nette applications. See above for an example on how to setup a trasnlation service.

#### public function \_\_construct($default\_lang = 'x', $use\_neon\_style\_params = FALSE)

[](#public-function-__constructdefault_lang--x-use_neon_style_params--false)

- @param string $default\_lang
- @param boolean $use\_neon\_style\_params

Translator constructor takes default language to use when none is specified explicitly. Initializes a Core object instance internally.

#### public function attach(I18n\\Reader\\ReaderInterface $reader)

[](#public-function-attachi18nreaderreaderinterface-reader)

- @param I18n\\Reader\\ReaderInterface $reader

Attaches a Reader object to the Core object (see below). `I18n\Nette\NeonReader` is a suggested default reader for Nette application, although there's `I18n\Nette\NetteReader` that gets translations from raw PHP files placed similarly into the Nette application, and you may of course also implement your own readers to provide translations from any source of your choice.

#### public function getAvailableLanguages()

[](#public-function-getavailablelanguages)

Returns list of available languages.

#### public function setAvailableLanguages($langs)

[](#public-function-setavailablelanguageslangs)

- @param array $langs
- @return $this

Sets list of available languages.

#### public function getDefaultLanguage()

[](#public-function-getdefaultlanguage)

Returns default language, used when no target language passed to translate function call.

#### public function setDefaultLanguage($lang)

[](#public-function-setdefaultlanguagelang)

- @param string $lang
- @return $this

Sets default language.

#### public function setLanguageFromHeaders(Nette\\Http\\IRequest $httpRequest)

[](#public-function-setlanguagefromheadersnettehttpirequest-httprequest)

- @param Nette\\Http\\IRequest $httpRequest
- @return $this

Sets default language from HTTP Request headers, if at least one of accepted language is contained in the available languages list. The one with the highest priority is chosen.

#### public function setTranslator($object)

[](#public-function-settranslatorobject)

- @param object $object

Sets Nette translator (`$this` object instance) to the compatible objects:

- `'Nette\Bridges\ApplicationLatte\Template'`
- `'Nette\Forms\Controls\BaseControl'`
- `'Nette\Forms\Form'`
- `'Nette\Templating\Template'`

#### public function translate($string, $count, $parameters, $lang)

[](#public-function-translatestring-count-parameters-lang)

- @param string $string String to translate
- @param mixed $count String form or numeric count (optional)
- @param array $parameters Param values to replace (optional)
- @param string $lang Target language (optional)
- @return string

The `$parameters` values (array) may be passed as 2nd argument, in that case `$count` is considered `NULL`and `$lang` is the 3rd argument.

#### public function getService()

[](#public-function-getservice)

- @return I18n\\Core

Returns the internal Core object reference if needed for some reason.

License
-------

[](#license)

The distribution is permitted under the MIT License. See LICENSE.md for details.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~37 days

Total

4

Last Release

3533d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/186792?v=4)[Korney Czukowski](/maintainers/czukowski)[@czukowski](https://github.com/czukowski)

---

Top Contributors

[![czukowski](https://avatars.githubusercontent.com/u/186792?v=4)](https://github.com/czukowski "czukowski (21 commits)")

---

Tags

i18nlocalizationnettepluralsi18npluralsnette

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/czukowski-i18n-nette/health.svg)

```
[![Health](https://phpackages.com/badges/czukowski-i18n-nette/health.svg)](https://phpackages.com/packages/czukowski-i18n-nette)
```

###  Alternatives

[contributte/translation

Symfony/Translation integration for Nette Framework.

771.8M49](/packages/contributte-translation)[kdyby/translation

Integration of Symfony/Translation into Nette Framework

891.2M24](/packages/kdyby-translation)[gettext/languages

gettext languages with plural rules

7532.0M12](/packages/gettext-languages)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
