function get_source($temp1, $temp2) { $source1 = sfMessageSource::factory('XLIFF', $temp1); $source2 = sfMessageSource::factory('XLIFF', $temp2); return sfMessageSource::factory('Aggregate', array($source1, $source2)); }
/** * Do string translation. * * @param string $string the string to translate. * @param array $args a list of string to substitute. * @param string $catalogue get the translation from a particular message catalogue. * @return string translated string. */ protected function formatString($string, $args = array(), $catalogue = null) { if (empty($args)) { $args = array(); } if (empty($catalogue)) { $catalogue = empty($this->catalogue) ? 'messages' : $this->catalogue; } $this->loadCatalogue($catalogue); foreach ($this->messages[$catalogue] as $variant) { // we found it, so return the target translation if (isset($variant[$string])) { $target = $variant[$string]; // check if it contains only strings. if (is_array($target)) { $target = array_shift($target); } // found, but untranslated if (empty($target)) { return $this->postscript[0] . $this->replaceArgs($string, $args) . $this->postscript[1]; } return $this->replaceArgs($target, $args); } } // well we did not find the translation string. $this->source->append($string); return $this->postscript[0] . $this->replaceArgs($string, $args) . $this->postscript[1]; }
public function setCulture($culture) { parent::setCulture($culture); foreach ($this->messageSources as $messageSource) { $messageSource->setCulture($culture); } }
/** * @see sfI18nExtract */ protected function loadMessageSources() { $opt = $this->i18n->getOptions(); $this->messageSource = sfMessageSource::factory($opt['source'], $this->parameters['path'] . '/i18n'); $this->messageSource->setCulture($this->culture); $this->messageSource->load(); }
function init_fixtures($temp) { $db = sqlite_open($temp); sqlite_query('CREATE TABLE catalogue (cat_id INTEGER PRIMARY KEY, name VARCHAR NOT NULL, source_lang VARCHAR, target_lang VARCHAR, date_created INT, date_modified INT, author VARCHAR);', $db); sqlite_query('CREATE TABLE trans_unit (msg_id INTEGER PRIMARY KEY, cat_id INTEGER NOT NULL DEFAULT \'1\', id VARCHAR, source TEXT, target TEXT, comments TEXT, date_added INT, date_modified INT, author VARCHAR, translated INT(1) NOT NULL DEFAULT \'0\');', $db); sqlite_query("INSERT INTO catalogue (cat_id, name) VALUES (1, 'messages.fr_FR')", $db); sqlite_query("INSERT INTO catalogue (cat_id, name) VALUES (2, 'messages.it')", $db); sqlite_query("INSERT INTO trans_unit (msg_id, cat_id, id, source, target, translated) VALUES (1, 1, 1, 'an english sentence', 'une phrase en français', 1)", $db); sqlite_query("INSERT INTO trans_unit (msg_id, cat_id, id, source, target, translated) VALUES (2, 1, 2, 'another english sentence', 'une autre phrase en français', 1)", $db); sqlite_close($db); return sfMessageSource::factory('SQLite', 'sqlite://localhost/' . $temp); }
/** * Do string translation. * * @param string the string to translate. * @param array a list of string to substitute. * @param string get the translation from a particular message catalogue. * @return string translated string. */ protected function formatString($string, $args = array(), $catalogue = null) { if (empty($args)) { $args = array(); } $target = $this->getFormattedString($string, $args, $catalogue); // well we did not find the translation string. if (!$target) { $this->source->append($string); $target = $this->postscript[0] . $this->replaceArgs($string, $args) . $this->postscript[1]; } return $target; }
public function createMessageSource($dir) { if (in_array(sfConfig::get('sf_i18n_source'), array('Creole', 'MySQL', 'SQLite'))) { $messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), sfConfig::get('sf_i18n_database', 'default')); } else { $messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), $dir); } if (sfConfig::get('sf_i18n_cache')) { $subdir = str_replace(str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_root_dir')), '', $dir); $cacheDir = str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_i18n_cache_dir') . $subdir); $cache = new sfMessageCache(); $cache->initialize(array('cacheDir' => $cacheDir, 'lifeTime' => 86400)); $messageSource->setCache($cache); } return $messageSource; }
function init_fixtures($temp) { $queries = array('CREATE TABLE catalogue (cat_id INTEGER PRIMARY KEY, name VARCHAR NOT NULL, source_lang VARCHAR, target_lang VARCHAR, date_created INT, date_modified INT, author VARCHAR);', 'CREATE TABLE trans_unit (msg_id INTEGER PRIMARY KEY, cat_id INTEGER NOT NULL DEFAULT \'1\', id VARCHAR, source TEXT, target TEXT, comments TEXT, date_added INT, date_modified INT, author VARCHAR, translated INT(1) NOT NULL DEFAULT \'0\');', "INSERT INTO catalogue (cat_id, name) VALUES (1, 'messages.fr_FR')", "INSERT INTO catalogue (cat_id, name) VALUES (2, 'messages.it')", "INSERT INTO trans_unit (msg_id, cat_id, id, source, target, translated) VALUES (1, 1, 1, 'an english sentence', 'une phrase en français', 1)", "INSERT INTO trans_unit (msg_id, cat_id, id, source, target, translated) VALUES (2, 1, 2, 'another english sentence', 'une autre phrase en français', 1)"); if (version_compare(PHP_VERSION, '5.3', '>')) { $db = new SQLite3($temp); foreach ($queries as $query) { $db->exec($query); } $db->close(); } else { $db = sqlite_open($temp); foreach ($queries as $query) { sqlite_query($query, $db); } sqlite_close($db); } return sfMessageSource::factory('SQLite', 'sqlite://localhost/' . $temp); }
/** * @see sfTask */ public function execute($arguments = array(), $options = array()) { $this->logSection('i18n', sprintf('Rectifying existing i18n strings for the "%s" application', $options['application'])); // get i18n configuration from factories.yml $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml')); $class = $config['i18n']['class']; $params = $config['i18n']['param']; unset($params['cache']); // Get current (saved) messages from ALL sources (app and plugin) $this->i18n = new $class($this->configuration, new sfNoCache(), $params); $this->i18n->getMessageSource()->setCulture($arguments['culture']); $this->i18n->getMessageSource()->load(); $currentMessages = array(); foreach ($this->i18n->getMessageSource()->read() as $catalogue => $translations) { foreach ($translations as $key => $value) { // Use first message that has a valid translation if (0 < strlen(trim($value[0])) && !isset($currentMessages[$key][0])) { $currentMessages[$key] = $value; } } } // Loop through plugins $pluginNames = sfFinder::type('dir')->maxdepth(0)->relative()->not_name('.')->in(sfConfig::get('sf_plugins_dir')); foreach ($pluginNames as $pluginName) { $this->logSection('i18n', sprintf('rectifying %s plugin strings', $pluginName)); $messageSource = sfMessageSource::factory($config['i18n']['param']['source'], sfConfig::get('sf_plugins_dir') . '/' . $pluginName . '/i18n'); $messageSource->setCulture($arguments['culture']); $messageSource->load(); // If the current plugin source *doesn't* have a translation, then try // and get translated value from $currentMessages foreach ($messageSource->read() as $catalogue => $translations) { foreach ($translations as $key => &$value) { if (0 == strlen(trim($value[0])) && isset($currentMessages[$key])) { $messageSource->update($key, $currentMessages[$key][0], $value[2]); } } } $messageSource->save(); } }
public function setMessageSource($dirs, $culture = null) { $cachedDir = sfConfig::get('sf_app_cache_dir') . DIRECTORY_SEPARATOR . 'i18n'; if (is_file($cachedDir . DIRECTORY_SEPARATOR . 'messages.ja.xml.php')) { $this->messageSource = sfMessageSource::factory('OpenPNECached', $cachedDir); } else { $this->generateApplicationMessages($dirs); if (is_null($dirs)) { $this->messageSource = $this->createMessageSource(); } else { $this->messageSource = sfMessageSource::factory('Aggregate', array_map(array($this, 'createMessageSource'), $dirs)); } } if (!is_null($this->cache)) { $this->messageSource->setCache($this->cache); } if (!is_null($culture)) { $this->setCulture($culture); } else { $this->messageSource->setCulture($this->culture); } $this->messageFormat = null; }
$source = sfMessageSource::factory('XLIFF', $temp); $source->setCulture('fr_FR'); $format = new sfMessageFormat($source); $t->is($format->format('New message'), 'New message', '->save() saves new messages'); // test new culture $source->setCulture('it'); $source->append('New message & <more> (it)'); $source->save(); $source = sfMessageSource::factory('XLIFF', $temp); $source->setCulture('it'); $format = new sfMessageFormat($source); $t->is($format->format('New message & <more> (it)'), 'New message & <more> (it)', '->save() saves new messages'); $source->setCulture('fr_FR'); // ->update() $t->diag('->update()'); $t->is($source->update('New message', 'Nouveau message', ''), true, '->update() returns true if the message has been updated'); $source = sfMessageSource::factory('XLIFF', $temp); $source->setCulture('fr_FR'); $format = new sfMessageFormat($source); $t->is($format->format('New message'), 'Nouveau message', '->update() updates a message translation'); // ->delete() $t->diag('->delete()'); $t->is($source->delete('Non existant message'), false, '->delete() returns false if the message has not been deleted'); $t->is($source->delete('New message'), true, '->delete() returns true if the message has been deleted'); $source = sfMessageSource::factory('XLIFF', $temp); $source->setCulture('fr_FR'); $format = new sfMessageFormat($source); $t->is($format->format('New message'), 'New message', '->delete() deletes a message'); // teardown sfToolkit::clearDirectory($temp); rmdir($temp);
protected $dataExt = '.xml'; function delete($message, $catalogue = 'messages') { } function update($text, $target, $comments, $catalogue = 'messages') { } function save($catalogue = 'messages') { } public function getCatalogueByDir($catalogue) { return parent::getCatalogueByDir($catalogue); } } $source = sfMessageSource::factory('Simple', __DIR__ . '/fixtures'); $source->setCulture('fr_FR'); // ->getCatalogueByDir() $t->diag('->getCatalogueByDir()'); $t->is($source->getCatalogueByDir('messages'), array('fr_FR/messages.xml', 'fr/messages.xml'), '->getCatalogueByDir() returns catalogues by directory'); // ->getCatalogueList() $t->diag('->getCatalogueList()'); $t->is($source->getCatalogueList('messages'), array('fr_FR/messages.xml', 'fr/messages.xml', 'messages.fr_FR.xml', 'messages.fr.xml', 'messages.xml'), '->getCatalogueByDir() returns all catalogues for the current culture'); // ->getSource() $t->diag('->getSource()'); $t->is($source->getSource('fr_FR/messages.xml'), __DIR__ . '/fixtures/fr_FR/messages.xml', '->getSource() returns the full path name to a specific variant'); // ->isValidSource() $t->diag('->isValidSource()'); $t->is($source->isValidSource($source->getSource('fr_FR/messages.xml')), false, '->isValidSource() returns false if the source is not valid'); $t->is($source->isValidSource($source->getSource('messages.fr.xml')), true, '->isValidSource() returns true if the source is valid'); // ->getLastModified()
/** * Returns a new message source. * * @param mixed $dir An array of i18n directories to create a XLIFF or gettext message source, null otherwise * * @return sfMessageSource A sfMessageSource object */ public function createMessageSource($dir = null) { return sfMessageSource::factory($this->options['source'], self::isMessageSourceFileBased($this->options['source']) ? $dir : $this->options['database']); }
* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); $t = new lime_test(3); class sfMessageSource_Simple extends sfMessageSource { function __construct($source) {} function delete($message, $catalogue = 'messages') {} function update($text, $target, $comments, $catalogue = 'messages') {} function catalogues() {} function save($catalogue = 'messages') {} function getId() {} } // ::factory() $t->diag('::factory()'); $source = sfMessageSource::factory('Simple'); $t->ok($source instanceof sfIMessageSource, '::factory() returns a sfMessageSource instance'); // ->getCulture() ->setCulture() $t->diag('->getCulture() ->setCulture()'); $source->setCulture('en'); $t->is($source->getCulture(), 'en', '->setCulture() changes the source culture'); $source->setCulture('fr'); $t->is($source->getCulture(), 'fr', '->getCulture() gets the current source culture');
protected $dataExt = '.xml'; function delete($message, $catalogue = 'messages') { } function update($text, $target, $comments, $catalogue = 'messages') { } function save($catalogue = 'messages') { } public function getCatalogueByDir($catalogue) { return parent::getCatalogueByDir($catalogue); } } $source = sfMessageSource::factory('Simple', dirname(__FILE__) . '/fixtures'); $source->setCulture('fr_FR'); // ->getCatalogueByDir() $t->diag('->getCatalogueByDir()'); $t->is($source->getCatalogueByDir('messages'), array('fr_FR/messages.xml', 'fr/messages.xml'), '->getCatalogueByDir() returns catalogues by directory'); // ->getCatalogueList() $t->diag('->getCatalogueList()'); $t->is($source->getCatalogueList('messages'), array('fr_FR/messages.xml', 'fr/messages.xml', 'messages.fr_FR.xml', 'messages.fr.xml', 'messages.xml'), '->getCatalogueByDir() returns all catalogues for the current culture'); // ->getSource() $t->diag('->getSource()'); $t->is($source->getSource('fr_FR/messages.xml'), dirname(__FILE__) . '/fixtures/fr_FR/messages.xml', '->getSource() returns the full path name to a specific variant'); // ->isValidSource() $t->diag('->isValidSource()'); $t->is($source->isValidSource($source->getSource('fr_FR/messages.xml')), false, '->isValidSource() returns false if the source is not valid'); $t->is($source->isValidSource($source->getSource('messages.fr.xml')), true, '->isValidSource() returns true if the source is valid'); // ->getLastModified()