System.I18N.core
[ class tree: System.I18N.core ] [ index: System.I18N.core ] [ all elements ]

Class: MessageSource

Source Location: /I18N/core/MessageSource.php

Class Overview


Abstract MessageSource class.

Class Details

[line 72]
Abstract MessageSource class.

The base class for all MessageSources. Message sources must be instantiated using the factory method. The default valid sources are

  • XLIFF -- using XML XLIFF format to store the translation messages.
  • SQLite -- Store the translation messages in a SQLite database.
  • MySQL -- Using a MySQL database to store the messages.
  • gettext -- Translated messages are stored in the gettext format.
A custom message source can be instantiated by specifying the filename parameter to point to the custom class file. E.g.
  1. $resource = '...'; //custom message source resource
  2. $classfile = '../MessageSource_MySource.php'; //custom message source
  3. $source = MessageSource::factory('MySource', $resource, $classfile);

If you are writting your own message sources, pay attention to the loadCatalogue method. It details how the resources are loaded and cached. See also the existing message source types as examples.

The following example instantiates a MySQL message source, set the culture, set the cache handler, and use the source in a message formatter. The messages are store in a database named "messages". The source parameter for the actory method is a PEAR DB style DSN.

  1. $dsn = 'mysql://username:password@localhost/messages';
  2. $source = MessageSource::factory('MySQL', $dsn);
  3.  
  4. //set the culture and cache, store the cache in the /tmp directory.
  5. $source->setCulture('en_AU')l
  6. $source->setCache(new MessageCache('/tmp'));
  7.  
  8. $formatter = new MessageFormat($source);




abstract:  
version:  v1.0, last update on Fri Dec 24 19:55:49 EST 2004
author:  Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>


[ Top ]

Variables

Methods


Child classes:

MessageSource_gettext
MessageSource_gettext class.
MessageSource_MySQL
MessageSource_MySQL class.
MessageSource_SQLite
MessageSource_SQLite class.
MessageSource_XLIFF
MessageSource_XLIFF class.

Class Variables

$cache =

[line 96]

The translation cache.



access:  protected

Type:   MessageCache


[ Top ]

$culture =

[line 78]

The culture name for this message source.



access:  protected

Type:   string


[ Top ]

$messages = array()

[line 84]

Array of translation messages.



access:  protected

Type:   array


[ Top ]

$source =

[line 90]

The source of message translations.



access:  protected

Type:   string


[ Top ]

$untranslated = array()

[line 98]



access:  protected

Type:   mixed


[ Top ]



Class Methods


method append [line 243]

void append( string $message)

Add a untranslated message to the source. Need to call save() to save the messages to source.



access:  public


Parameters:

string   $message   message to add

[ Top ]

method factory [line 126]

MessageSource &factory( string $type, [string $source = '.'], [string $filename = ''])

Factory method to instantiate a new MessageSource depending on the

source type. The allowed source types are 'XLIFF', 'SQLite', 'MySQL', and 'gettext'. The source parameter is dependent on the source type. For 'gettext' and 'XLIFF', it should point to the directory where the messages are stored. For database types, e.g. 'SQLite' and 'MySQL', it should be a PEAR DB style DSN string.

Custom message source are possible by supplying the a filename parameter in the factory method.




return:  a new message source of the specified type.
static:  


Parameters:

string   $type   the message source type.
string   $source   the location of the resource.
string   $filename   the filename of the custom message source.

[ Top ]

method getCache [line 224]

MessageCache getCache( )

Get the cache handler for this source.



return:  cache handler
access:  public


[ Top ]

method getCatalogueList [line 314]

array getCatalogueList( string $catalogue)

Get all the variants of a particular catalogue.

This method must be implemented by subclasses.




return:  list of all variants for this catalogue.
access:  protected


Overridden in child classes as:

MessageSource_gettext::getCatalogueList()
Get all the variants of a particular catalogue.
MessageSource_MySQL::getCatalogueList()
Get all the variants of a particular catalogue.
MessageSource_SQLite::getCatalogueList()
Get all the variants of a particular catalogue.
MessageSource_XLIFF::getCatalogueList()
Get all the variants of a particular catalogue.

