You are here

private function PoDatabaseWriter::importString in Localization update 7.2

Imports one string into the database.

Parameters

PoItem $item: The item being imported.

integer $plid: The parent string identifier for plural strings.

integer $plural: The plural index number.

Return value

int The string ID of the existing string modified or the new string added.

1 call to PoDatabaseWriter::importString()
PoDatabaseWriter::writeItem in includes/locale/PoDatabaseWriter.php
Implements PoWriterInterface::writeItem().

File

includes/locale/PoDatabaseWriter.php, line 245
Definition of PoDatabaseWriter.

Class

PoDatabaseWriter
Gettext PO writer working with the locale module database.

Code

private function importString(PoItem $item, $plid = 0, $plural = 0) {

  // Initialize overwrite options if not set.
  $this->_options['overwrite_options'] += array(
    'not_customized' => FALSE,
    'customized' => FALSE,
  );
  $overwrite_options = $this->_options['overwrite_options'];
  $customized = $this->_options['customized'];
  $context = $item
    ->getContext();
  $source = $item
    ->getSource();
  $translation = $item
    ->getTranslation();
  $textgroup = $item
    ->getTextgroup();

  // Look up the source string and any existing translation.
  $strings = $this->storage
    ->getTranslations(array(
    'language' => $this->_langcode,
    'source' => $source,
    'context' => $context,
    'textgroup' => $textgroup,
  ));
  $string = reset($strings);
  if (!empty($translation)) {

    // Skip this string unless it passes a check for dangerous code.
    if (!locale_string_is_safe($translation)) {
      watchdog('l10n_update', 'Import of string "%string" was skipped because of disallowed or malformed HTML.', array(
        '%string' => $translation,
      ), WATCHDOG_ERROR);
      $this->_report['skips']++;
      return 0;
    }
    elseif ($string) {
      $string
        ->setString($translation);
      if ($string
        ->isNew()) {

        // No translation in this language.
        $string
          ->setValues(array(
          'plid' => $plid,
          'plural' => $plural,
          'language' => $this->_langcode,
          'customized' => $customized,
        ));
        $string
          ->save();
        $this->_report['additions']++;
      }
      elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {

        // Translation exists, only overwrite if instructed.
        $string->customized = $customized;
        $string
          ->save();
        $this->_report['updates']++;
      }
      $this->_report['strings'][] = $string
        ->getId();
      return $string->lid;
    }
    else {

      // No such source string in the database yet.
      $string = $this->storage
        ->createString(array(
        'source' => $source,
        'context' => $context,
        'textgroup' => $textgroup,
      ))
        ->save();
      $this->storage
        ->createTranslation(array(
        'lid' => $string
          ->getId(),
        'plid' => $plid,
        'plural' => $plural,
        'language' => $this->_langcode,
        'translation' => $translation,
        'customized' => $customized,
      ))
        ->save();
      $this->_report['additions']++;
      $this->_report['strings'][] = $string
        ->getId();
      return $string->lid;
    }
  }
  elseif ($string && !$string
    ->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {

    // Empty translation, remove existing if instructed.
    $string
      ->delete();
    $this->_report['deletes']++;
    $this->_report['strings'][] = $string->lid;
    return $string->lid;
  }
}