Admin Introduction

Admin Introduction

Admin Introduction -- Usage of Translation2_Admin

What is Translation2_Admin?

Translation2_Admin is a class meant to help with translation management (add/remove a language, add/remove a string).

Adding a new language

This simple example will show how you can add a new language [addLang()], using the MDB2 driver:
// 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_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.

Updating a language

This simple example will show how you can update an existing language [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'   => 'iso-8859-15',
);

$tr->updateLang($langData);

Removing an existing language

If you want to remove all the translated strings and the info for a certain language, all you have to do is
$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:
$tr->removeLang('fr', true);
Be warned it won't do any check, so you're responsible for what you do ;-)

Adding new translations

Now let's see how we can add() a new translation for a new or an existing string.
$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);

Remove a translation

You can remove() the translations for a certain stringID:
$tr->remove('smallTest', 'testGroup');

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.