You are here

public function LocaleSource::getData in Translation Management Tool 8

Returns an array with the data structured for translation.

Parameters

\Drupal\tmgmt\JobItemInterface $job_item: The job item entity.

Overrides SourcePluginInterface::getData

See also

JobItem::getData()

File

sources/locale/src/Plugin/tmgmt/Source/LocaleSource.php, line 175

Class

LocaleSource
Translation Source plugin for locale strings.

Namespace

Drupal\tmgmt_locale\Plugin\tmgmt\Source

Code

public function getData(JobItemInterface $job_item) {
  $locale_object = $this
    ->getLocaleObject($job_item);
  if (empty($locale_object)) {
    throw new TMGMTException(t('Unable to load %language translation for the locale %id', array(
      '%language' => $job_item
        ->getJob()
        ->getSourceLanguage()
        ->getName(),
      '%id' => $job_item
        ->getItemId(),
    )));
  }
  if ($locale_object->origin == 'source') {
    $text = $locale_object->source;
  }
  else {
    $text = $locale_object->translation;
  }

  // Identify placeholders that need to be escaped. Assume that placeholders
  // consist of alphanumeric characters and _,- only and are delimited by
  // non-alphanumeric characters. There are cases that don't match, for
  // example appended SI units like "@valuems", there only @value is the
  // actual placeholder.
  $escape = array();
  if (preg_match_all('/([@!%][a-zA-Z0-9_-]+)/', $text, $matches, PREG_OFFSET_CAPTURE)) {
    foreach ($matches[0] as $match) {
      $escape[$match[1]]['string'] = $match[0];
    }
  }
  $structure['singular'] = array(
    '#label' => t('Singular'),
    '#text' => (string) $text,
    '#translate' => TRUE,
    '#escape' => $escape,
  );
  return $structure;
}