Commit 3b39053d authored by webchick's avatar webchick

Issue #2098697 by tstoeckler, sxnc, Gábor Hojtsy: Provide a LocaleConfigManager::hasTranslation().

parent 6250d15f
......@@ -279,6 +279,23 @@ public function translateString($name, $langcode, $source, $context) {
return FALSE;
}
/**
* 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.
*
......@@ -293,4 +310,5 @@ public function translateString($name, $langcode, $source, $context) {
public static function localeConfigName($langcode, $name = NULL) {
return rtrim('locale.config.' . $langcode . '.' . $name, '.');
}
}
<?php
/**
* @file
* Contains \Drupal\locale\Tests\LocaleConfigManagerTest.
*/
namespace Drupal\locale\Tests;
use Drupal\locale\LocaleConfigManager;
use Drupal\Core\Language\Language;
use Drupal\Core\Config\StorageException;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Provides tests for \Drupal\locale\LocaleConfigManager
*/
class LocaleConfigManagerTest extends DrupalUnitTestBase {
/**
* A list of modules to install for this test.
*
* @var array
*/
public static $modules = array('locale', 'locale_test');
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '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.translation', $language);
$this->assertIdentical(TRUE, $result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment