PHPackages                             vladahejda/livetranslator - 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. vladahejda/livetranslator

ActiveLibrary

vladahejda/livetranslator
=========================

Translator for Nette (PHP framework).

0.9.0(12y ago)147.2k14[10 issues](https://github.com/VladaHejda/LiveTranslator/issues)[1 PRs](https://github.com/VladaHejda/LiveTranslator/pulls)1BSD-3-ClausePHPPHP &gt;=5.3.0

Since Nov 15Pushed 9y ago7 watchersCompare

[ Source](https://github.com/VladaHejda/LiveTranslator)[ Packagist](https://packagist.org/packages/vladahejda/livetranslator)[ RSS](/packages/vladahejda-livetranslator/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (5)Used By (1)

[![Latest Stable Version](https://camo.githubusercontent.com/480425456735d100f1014b2904caa0db1f1ae451b893df2401e670bbdb6fba1b/68747470733a2f2f706f7365722e707567782e6f72672f766c61646168656a64612f6c6976657472616e736c61746f722f762f737461626c652e706e67)](https://packagist.org/packages/vladahejda/livetranslator)[![Total Downloads](https://camo.githubusercontent.com/550ef10d8c426592c2275a162424604dfcba1de7422e9d193b33ab0e34fef785/68747470733a2f2f706f7365722e707567782e6f72672f766c61646168656a64612f6c6976657472616e736c61746f722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/vladahejda/livetranslator)[![Montly Downloads](https://camo.githubusercontent.com/08b29d93bb28e17cbaab340e88d9638cceef2ab74034f3c674415fb5fb3904b0/68747470733a2f2f706f7365722e707567782e6f72672f766c61646168656a64612f6c6976657472616e736c61746f722f642f6d6f6e74686c792e706e67)](https://packagist.org/packages/vladahejda/livetranslator)[![Daily Downloads](https://camo.githubusercontent.com/2ab01b06a928f1bf8a776fbebed86919669f1c6076f156015ba3535c8f9ed979/68747470733a2f2f706f7365722e707567782e6f72672f766c61646168656a64612f6c6976657472616e736c61746f722f642f6461696c792e706e67)](https://packagist.org/packages/vladahejda/livetranslator)

[![Build Status](https://camo.githubusercontent.com/472f6bf5ee39fbfd9aa7f2d8381d7a4e622711a434a73596078dc4c59752a9df/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f566c61646148656a64612f4c6976655472616e736c61746f722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/VladaHejda/LiveTranslator)

[DEMO](http://livetranslator.hejdav.cz/)

LiveTranslator
==============

[](#livetranslator)

LiveTranslator is tool for [Nette Framework](http://nette.org/en/).

LiveTranslator enables simple and user friendly localization of your web, by letting you to translate your texts via panel in debug bar. Works with the Nette 2.

*LiveTranslator is forked from [NetteTranslator](https://github.com/straiki/NetteTranslator), uses its robust parts (especially [TranslationPanel](http://forum.nette.org/cs/4399-nette-translation-panel-preklady-primo-v-prohlizeci)) and changes other, mainly the storage where translations are saved.*

Installation
------------

[](#installation)

- Download from Github:
- or better use [Composer](http://getcomposer.org/doc/00-intro.md#declaring-dependencies):

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

Then load classes via autoloader ([composer autoloading](http://getcomposer.org/doc/01-basic-usage.md#autoloading)or Nette RobotLoader).

Usage
-----

[](#usage)

To launch the translator follow these steps:

### 1. prepare storage

[](#1-prepare-storage)

- **I am using Nette Database**

Execute SQL script in `LiveTranslator/Storage/NetteDatabase.createTable.sql` (or its namespaced version, see [using namespaces](#using-namespaces)) at your database.

Open your configuration file and add service:

```
services:
	translatorStorage: LiveTranslator\Storage\NetteDatabase(localization_text, localization)

```

*You can rename tables in SQL script (use the same names in config file).*

- **I am using [Dibi](http://dibiphp.com/)**

Dibi storage is in progress. You can write your own storage. See below.

- **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.

### 2. add LiveTranslator and Panel service

[](#2-add-livetranslator-and-panel-service)

Into your config file add two more services `LiveTranslator\Translator` and `LiveTranslator\Panel` and define the default language (it is language which in your web is written basically):

```
nette:
	debugger:
		bar:
			- LiveTranslator\Panel
services:
	translator: LiveTranslator\Translator(en)
	translatorPanel: LiveTranslator\Panel

```

### 3. set up your BasePresenter

[](#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;
	}
}
```

### 4. mark texts for translation

[](#4-mark-texts-for-translation)

In presenters call `$this->translator->translate('text to translate')`, in latte use underscore macro `{_ 'text to translate'}`

Done. In development mode look at your debugbar. You can see panel "translations". Browse your web and texts will occurs in panel. Switch languages and translate!

Advanced
--------

[](#advanced)

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

### Say translator which languages you use

[](#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)

```

Now your panel will display links to switch the languages!

### Use skills of `sprintf`

[](#use-skills-of-sprintf)

If you give more arguments to translate method, it will be handed to php function [sprintf](http://php.net/manual/en/function.sprintf.php).

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)

[](#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