Parameters:

string   $catalogue   catalogue name

[ Top ]

method getCulture [line 262]

string getCulture( )

Get the culture identifier for the source.



return:  culture identifier.
access:  public


[ Top ]

method getLastModified [line 272]

int getLastModified( string $source)

Get the last modified unix-time for this particular catalogue+variant.



return:  last modified in unix-time format.
access:  protected


Overridden in child classes as:

MessageSource_gettext::getLastModified()
Get the last modified unix-time for this particular catalogue+variant.
MessageSource_MySQL::getLastModified()
Get the last modified unix-time for this particular catalogue+variant.
MessageSource_SQLite::getLastModified()
Get the last modified unix-time for this particular catalogue+variant.
MessageSource_XLIFF::getLastModified()
Get the last modified unix-time for this particular catalogue+variant.

Parameters:

string   $source   catalogue+variant

[ Top ]

method getSource [line 293]

string getSource( string $variant)

Get the source, this could be a filename or database ID.



return:  the resource key
access:  protected


Overridden in child classes as:

MessageSource_gettext::getSource()
Get the MO file for a specific message catalogue and cultural vairant.
MessageSource_XLIFF::getSource()
Get the XLIFF file for a specific message catalogue and cultural vairant.

Parameters:

string   $variant   catalogue+variant

[ Top ]

method isValidSource [line 303]

boolean isValidSource( string $source)

Determine if the source is valid.



return:  true if valid, false otherwise.
access:  protected


Overridden in child classes as:

MessageSource_gettext::isValidSource()
Determin if the MO file source is valid.
MessageSource_MySQL::isValidSource()
Check if a particular catalogue+variant exists in the database.
MessageSource_SQLite::isValidSource()
Check if a particular catalogue+variant exists in the database.
MessageSource_XLIFF::isValidSource()
Determin if the XLIFF file source is valid.

Parameters:

string   $source   catalogue+variant

[ Top ]

method load [line 168]

boolean load( [string $catalogue = 'messages'])

Load a particular message catalogue. Use read() to to get the array of messages. The catalogue loading sequence is as follows

  • [1] call getCatalogeList($catalogue) to get a list of variants for for the specified $catalogue.
  • [2] for each of the variants, call getSource($variant) to get the resource, could be a file or catalogue ID.
  • [3] verify that this resource is valid by calling isValidSource($source)
  • [4] try to get the messages from the cache
  • [5] if a cache miss, call load($source) to load the message array
  • [6] store the messages to cache.
  • [7] continue with the foreach loop, e.g. goto [2].




return:  true if loaded, false otherwise.
see:  MessageSource::read()


Parameters:

string   $catalogue   a catalogue to load

[ Top ]

method loadData [line 283]

array &loadData( string $variant)

Load the message for a particular catalogue+variant.

This methods needs to implemented by subclasses.




return:  of translation messages.
access:  protected


Overridden in child classes as:

MessageSource_gettext::loadData()
Load the messages from a MO file.
MessageSource_MySQL::loadData()
Get an array of messages for a particular catalogue and cultural variant.
MessageSource_SQLite::loadData()
Get an array of messages for a particular catalogue and cultural variant.
MessageSource_XLIFF::loadData()
Load the messages from a XLIFF file.

Parameters:

string   $variant   catalogue+variant.

[ Top ]

method read [line 215]

array read( parameter 0)

Get the array of messages.



return:  translation messages.
access:  public


Parameters:

parameter   0  

[ Top ]

method setCache [line 233]

void setCache( MessageCache $cache)

Set the cache handler for caching the messages.



access:  public


Parameters:

MessageCache   $cache   the cache handler.

[ Top ]

method setCulture [line 253]

void setCulture( string $culture)

Set the culture for this message source.



access:  public


Parameters:

string   $culture   culture name

[ Top ]


© 2004-2005 PRADO Software Group. All Rights Reserved.