Skip to content

LiveTranslator is tool for Nette Framework that enables translation of your texts via panel in debug bar.

Notifications You must be signed in to change notification settings

vilgain/LiveTranslator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Total Downloads Montly Downloads Daily Downloads

Build Status

DEMO

LiveTranslator

LiveTranslator is tool for Nette Framework.

LiveTranslator is forked from NetteTranslator.

Installation

{
	"require": {
		"vladahejda/livetranslator": "~1.0"
	},
	"minimum-stability": "RC"
}

Then load classes via autoloader (composer autoloading or Nette RobotLoader).

Usage

To launch the translator follow these steps:

1. prepare storage

  • I have no database

You can store translations into plaintext file. Just add following service into your config and choose persistent and write-accessible (existing) directory:

services:
	translatorStorage: LiveTranslator\Storage\File(%appDir%/../data/localization)
  • I want to save translations elsewhere

Look at the interface LiveTranslator\ITranslatorStorage and implement it to write your own storage.

Then add storage into your configuration file as a service.

3. set up your BasePresenter

Inject LiveTranslator, set current language and give translator to template and forms:

class BasePresenter extends \Nette\Application\UI\Presenter
{
	/** @var string @persistent */
	public $lang = 'en';

	/** @var \LiveTranslator\Translator @inject */
	public $translator;

	// since Nette 2.1 you can omit this method
	public function injectTranslator(\LiveTranslator\Translator $translator)
	{
		$this->translator = $translator;
	}

	public function startup()
	{
		parent::startup();
		$this->translator->setCurrentLang($this->lang);
	}
	
	protected function createTemplate($class = NULL)
	{
		$template = parent::createTemplate($class);
		$template->setTranslator($this->translator);
		return $template;
	}

	// to have translated even forms add this method too
	protected function createComponent($name)
	{
		$component = parent::createComponent($name);
		if ($component instanceof \Nette\Forms\Form) {
			$component->setTranslator($this->translator);
		}
		return $component;
	}
}

Advanced

If you want to use your translator fully, there is some more stuff you would do.

Say translator which languages you use

Call function $translator->setAvailableLanguages() and give the array of languages that your web is available in. Then set the name of presenter language persistent parameter ($translator->setPresenterLanguageParam()).

Better way of this is setting it in config:

	translator:
		class: LiveTranslator\Translator(en)
		setup:
			- setAvailableLanguages([en, de, fr])
			- setPresenterLanguageParam(lang)

Use skills of sprintf

If you give more arguments to translate method, it will be handed to php function sprintf.

That means that $translator->translate('Call me %s.', 'Johan') results in "Call me Johan", whereas "Johan" will not be translated.

It can be used in latte too.

Translate plurals (1 apple → 2 apples)

You can say what plural-form each language uses via setAvailableLanguages, this way:

	setup:
		- setAvailableLanguages([
			en: "nplurals=2; plural=(n==1) ? 0 : 1;",
			cz: "nplurals=3; plural=((n==1) ? 0 : (n>=2 && n<=4 ? 1 : 2));",
		])

(to understand this see plural forms)

More you need to do is to give plural variants of the default language to the translator, in array. And the number. Example: $translator->translate( array( 'There is %d apple', 'There are %d apples' ), 3 ) or in latte: {_ ['There is %d apple', 'There are %d apples'], 3}.

Using namespaces

When there is huge amount of texts at your web, it would be good to sort them somehow. Just give the namespace to the translator dependent for example on module. $translator->setNamespace('products')

And now, enjoy.

Authors

(alphabetic order)

Under New BSD License

About

LiveTranslator is tool for Nette Framework that enables translation of your texts via panel in debug bar.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 97.1%
  • Shell 2.9%