diff --git a/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php b/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php index 9937d78..f7771de 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php @@ -280,6 +280,23 @@ public function translateString($name, $langcode, $source, $context) { } /** + * Checks whether a language has configuration translation. + * + * @param string $name + * Configuration name. + * @param \Drupal\Core\Language\Language $language + * A language object. + * + * @return bool + * A boolean indicating if a language has configuration translations. + */ + public function hasTranslation($name, Language $language) { + $locale_name = static::localeConfigName($language->id, $name); + $translation = $this->configStorage->read($locale_name); + return !empty($translation); + } + + /** * Provides configuration data location for given langcode and name. * * @param string $langcode @@ -293,4 +310,5 @@ public function translateString($name, $langcode, $source, $context) { public static function localeConfigName($langcode, $name = NULL) { return rtrim('locale.config.' . $langcode . '.' . $name, '.'); } + } diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigManagerTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigManagerTest.php new file mode 100644 index 0000000..ae32cbd --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigManagerTest.php @@ -0,0 +1,69 @@ + 'Locale config manager', + 'description' => 'Tests that the locale config manager operates correctly.', + 'group' => 'Locale', + ); + } + + /** + * Tests hasTranslation(). + */ + public function testHasTranslation() { + $locale_config_manager = new LocaleConfigManager( + // In contrast to the actual configuration we use the installer storage + // as the config storage. That way, we do not actually have to install + // the module and can extend DrupalUnitTestBase. + $this->container->get('config.storage.installer'), + $this->container->get('config.storage.schema'), + $this->container->get('config.storage.installer'), + $this->container->get('locale.storage') + ); + + $language = new Language(array('id' => 'de')); + // The installer storage throws an expcetion when requesting a non-existing + // file. + try { + $locale_config_manager->hasTranslation('locale_test.no_translation', $language); + } + catch (StorageException $exception) { + $result = FALSE; + } + $this->assertIdentical(FALSE, $result); + + $result = $locale_config_manager->hasTranslation('locale_test.empty_translation', $language); + $this->assertIdentical(FALSE, $result); + + $result = $locale_config_manager->hasTranslation('locale_test.translation', $language); + $this->assertIdentical(TRUE, $result); + } +} diff --git a/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.empty_translation.yml b/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.empty_translation.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.empty_translation.yml @@ -0,0 +1 @@ + diff --git a/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.translation.yml b/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.translation.yml new file mode 100644 index 0000000..5166583 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/config/locale.config.de.locale_test.translation.yml @@ -0,0 +1 @@ +Test: German test diff --git a/core/modules/locale/tests/modules/locale_test/config/locale_test.empty_translation.yml b/core/modules/locale/tests/modules/locale_test/config/locale_test.empty_translation.yml new file mode 100644 index 0000000..4583231 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/config/locale_test.empty_translation.yml @@ -0,0 +1 @@ +test: Test diff --git a/core/modules/locale/tests/modules/locale_test/config/locale_test.no_translation.yml b/core/modules/locale/tests/modules/locale_test/config/locale_test.no_translation.yml new file mode 100644 index 0000000..4583231 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/config/locale_test.no_translation.yml @@ -0,0 +1 @@ +test: Test diff --git a/core/modules/locale/tests/modules/locale_test/config/locale_test.translation.yml b/core/modules/locale/tests/modules/locale_test/config/locale_test.translation.yml new file mode 100644 index 0000000..c464a89 --- /dev/null +++ b/core/modules/locale/tests/modules/locale_test/config/locale_test.translation.yml @@ -0,0 +1 @@ +Test: English test