本文整理汇总了PHP中Symfony\Component\Translation\MessageCatalogue::addResource方法的典型用法代码示例。如果您正苦于以下问题:PHP MessageCatalogue::addResource方法的具体用法?PHP MessageCatalogue::addResource怎么用?PHP MessageCatalogue::addResource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\MessageCatalogue
的用法示例。
在下文中一共展示了MessageCatalogue::addResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
try {
$dom = XmlUtils::loadFile($resource);
} catch (\InvalidArgumentException $e) {
throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e);
}
$internalErrors = libxml_use_internal_errors(true);
libxml_clear_errors();
$xpath = new \DOMXPath($dom);
$nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]');
$catalogue = new MessageCatalogue($locale);
if ($nodes->length == 1) {
$translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message');
foreach ($translations as $translation) {
$translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue;
if (!empty($translationValue)) {
$catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, $translationValue, $domain);
}
$translation = $translation->nextSibling;
}
$catalogue->addResource(new FileResource($resource));
}
libxml_use_internal_errors($internalErrors);
return $catalogue;
}
示例2: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
list($xml, $encoding) = $this->parseFile($resource);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();
if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
continue;
}
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
$target = (string) $translation->target;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
if ('UTF-8' !== $encoding && !empty($encoding)) {
if (function_exists('mb_convert_encoding')) {
$target = mb_convert_encoding($target, $encoding, 'UTF-8');
} elseif (function_exists('iconv')) {
$target = iconv('UTF-8', $encoding, $target);
} else {
throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
}
}
$catalogue->set((string) $source, $target, $domain);
}
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例3: load
/**
* {@inheritdoc}
*/
function load($resource, $locale, $domain = 'messages')
{
$catalogue = new MessageCatalogue($locale);
$catalogue->addMessages(require $resource, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例4: load
/**
*
* {@inheritdoc}
*
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!is_dir($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
try {
$rb = new \ResourceBundle($locale, $resource);
} catch (\Exception $e) {
// HHVM compatibility: constructor throws on invalid resource
$rb = null;
}
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
if (class_exists('Symfony\\Component\\Config\\Resource\\DirectoryResource')) {
$catalogue->addResource(new DirectoryResource($resource));
}
return $catalogue;
}
示例5: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
list($xml, $encoding) = $this->parseFile($resource);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();
if (!(isset($attributes['resname']) || isset($translation->source))) {
continue;
}
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
$target = $this->utf8ToCharset((string) (isset($translation->target) ? $translation->target : $source), $encoding);
$catalogue->set((string) $source, $target, $domain);
$metadata = array();
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
$metadata['notes'] = $notes;
}
if ($translation->target->attributes()) {
$metadata['target-attributes'] = $translation->target->attributes();
}
$catalogue->setMetadata((string) $source, $metadata, $domain);
}
if (class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) {
$catalogue->addResource(new FileResource($resource));
}
return $catalogue;
}
示例6: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$dom = new \DOMDocument();
$current = libxml_use_internal_errors(true);
if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
throw new InvalidResourceException(implode("\n", $this->getXmlErrors()));
}
$xpath = new \DOMXPath($dom);
$nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]');
$catalogue = new MessageCatalogue($locale);
if ($nodes->length == 1) {
$translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message');
foreach ($translations as $translation) {
$translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue;
if (!empty($translationValue)) {
$catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, $translationValue, $domain);
}
$translation = $translation->nextSibling;
}
$catalogue->addResource(new FileResource($resource));
}
libxml_use_internal_errors($current);
return $catalogue;
}
示例7: testLoadOldFormat
public function testLoadOldFormat()
{
$expected = new MessageCatalogue('en');
$expected->add(array('foo1' => 'bar', 'foo2' => 'bar', 'foo3' => 'bar', 'foo4' => 'bar'));
$file = __DIR__ . '/xliff/old_format.xml';
$expected->addResource(new FileResource($file));
$this->assertEquals($expected, $this->getLoader()->load($file, 'en'));
}
示例8: testLoadWithMetadata
public function testLoadWithMetadata()
{
$expected = new MessageCatalogue('en');
$expected->add(array('foo' => 'bar'));
$file = $this->getInputFile('with_metadata');
$expected->addResource(new FileResource($file));
$this->assertEquals($expected, $this->load($file));
}
示例9: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$xml = $this->parseFile($resource);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
}
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例10: load
/**
* {@inheritDoc}
*
* @param mixed $resource resource
* @param string $locale locale name
* @param string $domain message domain
*
* @return MessageCatalogue
*/
public function load($resource, $locale, $domain = 'messages')
{
$repository = $this->getRepository();
$messages = $repository->findBy(array('domain' => $domain, 'locale' => $locale, 'isLocalized' => true));
$catalogue = new MessageCatalogue($locale);
array_walk($messages, function ($message) use($catalogue) {
$catalogue->set((string) $message->getOriginal(), $message->getTranslated(), $message->getDomain());
});
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例11: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$rb = new \ResourceBundle($locale, $resource);
if (!$rb) {
throw new \RuntimeException("cannot load this resource : $resource");
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
if (is_dir($resource)) {
$catalogue->addResource(new DirectoryResource($resource));
} elseif (is_file($resource.'.dat')) {
$catalogue->addResource(new FileResource($resource.'.dat'));
}
return $catalogue;
}
示例12: load
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf("This is not a local file '%s'.", $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf("File '%s' not found.", $resource));
}
$messages = (require $resource);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例13: load
/**
* {@inheritdoc}
*
* @api
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
}
$xml = $this->parseFile($resource);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
}
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例14: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$previous = libxml_use_internal_errors(true);
if (false === ($xml = simplexml_load_file($resource))) {
libxml_use_internal_errors($previous);
$error = libxml_get_last_error();
throw new RuntimeException(sprintf('An error occurred while reading "%s": %s', $resource, $error->message));
}
libxml_use_internal_errors($previous);
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$id = ($resName = (string) $translation->attributes()->resname) ? $resName : (string) $translation->source;
$catalogue->set($id, (string) $translation->target, $domain);
}
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
示例15: load
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource . '.dat')) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource . '.dat')) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$rb = new \ResourceBundle($locale, $resource);
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
$catalogue->addResource(new FileResource($resource . '.dat'));
return $catalogue;
}