/** * 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); }
/** * {@inheritdoc} */ protected function resolveCacheMiss($offset) { $translation = $this->stringStorage->findTranslation(array('language' => $this->langcode, 'source' => $offset, 'context' => $this->context)); if ($translation) { $value = !empty($translation->translation) ? $translation->translation : TRUE; } else { // We don't have the source string, update the {locales_source} table to // indicate the string is not translated. $this->stringStorage->createString(array('source' => $offset, 'context' => $this->context, 'version' => \Drupal::VERSION))->addLocation('path', $this->requestUri())->save(); $value = TRUE; } // If there is no translation available for the current language then use // language fallback to try other translations. if ($value === TRUE) { $fallbacks = $this->languageManager->getFallbackCandidates($this->langcode, array('operation' => 'locale_lookup', 'data' => $offset)); if (!empty($fallbacks)) { foreach ($fallbacks as $langcode) { $translation = $this->stringStorage->findTranslation(array('language' => $langcode, 'source' => $offset, 'context' => $this->context)); if ($translation && !empty($translation->translation)) { $value = $translation->translation; break; } } } } $this->storage[$offset] = $value; // Disabling the usage of string caching allows a module to watch for // the exact list of strings used on a page. From a performance // perspective that is a really bad idea, so we have no user // interface for this. Be careful when turning this option off! if ($this->configFactory->get('locale.settings')->get('cache_strings')) { $this->persist($offset); } return $value; }
/** * 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); } } } }
/** * Tests locale lookups without a found translation. * * @covers ::resolveCacheMiss() */ public function testResolveCacheMissNoTranslation() { $string = $this->getMock('Drupal\\locale\\StringInterface'); $string->expects($this->once())->method('addLocation')->will($this->returnSelf()); $this->storage->expects($this->once())->method('findTranslation')->will($this->returnValue(NULL)); $this->storage->expects($this->once())->method('createString')->will($this->returnValue($string)); $locale_lookup = $this->getMockBuilder('Drupal\\locale\\LocaleLookup')->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager))->setMethods(array('persist', 'requestUri'))->getMock(); $locale_lookup->expects($this->never())->method('persist'); $this->assertTrue($locale_lookup->get('test')); }
/** * 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; }
/** * Creates single translation for source string. */ protected function createTranslation($source, $translation, $langcode) { $values = array('lid' => $source->lid, 'language' => $langcode, 'translation' => $translation); return $this->localeStorage->createTranslation($values)->save(); }
/** * Creates single translation for source string. */ public function createTranslation($source, $langcode, $values = array()) { return $this->storage->createTranslation($values + array('lid' => $source->lid, 'language' => $langcode, 'translation' => $this->randomName(100)))->save(); }
/** * Creates single translation for source string. */ public function createTranslation($source, $langcode) { return $this->localeStorage->createTranslation(array('lid' => $source->lid, 'language' => $langcode, 'translation' => $this->randomMachineName(100)))->save(); }