Prado\I18N\core\MessageSource::factory PHP Method

factory() static public method

For 'gettext' and 'XLIFF', 'source' should point to the directory where the messages are stored. For 'Database', 'source' must be a valid connection id. If a deprecated 'SQLite' type is used, 'source' must contain a valid DSN. Custom message source are possible by supplying the a filename parameter in the factory method.
static public factory ( $type, $source = '.', $filename = '' ) : MessageSource
return MessageSource a new message source of the specified type.
    static function &factory($type, $source = '.', $filename = '')
    {
        $types = array('XLIFF', 'gettext', 'Database', 'SQLite');
        if (empty($filename) && !in_array($type, $types)) {
            throw new Exception('Invalid type "' . $type . '", valid types are ' . implode(', ', $types));
        }
        $class = 'MessageSource_' . $type;
        if (empty($filename)) {
            $filename = dirname(__FILE__) . '/' . $class . '.php';
        }
        if (is_file($filename) == false) {
            throw new Exception("File {$filename} not found");
        }
        include_once $filename;
        $obj = new $class($source);
        return $obj;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Initialize the TTranslate translation components
  */
 public static function init($catalogue = 'messages')
 {
     static $saveEventHandlerAttached = false;
     //initialized the default class wide formatter
     if (!isset(self::$formatters[$catalogue])) {
         $app = Prado::getApplication()->getGlobalization();
         $config = $app->getTranslationConfiguration();
         $source = MessageSource::factory($config['type'], $config['source'], $config['filename']);
         $source->setCulture($app->getCulture());
         if (isset($config['cache'])) {
             $source->setCache(new MessageCache($config['cache']));
         }
         self::$formatters[$catalogue] = new MessageFormat($source, $app->getCharset());
         //mark untranslated text
         if ($ps = $config['marker']) {
             self::$formatters[$catalogue]->setUntranslatedPS(array($ps, $ps));
         }
         //save the message on end request
         // Do it only once !
         if (!$saveEventHandlerAttached && TPropertyValue::ensureBoolean($config['autosave'])) {
             Prado::getApplication()->attachEventHandler('OnEndRequest', array('Translation', 'saveMessages'));
             $saveEventHandlerAttached = true;
         }
     }
 }