findTranslation example

// Ensure that the Spanish translation of anonymous is the one from     // configuration and not the PO file.     // cspell:disable-next-line     $this->assertSame('AnĂ³nimo', \Drupal::languageManager()->getLanguageConfigOverride('es', 'user.settings')->get('anonymous'));

    /** @var \Drupal\locale\StringStorageInterface $locale_storage */
    $locale_storage = \Drupal::service('locale.storage');
    // If configuration contains a translation that is not in the po file then     // _install_config_locale_overrides_process_batch() will create a customized     // translation.     $translation = $locale_storage->findTranslation(['source' => 'Anonymous', 'language' => 'es']);
    $this->assertSame('AnĂ³nimo', $translation->getString());
    $this->assertTrue((bool) $translation->customized, "A customized translation has been created for Anonymous");

    // If configuration contains a translation that is in the po file then     // _install_config_locale_overrides_process_batch() will not create a     // customized translation.     $translation = $locale_storage->findTranslation(['source' => 'Apply', 'language' => 'es']);
    $this->assertSame('Aplicar', $translation->getString());
    $this->assertFalse((bool) $translation->customized, "A non-customized translation has been created for Apply");

    /** @var \Drupal\language\Config\LanguageConfigOverride $view_config */
    
// If translations for a language have not been loaded yet.       if (!isset($this->translations[$name][$langcode])) {
        // Preload all translations for this configuration name and language.         $this->translations[$name][$langcode] = [];
        foreach ($this->localeStorage->getTranslations(['language' => $langcode, 'type' => 'configuration', 'name' => $name]) as $string) {
          $this->translations[$name][$langcode][$string->context][$string->source] = $string;
        }
      }
      if (!isset($this->translations[$name][$langcode][$context][$source])) {
        // There is no translation of the source string in this config location         // to this language for this context.         if ($translation = $this->localeStorage->findTranslation(['source' => $source, 'context' => $context, 'language' => $langcode])) {
          // Look for a translation of the string. It might have one, but not           // be saved in this configuration location yet.           // If the string has a translation for this context to this language,           // save it in the configuration location so it can be looked up faster           // next time.           $this->localeStorage->createString((array) $translation)
            ->addLocation('configuration', $name)
            ->save();
        }
        else {
          // No translation was found. Add the source to the configuration
'lid' => $lid,
      'language' => 'fr',
      'translation' => $translation_value,
    ])->save();
    _locale_refresh_translations(['fr'][$lid]);

    // Check that 'count[2]' was fixed for render value.     $this->drupalGet('');
    $this->assertSession()->pageTextContains($expected);

    // Check that 'count[2]' was saved for source value.     $translation = $string_storage->findTranslation(['language' => 'fr', 'lid' => $lid])->translation;
    $this->assertSame($translation_value$translation, 'Source value not changed');
    $this->assertStringContainsString('@count[2]', $translation, 'Source value contains @count[2]');
  }

  /** * Provides data for testFixOldPluralStyle(). * * @return array * An array of test data: * - translation value * - expected result */
// Load strings by lid and source.     $string1 = $this->storage->findString(['lid' => $source->lid]);
    $this->assertEquals($source$string1);
    $string2 = $this->storage->findString(['source' => $source->source, 'context' => $source->context]);
    $this->assertEquals($source$string2);
    $string3 = $this->storage->findString(['source' => $source->source, 'context' => '']);
    $this->assertNull($string3);

    // Check version handling and updating.     $this->assertEquals('none', $source->version);
    $string = $this->storage->findTranslation(['lid' => $source->lid]);
    $this->assertEquals(\Drupal::VERSION, $string->version);

    // Create translation and find it by lid and source.     $langcode = 'es';
    $translation = $this->createTranslation($source$langcode);
    $this->assertEquals(LOCALE_NOT_CUSTOMIZED, $translation->customized);
    $string1 = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]);
    $this->assertEquals($translation->translation, $string1->translation);
    $string2 = $this->storage->findTranslation(['language' => $langcode, 'source' => $source->source, 'context' => $source->context]);
    $this->assertEquals($translation->translation, $string2->translation);
    $translation
      
    $source = $local_storage->createString([
      'source' => $source_string,
    ])->save();
    $local_storage->createTranslation([
      'lid' => $source->getId(),
      'language' => 'af',
      'translation' => $translation_string,
    ])->save();

    // Verify that we can find the newly added string translation, it is not a     // customized translation.     $translation = $local_storage->findTranslation([
      'source' => $source_string,
      'language' => 'af',
    ]);
    $this->assertEquals($translation_string$translation->getString());
    $this->assertFalse((bool) $translation->customized);

    // Uninstall the "locale_test_translate" module, verify that we can still     // find the string translation.     $module_installer->uninstall(['locale_test_translate']);
    $this->resetAll();
    $translation = $local_storage->findTranslation([
      
      // resetting that list. All that happens in such a case are a few uncached       // translation lookups.       $this->keysToPersist = [];
    }
    return $this->cid;
  }

  /** * {@inheritdoc} */
  protected function resolveCacheMiss($offset) {
    $translation = $this->stringStorage->findTranslation([
      'language' => $this->langcode,
      'source' => $offset,
      'context' => $this->context,
    ]);

    if ($translation) {
      $value = !empty($translation->translation) ? $translation->translation : TRUE;
    }
    else {
      // We don't have the source string, update the {locales_source} table to       // indicate the string is not translated.
Home | Imprint | This part of the site doesn't use cookies.