Class MessageSource

Description

Implements interfaces:

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>

Located in /I18N/core/MessageSource.php (line 72)


	
			
Direct descendents
Class Description
MessageSource_gettext MessageSource_gettext class.
MessageSource_MySQL MessageSource_MySQL class.
MessageSource_SQLite MessageSource_SQLite class.
MessageSource_XLIFF MessageSource_XLIFF class.
Variable Summary
Method Summary
void append (string $message)
MessageSource &factory (string $type, [string $source = '.'], [string $filename = ''])
array getCatalogueList (string $catalogue)
string getCulture ()
int getLastModified (string $source)
string getSource (string $variant)
boolean isValidSource (string $source)
boolean load ([string $catalogue = 'messages'])
array &loadData (string $variant)
array read (parameter 0)
void setCache (MessageCache $cache)
void setCulture (string $culture)
Variables
MessageCache $cache (line 96)

The translation cache.

  • access: protected
string $culture (line 78)

The culture name for this message source.

  • access: protected
array $messages = array() (line 84)

Array of translation messages.

  • access: protected
string $source (line 90)

The source of message translations.

  • access: protected

Redefined in descendants as:
mixed $untranslated = array() (line 98)
  • access: protected
Methods
append (line 243)

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

  • access: public
void append (string $message)
  • string $message: message to add
factory (line 126)

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:
  • throws: InvalidMessageSourceTypeException
MessageSource &factory (string $type, [string $source = '.'], [string $filename = ''])
  • string $type: the message source type.
  • string $source: the location of the resource.
  • string $filename: the filename of the custom message source.
getCache (line 224)

Get the cache handler for this source.

  • return: cache handler
  • access: public
MessageCache getCache ()
getCatalogueList (line 314)

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
array getCatalogueList (string $catalogue)
  • string $catalogue: catalogue name

Redefined in descendants as:
getCulture (line 262)

Get the culture identifier for the source.

  • return: culture identifier.
  • access: public
string getCulture ()
getLastModified (line 272)

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

  • return: last modified in unix-time format.
  • access: protected
int getLastModified (string $source)
  • string $source: catalogue+variant

Redefined in descendants as:
getSource (line 293)

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

  • return: the resource key
  • access: protected
string getSource (string $variant)
  • string $variant: catalogue+variant

Redefined in descendants as:
isValidSource (line 303)

Determine if the source is valid.

  • return: true if valid, false otherwise.
  • access: protected
boolean isValidSource (string $source)
  • string $source: catalogue+variant

Redefined in descendants as:
load (line 168)

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].

boolean load ([string $catalogue = 'messages'])
  • string $catalogue: a catalogue to load
loadData (line 283)

Load the message for a particular catalogue+variant.

This methods needs to implemented by subclasses.

  • return: of translation messages.
  • access: protected
array &loadData (string $variant)
  • string $variant: catalogue+variant.

Redefined in descendants as:
read (line 215)

Get the array of messages.

  • return: translation messages.
  • access: public
array read (parameter 0)
  • parameter 0
setCache (line 233)

Set the cache handler for caching the messages.

  • access: public
void setCache (MessageCache $cache)
setCulture (line 253)

Set the culture for this message source.

  • access: public
void setCulture (string $culture)
  • string $culture: culture name

Documentation generated on Sun, 04 Jun 2006 18:58:28 -0400 by phpDocumentor 1.3.0RC4