You are here

public function LingotekInterfaceTranslationService::saveTargetData in Lingotek Translation 3.5.x

Same name and namespace in other branches
  1. 4.0.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  2. 3.2.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  3. 3.3.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  4. 3.4.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  5. 3.6.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  6. 3.7.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()
  7. 3.8.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::saveTargetData()

Save the component translation.

Parameters

string $component: The component we want to save a translation for.

$locale: The locale of the translation being saved.

$data: The data being saved.

Return value

string Returns the component which translations are saved.

Overrides LingotekInterfaceTranslationServiceInterface::saveTargetData

2 calls to LingotekInterfaceTranslationService::saveTargetData()
LingotekInterfaceTranslationService::downloadDocument in src/LingotekInterfaceTranslationService.php
Downloads a document from the Lingotek service for a given locale.
LingotekInterfaceTranslationService::downloadDocuments in src/LingotekInterfaceTranslationService.php
Downloads a document from the Lingotek service for all available locales.

File

src/LingotekInterfaceTranslationService.php, line 892

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function saveTargetData($component, $langcode, $data) {
  $customized = TRUE;
  $overwrite_options['customized'] = TRUE;
  $overwrite_options['not_customized'] = FALSE;
  foreach ($data as $sourceData => $translationData) {

    // We need to take and ignore the special _context entry.
    $context = $translationData['_context'];
    unset($translationData['_context']);

    // We need to manage plurals.
    if (count($translationData) == 1) {
      $keys = array_keys($translationData);
      $source = reset($keys);
      $translation = reset($translationData);
    }
    else {
      $keys = array_keys($translationData);
      $source = implode(PoItem::DELIMITER, $keys);
      $translation = implode(PoItem::DELIMITER, $translationData);
    }

    // Look up the source string and any existing translation.
    $strings = \Drupal::service('locale.storage')
      ->getTranslations([
      'language' => $langcode,
      'source' => $source,
      'context' => $context,
    ]);
    $string = reset($strings);
    if (!empty($translation)) {

      // Skip this string unless it passes a check for dangerous code.
      if (!locale_string_is_safe($translation)) {
        \Drupal::logger('lingotek')
          ->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', [
          '%string' => $translation,
        ]);
      }
      elseif ($string) {
        $string
          ->setString($translation);
        if ($string
          ->isNew()) {

          // No translation in this language.
          $string
            ->setValues([
            'language' => $langcode,
            'customized' => $customized,
          ]);
          $string
            ->save();
        }
        elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {

          // Translation exists, only overwrite if instructed.
          $string->customized = $customized;
          $string
            ->save();
        }
      }
      else {

        // No such source string in the database yet.
        $string = \Drupal::service('locale.storage')
          ->createString([
          'source' => $source,
          'context' => $context,
        ])
          ->save();
        \Drupal::service('locale.storage')
          ->createTranslation([
          'lid' => $string
            ->getId(),
          'language' => $langcode,
          'translation' => $translation,
          'customized' => $customized,
        ])
          ->save();
      }
    }
  }
  return $component;
}