Containers Overview

Containers Overview -- Overview of the different Translation2 containers

Summary

Translation2 supports different storage drivers; this page is meant to highlight the differences among them.

PEAR::DB, PEAR::MDB, PEAR::MDB2

Translation2 can work with any of these database abstraction layers, just pass the appropriate connection options. These three containers are absolutely identical in what they do and in how they work (wrt Translation2, of course).
// connection options
$dbinfo = array(
    'hostspec' => 'host',
    'database' => 'dbname',
    'phptype'  => 'mysql',
    'username' => 'user',
    'password' => 'pwd'
);
//select the preferred driver
$driver = 'MDB2'; //switch to 'DB' or 'MDB' as needed

require_once 'Translation2.php';
$tr = new Translation2($driver, $dbinfo, $params);
If your table definition is different from the default one, you need to specify it in the $params array.

DB_DataObject

The dataobjectsimple container is the natural choice for those using DB_DataObject, as it is tightly tied to the DAO. This storage driver can use all databases supported by the PEAR::DB abstraction layer to fetch data.

For this container, you can't specify a custom table definition, since this feature is not supported yet. You must create a table with the following structure:
// meta data etc. not supported
table: translations
id          // not null primary key autoincrement..
string_id   // translation id
page        // indexed varchar eg. (mytemplate.html)
lang        // index varchar (eg. en|fr|.....)
translation // the translated value in language lang.
Here's the MySQL query that can be used to create the table:
create table translations (
  id int(11) auto_increment not null primary key,
  string_id int(11), page varchar(128),
  lang varchar(10), translation text
);
alter table translations add index page (page);
alter table translations add index lang (lang);
alter table translations add index string_id (string_id);
then just run the dataobjects createtables script.

Gettext

This is a wrapper around the gettext base functions, and thanks to File_Gettext you can retrieve an entire domain or write to an existing/new domain without calling the command line compiler utility.

The connection parameters are a bit different from the db ones. To make things as simple as possible, the domain definitions and the available language list are read from two .ini files. langs.ini example:
[en]
use = en_US

[en_US]
id = en
name = English
meta = iso-8859-1
error_text = not available in English
windows = enu

[en_GB]
id = en_GB
name = English
meta = iso-8859-1
error_text = not available in English
windows = eng

[it]
id = it
name = italiano
meta = iso-8859-1
error_text = non disponibile in Italiano
windows = ita

[de]
id = de
name = deutsch
meta = iso-8859-1
error_text = not available
windows = deu
domains.ini example:
messages = /path/to/locale
2nddomain = /path/to/locale
3rddomain = /path/to/locale

Sample code to use Translation2 with the gettext container:
require_once 'Translation2.php';

$params = array(
    'langs_avail_file'  => 'path/to/langs.ini',
    'domains_path_file' => 'path/to/domains.ini',
    'default_domain'    => 'messages',
);

$tr = new Translation2('gettext', null, $params);

$tr->setLang('en');
echo $tr->get('mystring');
print_r($tr->getPage('3rddomain'));