You are here

protected function ConfigTranslationUiTest::getTranslation in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiTest::getTranslation()
  2. 9 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiTest::getTranslation()

Gets translation from locale storage.

Parameters

$config_name: Configuration object.

$key: Translation configuration field key.

$langcode: String language code to load translation.

Return value

bool|mixed Returns translation if exists, FALSE otherwise.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php, line 1124

Class

ConfigTranslationUiTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

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);
    }
  }
  return FALSE;
}