You are here

public function XliffFileLoader::load in Plug 7

@api

Overrides LoaderInterface::load

File

lib/Symfony/translation/Loader/XliffFileLoader.php, line 34

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)) || !isset($translation->target)) {
      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) $translation->target, $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;
}