/** * Test CRUD API. */ public function testStringCRUDAPI() { // Create source string. $source = $this->buildSourceString(); $source->save(); $this->assertTrue($source->lid, format_string('Successfully created string %string', array('%string' => $source->source))); // Load strings by lid and source. $string1 = $this->storage->findString(array('lid' => $source->lid)); $this->assertEqual($source, $string1, 'Successfully retrieved string by identifier.'); $string2 = $this->storage->findString(array('source' => $source->source, 'context' => $source->context)); $this->assertEqual($source, $string2, 'Successfully retrieved string by source and context.'); $string3 = $this->storage->findString(array('source' => $source->source, 'context' => '')); $this->assertFalse($string3, 'Cannot retrieve string with wrong context.'); // Check version handling and updating. $this->assertEqual($source->version, 'none', 'String originally created without version.'); $string = $this->storage->findTranslation(array('lid' => $source->lid)); $this->assertEqual($string->version, \Drupal::VERSION, 'Checked and updated string version to Drupal version.'); // Create translation and find it by lid and source. $langcode = 'es'; $translation = $this->createTranslation($source, $langcode); $this->assertEqual($translation->customized, LOCALE_NOT_CUSTOMIZED, 'Translation created as not customized by default.'); $string1 = $this->storage->findTranslation(array('language' => $langcode, 'lid' => $source->lid)); $this->assertEqual($string1->translation, $translation->translation, 'Successfully loaded translation by string identifier.'); $string2 = $this->storage->findTranslation(array('language' => $langcode, 'source' => $source->source, 'context' => $source->context)); $this->assertEqual($string2->translation, $translation->translation, 'Successfully loaded translation by source and context.'); $translation->setCustomized()->save(); $translation = $this->storage->findTranslation(array('language' => $langcode, 'lid' => $source->lid)); $this->assertEqual($translation->customized, LOCALE_CUSTOMIZED, 'Translation successfully marked as customized.'); // Delete translation. $translation->delete(); $deleted = $this->storage->findTranslation(array('language' => $langcode, 'lid' => $source->lid)); $this->assertFalse(isset($deleted->translation), 'Successfully deleted translation string.'); // Create some translations and then delete string and all of its // translations. $lid = $source->lid; $this->createAllTranslations($source); $search = $this->storage->getTranslations(array('lid' => $source->lid)); $this->assertEqual(count($search), 3, 'Created and retrieved all translations for our source string.'); $source->delete(); $string = $this->storage->findString(array('lid' => $lid)); $this->assertFalse($string, 'Successfully deleted source string.'); $deleted = $search = $this->storage->getTranslations(array('lid' => $lid)); $this->assertFalse($deleted, 'Successfully deleted all translation strings.'); // Tests that locations of different types and arbitrary lengths can be // added to a source string. Too long locations will be cut off. $source_string = $this->buildSourceString(); $source_string->addLocation('javascript', $this->randomString(8)); $source_string->addLocation('configuration', $this->randomString(50)); $source_string->addLocation('code', $this->randomString(100)); $source_string->addLocation('path', $location = $this->randomString(300)); $source_string->save(); $rows = db_query('SELECT * FROM {locales_location} WHERE sid = :sid', array(':sid' => $source_string->lid))->fetchAllAssoc('type'); $this->assertEqual(count($rows), 4, '4 source locations have been persisted.'); $this->assertEqual($rows['path']->name, substr($location, 0, 255), 'Too long location has been limited to 255 characters.'); }
/** * Sets configuration based on a nested form value array. * * @param \Drupal\Core\Language\LanguageInterface $language * Set the configuration in this language. * @param \Drupal\Core\Config\Config $base_config * Base configuration values, in the source language. * @param \Drupal\language\Config\LanguageConfigOverride $config_translation * Translation configuration override data. * @param array $config_values * A simple one dimensional or recursive array: * - simple: * array(name => array('translation' => 'French site name')); * - recursive: * cancel_confirm => array( * cancel_confirm.subject => array('translation' => 'Subject'), * cancel_confirm.body => array('translation' => 'Body content'), * ); * Either format is used, the nested arrays are just containers and not * needed for saving the data. * @param bool $shipped_config * (optional) Flag to specify whether the configuration had a shipped * version and therefore should also be stored in the locale database. * * @return array * Translation configuration override data. */ protected function setConfig(LanguageInterface $language, Config $base_config, LanguageConfigOverride $config_translation, array $config_values, $shipped_config = FALSE) { foreach ($config_values as $key => $value) { if (is_array($value) && !isset($value['translation'])) { // Traverse into this level in the configuration. $this->setConfig($language, $base_config, $config_translation, $value, $shipped_config); } else { // If the configuration file being translated was originally shipped, we // should update the locale translation storage. The string should // already be there, but we make sure to check. if ($shipped_config && ($source_string = $this->localeStorage->findString(array('source' => $base_config->get($key))))) { // Get the translation for this original source string from locale. $conditions = array('lid' => $source_string->lid, 'language' => $language->id); $translations = $this->localeStorage->getTranslations($conditions + array('translated' => TRUE)); // If we got a translation, take that, otherwise create a new one. $translation = reset($translations) ?: $this->localeStorage->createTranslation($conditions); // If we have a new translation or different from what is stored in // locale before, save this as an updated customize translation. if ($translation->isNew() || $translation->getString() != $value['translation']) { $translation->setString($value['translation'])->setCustomized()->save(); } } // Save value, if different from the source value in the base // configuration. If same as original configuration, remove override. if ($base_config->get($key) !== $value['translation']) { $config_translation->set($key, $value['translation']); } else { $config_translation->clear($key); } } } }
/** * Gets translation from locale storage. * * @param $config_name * Configuration object. * @param $key * Translation configuration field key. * @param $langcode * String language code to load translation. * * @return bool|mixed * Returns translation if exists, FALSE otherwise. */ protected function getTranslation($config_name, $key, $langcode) { $settings_locations = $this->localeStorage->getLocations(array('type' => 'configuration', 'name' => $config_name)); $this->assertTrue(!empty($settings_locations), format_string('Configuration locations found for %config_name.', array('%config_name' => $config_name))); if (!empty($settings_locations)) { $source = $this->container->get('config.factory')->get($config_name)->get($key); $source_string = $this->localeStorage->findString(array('source' => $source, 'type' => 'configuration')); $this->assertTrue(!empty($source_string), format_string('Found string for %config_name.%key.', array('%config_name' => $config_name, '%key' => $key))); if (!empty($source_string)) { $conditions = array('lid' => $source_string->lid, 'language' => $langcode); $translations = $this->localeStorage->getTranslations($conditions + array('translated' => TRUE)); return reset($translations); } } return FALSE; }