findString example

'label' => $this->randomMachineName(16),
      'direction' => LanguageInterface::DIRECTION_LTR,
    ];
    $this->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add custom language');

    // Check for the source strings we are going to translate. Adding the     // custom language should have made the process to export configuration     // strings to interface translation executed.     $locale_storage = $this->container->get('locale.storage');
    foreach ($config_strings as $config_string) {
      $string = $locale_storage->findString(['source' => $config_string[0], 'context' => '', 'type' => 'configuration']);
      $this->assertNotEmpty($string, 'Configuration strings have been created upon installation.');
    }

    // Import a .po file to translate.     $this->importPoFile($this->getPoFileWithConfig()[
      'langcode' => $langcode,
    ]);

    // Translations got recorded in the interface translation system.     foreach ($config_strings as $config_string) {
      $search = [
        


  /** * Tests CRUD API. */
  public function testStringCrudApi() {
    // Create source string.     $source = $this->buildSourceString()->save();
    $this->assertNotEmpty($source->lid);

    // 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);

    
$this->assertEquals('fr', $context['langcode']);
    $this->assertEquals('locale_lookup', $context['operation']);
  }

  /** * Tests old plural style @count[number] fix. * * @dataProvider providerTestFixOldPluralStyle */
  public function testFixOldPluralStyle($translation_value$expected) {
    $string_storage = \Drupal::service('locale.storage');
    $string = $string_storage->findString(['source' => 'Member for', 'context' => '']);
    $lid = $string->getId();
    $string_storage->createTranslation([
      '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);

    
// Look for the input element, always in second spot.     $elements = $page->findAll('css', '.add ul input');
    $this->assertEquals('Blokk', $elements[1]->getAttribute('value'));
  }

  /** * 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,
    ])
ConfigurableLanguage::createFromLangcode($langcode)->save();
    $this->config('system.site')->set('default_langcode', $langcode)->save();

    // Visit a page that will trigger a JavaScript file parsing for     // translatable strings.     $this->drupalGet('node/add');
    $this->assertNotEmpty($assert_session->waitForElement('css', '.ck-editor'));

    // Ensure a string from the CKEditor 5 plugin is picked up by translation.     // @see core/modules/ckeditor5/js/ckeditor5_plugins/drupalMedia/src/drupalmediatoolbar.js     $locale_storage = $this->container->get('locale.storage');
    $string = $locale_storage->findString(['source' => 'Drupal Media toolbar', 'context' => '']);
    $this->assertNotEmpty($string, 'String from JavaScript file saved.');
  }

}
$this->container->get('router.builder')->rebuild();

    // Place a Block with a translatable string on the page.     $this->placeBlock('system_powered_by_block', ['region' => 'content']);

    // Load the Spanish Node page once, to register the translatable string.     $this->drupalGet('/es/node/1');

    // Translate the Powered by string.     /** @var \Drupal\locale\StringStorageInterface $string_storage */
    $string_storage = \Drupal::getContainer()->get('locale.storage');
    $source = $string_storage->findString(['source' => 'Powered by <a href=":poweredby">Drupal</a>']);
    $string_storage->createTranslation([
      'lid' => $source->lid,
      'language' => 'es',
      'translation' => 'Funciona con ...',
    ])->save();
    // Invalidate caches so that the new translation will be used.     Cache::invalidateTags(['rendered', 'locale']);
  }

  /** * Tests translation with URL and Preferred Admin Language negotiators. * * The interface language uses the preferred language for admin pages of the * user and after that the URL. The Content uses just the URL. */
// Add predefined language.     $this->drupalGet('admin/config/regional/language/add');
    $this->submitForm(['predefined_langcode' => 'af'], 'Add language');

    $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
    $this->assertEquals(['translatable_default_with_translation' => 'Locale can translate Afrikaans']$override->get());

    // Remove the string from translation to simulate a Locale removal. Note     // that is no current way of doing this in the UI.     $locale_storage = \Drupal::service('locale.storage');
    $string = $locale_storage->findString(['source' => 'Locale can translate']);
    \Drupal::service('locale.storage')->delete($string);
    // Force a rebuild of config translations.     $count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings']['af']);
    $this->assertEquals(1, $count, 'Correct count of updated translations');

    $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
    $this->assertEquals([]$override->get());
    $this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
  }

  /** * Tests removing a string from Locale changes configuration translations. */

  protected function getTranslation($config_name$key$langcode) {
    $settings_locations = $this->localeStorage->getLocations(['type' => 'configuration', 'name' => $config_name]);
    $this->assertNotEmpty($settings_locations, "$config_name should have configuration locations.");

    if (!empty($settings_locations)) {
      $source = $this->container->get('config.factory')->get($config_name)->get($key);
      $source_string = $this->localeStorage->findString(['source' => $source, 'type' => 'configuration']);
      $this->assertNotEmpty($source_string, "$config_name.$key should have a source string.");

      if (!empty($source_string)) {
        $conditions = [
          'lid' => $source_string->lid,
          'language' => $langcode,
        ];
        $translations = $this->localeStorage->getTranslations($conditions + ['translated' => TRUE]);
        return reset($translations);
      }
    }
    
    $edit = ["prefix[$this->langcode]" => $this->langcode];
    $this->drupalGet('admin/config/regional/language/detection/url');
    $this->submitForm($edit, 'Save configuration');
  }

  /** * Tests basic configuration translation. */
  public function testConfigTranslation() {
    // Check that the maintenance message exists and create translation for it.     $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
    $string = $this->storage->findString(['source' => $source, 'context' => '', 'type' => 'configuration']);
    $this->assertNotEmpty($string, 'Configuration strings have been created upon installation.');

    // Translate using the UI so configuration is refreshed.     $message = $this->randomMachineName(20);
    $search = [
      'string' => $string->source,
      'langcode' => $this->langcode,
      'translation' => 'all',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    
Home | Imprint | This part of the site doesn't use cookies.