function testUnknownType() { try { MessageFormat::get("{0,wopple}", array("111111111")); $this->fail("Since dev mode is on, an unknown formatter should throw an error."); } catch (Exception $e) { //noop } }
/** * Save untranslated messages to the catalogue. */ public static function saveMessages() { static $onceonly = true; if (!is_null(self::$formatter)) { if ($onceonly) { $app = pradoGetApplication()->getGlobalization(); if (isset($app->Translation['autosave'])) { $catalogue = null; if (isset($app->Translation['catalogue'])) { $catalogue = $app->Translation['catalogue']; } $auto = $app->Translation['autosave']; if ($auto == 'true') { self::$formatter->getSource()->save($catalogue); } else { self::$formatter->getSource()->setCulture($app->getDefaultCulture()); self::$formatter->getSource()->save($auto); } } } $onceonly = false; } }
function testDirectoryTypeSaveUpdateDelete() { $MObackup = './messages/en_AU/tests.mo.bak'; $MOfile = './messages/en_AU/tests.mo'; $PObackup = './messages/en_AU/tests.po.bak'; $POfile = './messages/en_AU/tests.po'; //restore using the back file copy($MObackup, $MOfile); copy($PObackup, $POfile); //test that the back file doesn't contain the 'Testing123' string. $this->assertNoUnwantedPattern('/Testing123/', file_get_contents($MOfile)); $this->assertNoUnwantedPattern('/Testing123/', file_get_contents($POfile)); $source = MessageSource::factory($this->type, $this->source); $source->setCulture('en_AU'); $source->setCache(new MessageCache('./tmp')); $formatter = new MessageFormat($source); //add a untranslated string, note, doesn't matter which catalogue $this->assertEqual($formatter->format('Testing123'), 'Testing123'); //save it to the 'tests' catalgoue $this->assertTrue($formatter->getSource()->save('tests')); //check the contents //$this->assertWantedPattern('/Testing123/',file_get_contents($MOfile)); $this->assertWantedPattern('/Testing123/', file_get_contents($POfile)); //testing for update. Update it to the 'tests' catalogue $this->assertTrue($formatter->getSource()->update('Testing123', '123Test', 'update comments', 'tests')); $this->assertWantedPattern('/123Test/', file_get_contents($MOfile)); //now doing some delete from the 'tests' catalogue $this->assertFalse($formatter->getSource()->delete('Test123', 'tests')); $this->assertTrue($formatter->getSource()->delete('Testing123', 'tests')); $this->assertNoUnwantedPattern('/Testing123/', file_get_contents($MOfile)); $this->assertNoUnwantedPattern('/Testing123/', file_get_contents($POfile)); //restore using the backup file. copy($MObackup, $MOfile); copy($PObackup, $POfile); }
/** * Creates a MessageFormat with the given pattern and uses it to format * the given substitutes. * * This static method is shortcut of: * <code> * (new MessageFormat($pattern)).format($substitutes) * </code> * * @param * string * @param * Array() * @return string */ public static function formatString($pattern, $substitutes = array()) { $msgFormat = new MessageFormat($pattern); return $msgFormat->format($substitutes); }
function testAltCatalogue() { $source = MessageSource::factory($this->type, $this->source); $source->setCulture('en_AU'); $source->setCache(new MessageCache('./tmp')); $formatter = new MessageFormat($source); $formatter->Catalogue = 'tests'; //from a different catalogue $this->assertEqual($formatter->format('Hello'), 'Howdy!'); $this->assertEqual($formatter->format('Welcome'), 'Ho Ho!'); $this->assertEqual($formatter->format('Goodbye'), 'Sayonara'); //switch to 'messages' catalogue $this->assertEqual($formatter->format('Hello', null, 'messages'), 'G\'day Mate!'); }
/** * Get value for for a given key and arguments. This uses message * formatting to substitute arguments or parse simple expressions. * * @param string $key Key to use for message lookup. * @param mixed $arguments Arguments to use for expression evaluation and * substitution. * @param boolean $strict Whether to throw an error when key not found. * * @return string Processed message. */ public function get($key, $arguments = array(), $strict = FALSE) { // convenience, if one arg passed in, need not be array if (is_scalar($arguments)) { $arguments = array($arguments); } $entry = $this->getSimple($key, $strict); if (count($arguments) > 0) { return MessageFormat::get($entry, $arguments, $this->locale); } return $entry; }
/** * Apply format to argument * * @param var fmt * @param var argument * @return string */ public function apply($fmt, $argument) { static $instance; static $level = 0; if (FALSE === ($p = strpos($fmt, '{'))) { return $fmt; } if (!isset($instance)) { $instance = MessageFormat::getInstance(); } if (!is_array($argument)) { $argument = array($argument); } $level++; // Loop while {'s can be found $result = ''; do { $result .= substr($fmt, 0, $p); // Find corresponding closing bracket $index = $rest = FALSE; $c = 0; for ($i = $p, $l = strlen($fmt); $i < $l; $i++) { switch ($fmt[$i]) { case '{': $c++; break; case '}': if (0 >= --$c) { $index = substr($fmt, $p + 1, $i - $p - 1); $fmt = substr($fmt, $i + 1); break 2; } break; } } // No closing bracket found if (FALSE === $index) { trigger_error(sprintf('Opening bracket found at position %d of "%s"', $p, $fmt), E_USER_NOTICE); throw new FormatException('Parse error [level ' . $level . ']: closing curly bracket not found'); } // Syntax: {2} = paste argument, {2,printf,%s} use formatter if (FALSE !== strpos($index, ',')) { list($index, $type, $param) = explode(',', $index, 3); } else { $type = $param = NULL; } // Check argument index if (!isset($argument[$index])) { throw new FormatException('Missing argument at index ' . $index); } // Default if (NULL == $type) { $result .= is_object($argument[$index]) && method_exists($argument[$index], 'toString') ? $argument[$index]->toString() : $argument[$index]; continue; } // No formatter registered if (!$this->hasFormatter($type)) { throw new FormatException('Unknown formatter "' . $type . '"'); } // Formatters return FALSE to indicate failure if (FALSE === ($format = $this->formatters[$type]->apply($param, $argument[$index]))) { return FALSE; } // Look to see if a formatstring was returned if (FALSE !== strpos($format, '{')) { $format = $instance->apply($format, $argument[$index]); } // Append formatter's result $result .= $format; } while (FALSE !== ($p = strpos($fmt, '{'))); return $result . $fmt; }