本文整理汇总了PHP中JMS\TranslationBundle\Model\MessageCatalogue::setLocale方法的典型用法代码示例。如果您正苦于以下问题:PHP MessageCatalogue::setLocale方法的具体用法?PHP MessageCatalogue::setLocale怎么用?PHP MessageCatalogue::setLocale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JMS\TranslationBundle\Model\MessageCatalogue
的用法示例。
在下文中一共展示了MessageCatalogue::setLocale方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: write
public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
{
$newCatalogue = new MessageCatalogue();
$newCatalogue->setLocale($catalogue->getLocale());
foreach (array_keys($catalogue->getDomains()) as $catalogueDomainString) {
if ($catalogue->getLocale() !== 'en' && $this->hasEnglishCatalogue($filePath)) {
$englishCatalogue = $this->loadEnglishCatalogue($filePath, $domain, $format);
}
$domainMessageCollection = $catalogue->getDomain($catalogueDomainString);
/** @var Message $message */
foreach ($domainMessageCollection->all() as $message) {
if ($message->getDomain() !== $domain) {
continue;
}
$newMessage = $this->makeXliffMessage($message);
if ($message->getId() === $message->getSourceString()) {
if (isset($englishCatalogue)) {
try {
$newMessage->setDesc($englishCatalogue->get($message->getId(), $message->getDomain())->getLocaleString());
} catch (InvalidArgumentException $e) {
continue;
}
} else {
$newMessage->setDesc($message->getLocaleString());
}
}
$newCatalogue->add($newMessage);
}
}
$this->innerFileWriter->write($newCatalogue, $domain, $filePath, $format);
}
示例2: testGetSetLocale
public function testGetSetLocale()
{
$catalogue = new MessageCatalogue();
$this->assertNull($catalogue->getLocale());
$catalogue->setLocale('en');
$this->assertEquals('en', $catalogue->getLocale());
}
示例3: load
/**
* @param mixed $resource
* @param string $locale
* @param string $domain
* @return MessageCatalogue
*/
public function load($resource, $locale, $domain = 'messages')
{
$previous = libxml_use_internal_errors(true);
if (false === ($doc = simplexml_load_file($resource))) {
libxml_use_internal_errors($previous);
$libxmlError = libxml_get_last_error();
throw new RuntimeException(sprintf('Could not load XML-file "%s": %s', $resource, $libxmlError->message));
}
libxml_use_internal_errors($previous);
$doc->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$doc->registerXPathNamespace('jms', 'urn:jms:translation');
$hasReferenceFiles = in_array('urn:jms:translation', $doc->getNamespaces(true));
$catalogue = new MessageCatalogue();
$catalogue->setLocale($locale);
/** @var \SimpleXMLElement $trans */
foreach ($doc->xpath('//xliff:trans-unit') as $trans) {
$id = ($resName = (string) $trans->attributes()->resname) ? $resName : (string) $trans->source;
/** @var Message $m */
$m = Message::create($id, $domain)->setDesc((string) $trans->source)->setLocaleString((string) $trans->target);
$m->setApproved($trans['approved'] == 'yes');
if (isset($trans->target['state'])) {
$m->setState((string) $trans->target['state']);
}
// Create closure
$addNoteToMessage = function (Message $m, $note) {
$m->addNote((string) $note, isset($note['from']) ? (string) $note['from'] : null);
};
// If the translation has a note
if (isset($trans->note)) {
// If we have more than one note. We can't use is_array becuase $trans->note is a \SimpleXmlElement
if (count($trans->note) > 1) {
foreach ($trans->note as $note) {
$addNoteToMessage($m, $note);
}
} else {
$addNoteToMessage($m, $trans->note);
}
}
$catalogue->add($m);
if ($hasReferenceFiles) {
foreach ($trans->xpath('./jms:reference-file') as $file) {
$line = (string) $file->attributes()->line;
$column = (string) $file->attributes()->column;
$m->addSource(new FileSource((string) $file, $line ? (int) $line : null, $column ? (int) $column : null));
}
}
if ($meaning = (string) $trans->attributes()->extradata) {
if (0 === strpos($meaning, 'Meaning: ')) {
$meaning = substr($meaning, 9);
}
$m->setMeaning($meaning);
}
if (!($state = (string) $trans->target->attributes()->state) || 'new' !== $state) {
$m->setNew(false);
}
}
return $catalogue;
}
示例4: testDumpStructureWithoutPrettyPrint
public function testDumpStructureWithoutPrettyPrint()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('fr');
$catalogue->add(new Message('foo.bar.baz'));
$dumper = new PhpDumper();
$dumper->setPrettyPrint(false);
$this->assertEquals($this->getOutput('structure_wo_pretty_print'), $dumper->dump($catalogue, 'messages'));
}
示例5: load
/**
* Converts Symfony's message catalogue to the catalogue of this
* bundle.
*
* @param mixed $resource
* @param string $locale
* @param string $domain
* @return MessageCatalogue
*/
public function load($resource, $locale, $domain = 'messages')
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale($locale);
foreach ($this->loader->load($resource, $locale, $domain)->all($domain) as $id => $message) {
$catalogue->add(Message::create($id, $domain)->setLocaleString($message)->setNew(false));
}
return $catalogue;
}
示例6: testPathWithSubPath
public function testPathWithSubPath()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('fr');
$catalogue->add(new Message('foo.bar'));
$catalogue->add(new Message('foo.bar.baz'));
$dumper = $this->getDumper();
$dumper->expects($this->once())->method('dumpStructure')->with(array('foo' => array('bar' => new Message('foo.bar'), 'bar.baz' => new Message('foo.bar.baz'))))->will($this->returnValue('foo'));
$this->assertEquals('foo', $dumper->dump($catalogue, 'messages'));
}
示例7: getStructureCatalogue
/**
* Get the catalogue used for the structure tests
*
* @return MessageCatalogue
*/
protected function getStructureCatalogue()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');
$message = new Message('foo.bar.baz');
$message->addSource(new FileSource('/a/b/c/foo/bar', 1, 2));
$message->addSource(new FileSource('bar/baz', 1, 2));
$catalogue->add($message);
return $catalogue;
}
示例8: testLoadWithSymfonyFormat
public function testLoadWithSymfonyFormat()
{
$loader = new XliffLoader();
$expected = new MessageCatalogue();
$expected->setLocale('en');
$expected->add(XliffMessage::create('foo1')->setDesc('foo1')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo2')->setDesc('foo2')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo3')->setDesc('foo3')->setLocaleString('bar')->setNew(false));
$expected->add(XliffMessage::create('foo4')->setDesc('foo4')->setLocaleString('bar')->setNew(false));
$this->assertEquals($expected, $loader->load(__DIR__ . '/Symfony/xliff/old_format.xml', 'en'));
}
示例9: setConfig
/**
* @param Config $config
*/
private function setConfig(Config $config)
{
$this->config = $config;
$this->logger->info(sprintf("Loading catalogues from \"%s\"", $config->getTranslationsDir()));
$this->existingCatalogue = new MessageCatalogue();
// load external resources, so current translations can be reused in the final translation
foreach ($config->getLoadResources() as $resource) {
$this->existingCatalogue->merge($this->loader->loadFromDirectory($resource, $config->getLocale()));
}
$this->existingCatalogue->merge($this->loader->loadFromDirectory($config->getTranslationsDir(), $config->getLocale()));
$this->extractor->reset();
$this->extractor->setDirectories($config->getScanDirs());
$this->extractor->setExcludedDirs($config->getExcludedDirs());
$this->extractor->setExcludedNames($config->getExcludedNames());
$this->extractor->setEnabledExtractors($config->getEnabledExtractors());
$this->logger->info("Extracting translation keys");
$this->scannedCatalogue = $this->extractor->extract();
$this->scannedCatalogue->setLocale($config->getLocale());
// merge existing messages into scanned messages
foreach ($this->scannedCatalogue->getDomains() as $domainCatalogue) {
foreach ($domainCatalogue->all() as $message) {
if (!$this->existingCatalogue->has($message)) {
continue;
}
$existingMessage = clone $this->existingCatalogue->get($message->getId(), $message->getDomain());
$existingMessage->mergeScanned($message);
$this->scannedCatalogue->set($existingMessage, true);
}
}
if ($this->config->isKeepOldMessages()) {
foreach ($this->existingCatalogue->getDomains() as $domainCatalogue) {
foreach ($domainCatalogue->all() as $message) {
if ($this->scannedCatalogue->has($message)) {
continue;
}
$this->scannedCatalogue->add($message);
}
}
}
//keep old translations translated
if ($this->config->isKeepOldTranslationMessages()) {
$locale = $this->scannedCatalogue->getLocale();
/** @var MessageCatalogue $domainCatalogue */
foreach ($this->scannedCatalogue->getDomains() as $domainCatalogue) {
/** @var Message $message */
foreach ($domainCatalogue->all() as $message) {
$translated = $this->translator->trans($message->getId(), array(), $message->getDomain(), $locale);
$message->setLocaleString($translated);
$message->setNew(false);
}
}
}
}
示例10: testDumpStructureWithMetadata
public function testDumpStructureWithMetadata()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');
$message = new Message('foo.bar.baz');
$message->setDesc('Foo');
$catalogue->add($message);
$message = new Message('foo.bar.moo');
$message->setMeaning('Bar');
$catalogue->add($message);
$message = new Message('foo.baz');
$catalogue->add($message);
$this->assertEquals($this->getOutput('structure_with_metadata'), $this->dump($catalogue, 'messages'));
}
示例11: loadFromDirectory
/**
* @param $dir
* @param $targetLocale
* @return \JMS\TranslationBundle\Model\MessageCatalogue
*/
public function loadFromDirectory($dir, $targetLocale)
{
$files = FileUtils::findTranslationFiles($dir);
$catalogue = new MessageCatalogue();
$catalogue->setLocale($targetLocale);
foreach ($files as $domain => $locales) {
foreach ($locales as $locale => $data) {
if ($locale !== $targetLocale) {
continue;
}
list($format, $file) = $data;
$catalogue->merge($this->getLoader($format)->load($file, $locale, $domain));
}
}
return $catalogue;
}
示例12: testCatalogueIsSortedBeforeBeingDumped
public function testCatalogueIsSortedBeforeBeingDumped()
{
$dumper = $this->getMock('JMS\\TranslationBundle\\Translation\\Dumper\\DumperInterface');
$self = $this;
$dumper->expects($this->once())->method('dump')->will($this->returnCallback(function ($v) use($self) {
$self->assertEquals(array('foo.bar', 'foo.bar.baz'), array_keys($v->getDomain('messages')->all()));
}));
$writer = new FileWriter(array('test' => $dumper));
$catalogue = new MessageCatalogue();
$catalogue->setLocale('fr');
$catalogue->add(new Message('foo.bar.baz'));
$catalogue->add(new Message('foo.bar'));
$path = tempnam(sys_get_temp_dir(), 'filewriter');
$writer->write($catalogue, 'messages', $path, 'test');
@unlink($path);
}
示例13: load
public function load($resource, $locale, $domain = 'messages')
{
$previous = libxml_use_internal_errors(true);
if (false === ($doc = simplexml_load_file($resource))) {
libxml_use_internal_errors($previous);
$libxmlError = libxml_get_last_error();
throw new RuntimeException(sprintf('Could not load XML-file "%s": %s', $resource, $libxmlError->message));
}
libxml_use_internal_errors($previous);
$doc->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
// $doc->registerXPathNamespace('jms', 'urn:jms:translation');
// $hasReferenceFiles = in_array('urn:jms:translation', $doc->getNamespaces(true));
$catalogue = new MessageCatalogue();
$catalogue->setLocale($locale);
foreach ($doc->xpath('//xliff:trans-unit') as $trans) {
$id = ($resName = (string) $trans->attributes()->resname) ? $resName : (string) $trans->source;
$m = Message::create($id, $domain)->setDesc((string) $trans->source)->setLocaleString((string) $trans->target);
$catalogue->add($m);
// if ($hasReferenceFiles) {
// foreach ($trans->xpath('./jms:reference-file') as $file) {
// $line = (string) $file->attributes()->line;
// $column = (string) $file->attributes()->column;
// $m->addSource(new FileSource(
// (string) $file,
// $line ? (integer) $line : null,
// $column ? (integer) $column : null
// ));
// }
// }
if ($meaning = (string) $trans->attributes()->extradata) {
if (0 === strpos($meaning, 'Meaning: ')) {
$meaning = substr($meaning, 9);
}
$m->setMeaning($meaning);
}
if (!($state = (string) $trans->target->attributes()->state) || 'new' !== $state) {
$m->setNew(false);
}
}
return $catalogue;
}