setString example



  /** * Helper function for adding interface text translations. */
  private function addTranslation($langcode$source_string$translation_string) {
    $storage = \Drupal::service('locale.storage');
    $string = $storage->findString(['source' => $source_string]);
    if (is_null($string)) {
      $string = new SourceString();
      $string
        ->setString($source_string)
        ->setStorage($storage)
        ->save();
    }
    $storage->createTranslation([
      'lid' => $string->getId(),
      'language' => $langcode,
      'translation' => $translation_string,
    ])->save();
  }

}
$this->submitForm([
      'langcode' => 'fr',
      'files[file]' => $name,
      'customized' => 1,
    ], 'Import');
    $file_system->unlink($name);

    // Create string without translation in the locales_source table.     $this->container
      ->get('locale.storage')
      ->createString()
      ->setString('February')
      ->save();

    // Export only customized French translations.     $this->drupalGet('admin/config/regional/translate/export');
    $this->submitForm([
      'langcode' => 'fr',
      'content_options[not_customized]' => FALSE,
      'content_options[customized]' => TRUE,
      'content_options[not_translated]' => FALSE,
    ], 'Export');

    

  protected function setUpTranslation($config_name$key$source$translation$langcode$is_active = FALSE) {
    // Create source and translation strings for the configuration value and add     // the configuration name as a location. This would be performed by     // locale_translate_batch_import() invoking     // LocaleConfigManager::updateConfigTranslations() normally.     $this->localeConfigManager->reset();
    $this->localeConfigManager
      ->getStringTranslation($config_name$langcode$source, '')
      ->setString($translation)
      ->setCustomized(FALSE)
      ->save();
    $this->configFactory->reset($config_name);
    $this->localeConfigManager->reset();
    $this->localeConfigManager->updateConfigTranslations([$config_name][$langcode]);

    if ($is_active) {
      $this->assertActiveConfig($config_name$key$translation$langcode);
    }
    else {
      $this->assertConfigOverride($config_name$key$translation$langcode);
    }

      // Save this translation as custom if it was a new translation and not the       // same as the source. (The interface prefills translation values with the       // source). Or if there was an existing (non-empty) translation and the       // user changed it (even if it was changed back to the original value).       // Otherwise the translation file would be overwritten with the locale       // copy again later.       $existing_translation = $locale_translation->getString();
      if (($locale_translation->isNew() && $source != $new_translation) ||
        (!$locale_translation->isNew() && ((empty($existing_translation) && $source != $new_translation) || ((!empty($existing_translation) && $new_translation != $existing_translation))))) {
        $locale_translation
          ->setString($new_translation)
          ->setCustomized(TRUE)
          ->save();
      }
    }
  }

}
/** * {@inheritdoc} */
  public function getPlurals() {
    return explode(PoItem::DELIMITER, $this->getString());
  }

  /** * {@inheritdoc} */
  public function setPlurals($plurals) {
    $this->setString(implode(PoItem::DELIMITER, $plurals));
    return $this;
  }

  /** * {@inheritdoc} */
  public function getStorage() {
    return $this->storage ?? NULL;
  }

  /** * {@inheritdoc} */

    $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('locale')->error('Import of string "%string" was skipped because of disallowed or malformed HTML.', ['%string' => $translation]);
        $this->report['skips']++;
        return 0;
      }
      elseif ($string) {
        $string->setString($translation);
        if ($string->isNew()) {
          // No translation in this language.           $string->setValues([
            'language' => $this->langcode,
            'customized' => $customized,
          ]);
          $string->save();
          $this->report['additions']++;
        }
        elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
          // Translation exists, only overwrite if instructed.
public function testGetStringTranslation() {
    $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']);
    $this->installConfig(['locale_test']);

    $locale_config_manager = \Drupal::service('locale.config_manager');

    $language = ConfigurableLanguage::createFromLangcode('de');
    $language->save();

    $translation_before = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
    $this->assertTrue($translation_before->isNew());
    $translation_before->setString('translation')->save();

    $translation_after = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
    $this->assertFalse($translation_after->isNew());
    $translation_after->setString('updated_translation')->save();
  }

  /** * Tests getDefaultConfigLangcode(). */
  public function testGetDefaultConfigLangcode() {
    // Install the Language module's configuration so we can use the
Home | Imprint | This part of the site doesn't use cookies.