Class \Prado\I18N\core\MessageSource
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.
PHP -- using PHP arrays to store the translation messages.
gettext -- Translated messages are stored in the gettext format.
Database -- Use an existing TDbConnection to store the messages.
A custom message source can be instantiated by specifying the filename parameter to point to the custom class file. E.g.
$resource = '...'; //custom message source resource
$classfile = '../MessageSource_MySource.php'; //custom message source
$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 Database message source, set the culture, set the cache handler, and use the source in a message formatter. The messages are stored using an existing connection. The source parameter for the factory method must contain a valid ConnectionID.
// db1 must be already configured
$source = MessageSource::factory('Database', 'db1');
//set the culture and cache, store the cache in the /tmp directory.
$source->setCulture('en_AU')l
$source->setCache(new MessageCache('/tmp'));
$formatter = new MessageFormat($source);
Class hierarchy
- \Prado\I18N\core\MessageSource implements IMessageSource
public
|
append(string $message) : mixed
Add a untranslated message to the source. Need to call save()
to save the messages to source.
|
public
static
|
factory(string $type[, string $source = '.' ][, string $filename = '' ]) : MessageSource
Factory method to instantiate a new MessageSource depending on the
source type. The allowed source types are 'XLIFF', 'PHP', 'gettext' and
'Database'. The source parameter depends on the source type.
For 'gettext', 'PHP' and 'XLIFF', 'source' should point to the directory where the messages are stored. For 'Database', 'source' must be a valid connection id. Custom message source are possible by supplying the a filename parameter in the factory method. |
public
|
|
public
|
|
public
|
load([string $catalogue = 'messages' ]) : bool
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 ofvariants 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]. |
public
|
|
public
|
|
public
|
|
protected
|
getCatalogueList(string $catalogue) : array<string|int, mixed>
Get all the variants of a particular catalogue.
This method must be implemented by subclasses. |
protected
|
getLastModified(string $source) : int
Get the last modified unix-time for this particular catalogue+variant.
|
protected
|
|
protected
|
|
protected
|
loadData(string $variant) : array<string|int, mixed>
Load the message for a particular catalogue+variant.
This methods needs to implemented by subclasses. |
private
|
__construct() : mixed
Private constructor. MessageSource must be initialized using
the factory method.
|
|