Containers Overview

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 =& Translation2::factory($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 gettext container requires PEAR::File_Gettext and PEAR::I18Nv2 0.9.1 or newer, make sure you have them installed.

The construction 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:
; If one does not specify the source encoding, ISO-8859-1 is assumed
; Beware that gettext can choke on bad encodings!

[en]
name = English
encoding = iso-8859-1

[de]
name = Deutsch
encoding = iso-8859-1

[it]
name = italiano
encoding = iso-8859-1
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(
	'prefetch'          => false,
    'langs_avail_file'  => 'path/to/langs.ini',
    'domains_path_file' => 'path/to/domains.ini',
    'default_domain'    => 'messages',
    //'file_type'       => 'po',
);

// Better set prefetch to FALSE for the gettext container, so we don't need 
// to read in the whole MO file with File_Gettext on every request.
$tr =& Translation2::factory('gettext', $params);

$tr->setLang('en');

// Note that, if there is no translation available for a string, the gettext 
// container will return the string ID!  This behaviour emulates native gettext.
echo $tr->get('mystring');

print_r($tr->getPage('3rddomain'));

XML

The XML container requires PEAR::XML_Serializer 0.13.0 or newer, make sure you have it installed.
$driver = 'XML';
$options = array(
    'filename'         => 'i18n.xml',
    'save_on_shutdown' => true, //set to FALSE to save in real time
);
require_once 'Translation2.php';
$tr =& Translation2::factory($driver, $options);

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