示例#1
0
 /**
  * {@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;
 }
示例#2
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.');
 }