Example #1
0
 /**
  * Builds a string search query and returns an array of string objects.
  *
  * @return \Drupal\locale\TranslationString[]
  *   Array of \Drupal\locale\TranslationString objects.
  */
 protected function translateFilterLoadStrings()
 {
     $filter_values = $this->translateFilterValues();
     // Language is sanitized to be one of the possible options in
     // translateFilterValues().
     $conditions = array('language' => $filter_values['langcode']);
     $options = array('pager limit' => 30, 'translated' => TRUE, 'untranslated' => TRUE);
     // Add translation status conditions and options.
     switch ($filter_values['translation']) {
         case 'translated':
             $conditions['translated'] = TRUE;
             if ($filter_values['customized'] != 'all') {
                 $conditions['customized'] = $filter_values['customized'];
             }
             break;
         case 'untranslated':
             $conditions['translated'] = FALSE;
             break;
     }
     if (!empty($filter_values['string'])) {
         $options['filters']['source'] = $filter_values['string'];
         if ($options['translated']) {
             $options['filters']['translation'] = $filter_values['string'];
         }
     }
     return $this->localeStorage->getTranslations($conditions, $options);
 }
 /**
  * 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);
             }
         }
     }
 }
Example #3
0
 /**
  * Test Search API loading multiple objects.
  */
 public function testStringSearchAPI()
 {
     $language_count = 3;
     // Strings 1 and 2 will have some common prefix.
     // Source 1 will have all translations, not customized.
     // Source 2 will have all translations, customized.
     // Source 3 will have no translations.
     $prefix = $this->randomName(100);
     $source1 = $this->buildSourceString(array('source' => $prefix . $this->randomName(100)))->save();
     $source2 = $this->buildSourceString(array('source' => $prefix . $this->randomName(100)))->save();
     $source3 = $this->buildSourceString()->save();
     // Load all source strings.
     $strings = $this->storage->getStrings(array());
     $this->assertEqual(count($strings), 3, 'Found 3 source strings in the database.');
     // Load all source strings matching a given string.
     $filter_options['filters'] = array('source' => $prefix);
     $strings = $this->storage->getStrings(array(), $filter_options);
     $this->assertEqual(count($strings), 2, 'Found 2 strings using some string filter.');
     // Not customized translations.
     $translate1 = $this->createAllTranslations($source1);
     // Customized translations.
     $this->createAllTranslations($source2, array('customized' => LOCALE_CUSTOMIZED));
     // Try quick search function with different field combinations.
     $langcode = 'es';
     $found = $this->storage->findTranslation(array('language' => $langcode, 'source' => $source1->source, 'context' => $source1->context));
     $this->assertTrue($found && isset($found->language) && isset($found->translation) && !$found->isNew(), 'Translation found searching by source and context.');
     $this->assertEqual($found->translation, $translate1[$langcode]->translation, 'Found the right translation.');
     // Now try a translation not found.
     $found = $this->storage->findTranslation(array('language' => $langcode, 'source' => $source3->source, 'context' => $source3->context));
     $this->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found->isNew(), 'Translation not found but source string found.');
     // Load all translations. For next queries we'll be loading only translated
     // strings.
     $translations = $this->storage->getTranslations(array('translated' => TRUE));
     $this->assertEqual(count($translations), 2 * $language_count, 'Created and retrieved all translations for source strings.');
     // Load all customized translations.
     $translations = $this->storage->getTranslations(array('customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE));
     $this->assertEqual(count($translations), $language_count, 'Retrieved all customized translations for source strings.');
     // Load all Spanish customized translations.
     $translations = $this->storage->getTranslations(array('language' => 'es', 'customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE));
     $this->assertEqual(count($translations), 1, 'Found only Spanish and customized translations.');
     // Load all source strings without translation (1).
     $translations = $this->storage->getStrings(array('translated' => FALSE));
     $this->assertEqual(count($translations), 1, 'Found 1 source string without translations.');
     // Load Spanish translations using string filter.
     $filter_options['filters'] = array('source' => $prefix);
     $translations = $this->storage->getTranslations(array('language' => 'es'), $filter_options);
     $this->assertEqual(count($translations), 2, 'Found 2 translations using some string filter.');
 }
 /**
  * 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;
 }