You are here

public function XliffFileLoader::load in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/translation/Loader/XliffFileLoader.php \Symfony\Component\Translation\Loader\XliffFileLoader::load()

Loads a locale.

Parameters

mixed $resource A resource:

string $locale A locale:

string $domain The domain:

Return value

MessageCatalogue A MessageCatalogue instance

Throws

NotFoundResourceException when the resource cannot be found

InvalidResourceException when the resource cannot be loaded

Overrides LoaderInterface::load

File

vendor/symfony/translation/Loader/XliffFileLoader.php, line 30

Class

XliffFileLoader
XliffFileLoader loads translations from XLIFF files.

Namespace

Symfony\Component\Translation\Loader

Code

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);
    if (isset($translation->note)) {
      $notes = array();
      foreach ($translation->note as $xmlNote) {
        $noteAttributes = $xmlNote
          ->attributes();
        $note = array(
          'content' => $this
            ->utf8ToCharset((string) $xmlNote, $encoding),
        );
        if (isset($noteAttributes['priority'])) {
          $note['priority'] = (int) $noteAttributes['priority'];
        }
        if (isset($noteAttributes['from'])) {
          $note['from'] = (string) $noteAttributes['from'];
        }
        $notes[] = $note;
      }
      $catalogue
        ->setMetadata((string) $source, array(
        'notes' => $notes,
      ), $domain);
    }
  }
  if (class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) {
    $catalogue
      ->addResource(new FileResource($resource));
  }
  return $catalogue;
}