Translation2_Admin is a class meant to help with translation management (add/remove a language, add/remove a string).
Translation2 can use different storage containers. Have a look in the /docs/examples dir of the package for some example setups (gettext ini files, SQL DDLs, PHP configuration files, etc).
This simple example will show how you can add a new language [addLang()], using the MDB2 driver:
<?php
// set the parameters to connect to your db
$dbinfo = array(
'hostspec' => 'host',
'database' => 'dbname',
'phptype' => 'mysql',
'username' => 'user',
'password' => 'pwd'
);
// tell Translation2 about your db-tables structure,
// if it's different from the default one.
$params = array(
'langs_avail_table' => 'langs_avail',
'lang_id_col' => 'id',
'lang_name_col' => 'name',
'lang_meta_col' => 'meta',
'lang_errmsg_col' => 'error_text',
'lang_encoding_col' => 'encoding',
'strings_tables' => array(
'it' => 'i18n',
'de' => 'i18n'
),
//OR, if you use only one table,
//'strings_default_table' => 'i18n',
'string_id_col' => 'id',
'string_page_id_col' => 'page_id',
'string_page_id_col_length' => 50, // db field size
'string_text_col' => '%s' //'%s' will be replaced by the lang code
);
$driver = 'MDB2';
require_once 'Translation2/Admin.php';
$tr =& Translation2_Admin::factory($driver, $dbinfo, $params);
// 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' => 'iso-8859-1',
);
$tr->addLang($newLang);
?>
That's it. If you specified a new table, it will be created, if you used the same table name as the one of your other translations, it will be ALTERed to host the new language too.
This simple example will show how you can update an existing language [updateLang()]:
<?php
// 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' => 'iso-8859-15',
);
$tr->updateLang($langData);
?>
If you want to remove all the translated strings and the info for a certain language, all you have to do is
<?php
$tr->removeLang('fr');
?>
removeLang()
can accept a 2nd parameter ($force
):
if you want to remove the whole strings table (regardless it being used
for other languages as well), you can do it this way:
<?php
$tr->removeLang('fr', true);
?>
Be warned it won't do any check, so you're responsible for what you do ;-)
Now let's see how we can add() a new translation for a new or an existing string.
<?php
$stringArray = array(
'en' => 'sample',
'it' => 'esempio',
);
// add the English and Italian translations associated to
// the 'smallTest' stringID and to the 'testGroup' pageID
$tr->add('smallTest', 'testGroup', $stringArray);
?>
You can remove() the translations for a certain stringID:
<?php
$tr->remove('smallTest', 'testGroup');
?>