Download the PHP package vladahejda/livetranslator without Composer
On this page you can find all versions of the php package vladahejda/livetranslator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vladahejda/livetranslator
More information about vladahejda/livetranslator
Files in vladahejda/livetranslator
Package livetranslator
Short Description Translator for Nette (PHP framework).
License BSD-3-Clause
Informations about the package livetranslator
LiveTranslator
LiveTranslator is tool for Nette Framework.
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, uses its robust parts (especially TranslationPanel) and changes other, mainly the storage where translations are saved.
Installation
- Download from Github: https://github.com/VladaHejda/LiveTranslator
- or better use Composer:
Then load classes via autoloader (composer autoloading or Nette RobotLoader).
Usage
To launch the translator follow these steps:
1. prepare storage
- I am using Nette Database
Execute SQL script in LiveTranslator/Storage/NetteDatabase.createTable.sql
(or its namespaced version,
see using namespaces) at your database.
Open your configuration file and add service:
You can rename tables in SQL script (use the same names in config file).
- I am using Dibi
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:
- 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
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):
3. set up your BasePresenter
Inject LiveTranslator, set current language and give translator to template and forms:
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
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:
Now your panel will display links to switch the languages!
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:
(to understand this see plural forms)
Then the panel will let you translate the text even in plural. 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')
Panel will separate all texts from another namespaces.
And now, enjoy.
Authors
(alphabetic order)
- Josef Kufner ([email protected])
- Miroslav Paulík (https://github.com/castamir)
- Roman Sklenář (http://romansklenar.cz)
- Miroslav Smetana
- Jan Smitka
- Patrik Votoček ([email protected])
- Tomáš Votruba ([email protected])
- Václav Vrbka ([email protected])
- Vladislav Hejda
Under New BSD License