Lorenzo Alberton

London, UK   ·   Contact me   ·  

« Articles

PEAR::Translation2 tutorials - "backend" usage

Abstract: This second Translation2 tutorial shows how to add, update and remove languages and translations.


This second Translation2 tutorial shows how to add new languages and new translations, how to update and delete them. I promise you'll master this class in less than 5 minutes. Read on.

Creating a Translation2_Admin object

The Translation2_Admin factory method inherits the Translation2 one, so you can learn about the parameters in the Translation2 frontend tutorial or in the manual. Just use Translation2_Admin instead of Translation2 and you're set:

<?php
require_once 'Translation2/Admin.php';
$tr =& Translation2_Admin::factory($driver, $dbinfo, $params);
?>

Adding a new language

If you want to translate your strings in a new language, the first thing to do is adding some info about the language itself:

// set some info about the new lang
$newLang = array(
    'lang_id'    => 'en',
    'table_name' => 'i18n',
    'name'       => 'english',
    'meta'       => 'some meta info',
    'error_text' => 'not available',
    'encoding'   => 'UTF-8',  // no, really. If you are building a multi-language system, USE UNICODE!!!
);
$tr->addLang($newLang);

Updating the info for an existing language has the same syntax, just use updateLang():

// set some info about the new lang
$langData = array(
    'lang_id'    => 'en',
    'table_name' => 'i18n',
    'name'       => 'English',
    'meta'       => 'some updated meta info',
    'error_text' => 'this text is not available in English',
    'encoding'   => 'UTF-8',
);
$tr->updateLang($langData);

To remove a language and all its translations, pass the lang code to removeLang():

$tr->removeLang('en');

Adding new translations

To add or update the translations for a new or an existing string, you need to call add() setting a stringID, a pageID and an array of (langID, translatedString) pairs:

$stringArray = array(
    'en' => 'hour',
    'it' => 'ora',
    'fr' => 'heure',
);

// add the English, French and Italian translations associated to
// the 'hour' stringID and to the 'time' pageID

$tr->add('hour', 'time', $stringArray);

There's also an update() method, but Translation2 is smart enough to call it automatically when the translation already exists, so you can use add() all the time (one less method to remember...).
To remove the translations for a certain stringID and pageID, use remove():

$tr->remove('hour', 'time');

To remove all the translations in the same group (identified by a certain pageID), use removePage():

$tr->removePage('time');

That's it.

Exactly, that's all you have to know to use Translation2. Can it be easier? I doubt it.
There's one last feature that someone might find useful, i.e. decorators, let's describe it for completeness.

Decorators

Translation2_Admin benefits from the same design of the base class, so it can have decorators too. The official package bundles an Autoadd decorator, that will add strings to a language when a request for them to be translated happens. The 'autoaddlang' option must be set to the language the strings will be added as:

$tr =& Translation2_Admin::factory(...);
$tr->setLang('en');
$tr =& $tr->getAdminDecorator('Autoadd');
$tr->setOption('autoaddlang', 'en');
$tr->get('Entirely new string', 'samplePage', 'de');

'Entirely new string' will be added to the English language table.

OK, our 5 minutes are up, and I think I kept my promise ;-)
If there's something else you need to know about Translation2, drop me a mail, and if the question is interesting enough for a larger audience, I might publish another tutorial.

Cheers!

« Go back to PEAR::Translation2 tutorials index.




Related articles

Latest articles

Filter articles by topic

AJAX, Apache, Book Review, Charset, Cheat Sheet, Data structures, Database, Firebird SQL, Hadoop, Imagick, INFORMATION_SCHEMA, JavaScript, Kafka, Linux, Message Queues, mod_rewrite, Monitoring, MySQL, NoSQL, Oracle, PDO, PEAR, Performance, PHP, PostgreSQL, Profiling, Scalability, Security, SPL, SQL Server, SQLite, Testing, Tutorial, TYPO3, Windows, Zend Framework


Back