Componette

Componette

bckp

bckp / translator-nette v2.0.0-rc1

Nette extension for Translator

download-cloud-line composer require bckp/translator-nette

Bckp\Translator

Downloads this Month Tests Coverage Status Scrutinizer Code Quality Latest Stable Version License

Simple and fast PHP translator

Usage

The best way to install bckp/translator-nette is using Composer:

$ composer require bckp/translator-nette

Configuration

In you nette config, add extension with temporary dir

extensions:
	translator: Bckp\Translator\Nette\Bridges\TranslatorExtension(%tempDir%/translator)

and set translator itself

translator:
	languages:
		- cs #Language codes
	path:
		- %appDir%/Locale # Where to search for language file
	pluralProvider: Bckp\Translator\PluralProvider # Optional - default is PluralProvider from bckp/translator-core
	debugger: %debugMode% # Optional - default is %debugMode%
	injectLatte: true # Optional - default is true, this will autoregister translator into latte
	resolvers:
	    - Bckp\Translator\Nette\Resolvers\HeaderLocaleResolver() # Any class implements Resolver, will be used to resolve language, if more then one used, extension will cyclu thru all of them to find first that can be used

Translator will find all files in path and make map to the DI. If debugger is on, it will on each request check, if any file is modified and rebuild all language file (only needed ones).

Naming convention

All language files should be in Neon format, and proper naming is: {module}.{langCode}.neon

Translation file format

Translation files are written in NEON format. Plural strings are in ARRAY, otherwise STRING.

welcome: 'Vítejte'
withArgs: 'Ahoj, já jsem %s, přeji krásné %s'
withArgsRev: 'Krásné %2$s, já jsem %1$s'
plural:
	zero: 'žádný člověk'
	one: 'jeden člověk'
	few: '%d lidé'
	other: '%d lidí'
next: 'This is next translation'

Translatings

For translator to be in Presenter, simply inject it or get in in constructor

<?php
use Nette\DI\Attributes\Inject;

class Presenter extends Nette\Application\UI\Presenter {
    #[Inject]
	public Nette\Localization\Translator $translator;
}

Translating in Presenters

	$changes = $this->model->doSomeChanges();
	$this->flashMessage($this->translator->translate('messages.flash.success', $changes));
	
	$form->addError($this->translator->translate('messages.error.form.empty'));

If you want do shortcut, you can define own translate method, like this

class Presenter extends Nette\Application\UI\Presenter {
	#[Inject]
	public Nette\Localization\Translator $translator;

	public function translate($message, ...$params){
		$this->translator->translate($message, ...$params);
	}

	public function renderTest(){
		$message = $this->translate('messages.test');
	}
}

Translating in Templates

If you set injectLatte to true, translator will autoregister itself to Latte, so you can use it in templates

	<div>{_'messages.test'}</div>

Every parameter you pass to the translate will be passed into sprintf in Bckp\Translator internally, so you can add order in neon translation format.

See (https://latte.nette.org/en/tags#toc-translation) for more informations about how to translate

See (https://github.com/bckp/translator-core) for more informations about Bckp\Translate-core

  • v2.0.0-rc1 Release v2.0.0-rc1

    What's Changed

    • rewritten to PHP 8.2+
    • require nette 3.2+
    • require translator-core 2.0+
    • require latte 3+
    • new config options (BC break)

    Full Changelog: v1.0...v2.0.0-rc1

  • v1.0 Initial release

    First public release

Componette Componette felix@nette.org