You are here

public function i18n_string_textgroup_default::multiple_translation_search in Internationalization 7

Search multiple translations with key combinations.

Each $context field may be a single value, an array of values or '*'. Example: array('term', array(1,2), '*') This will be mapped into the following conditions (provided language code is 'es') array('type' => 'term', 'objectid' => array(1,2), 'property' => '*', 'language' => 'es') And will result in these combinations to search for array('type' => 'term', 'objectid' => 1, 'property' => '*', 'language' => 'es') array('type' => 'term', 'objectid' => 2, 'property' => '*', 'language' => 'es')

Parameters

$context array: Array with String context conditions.

Return value

Array of translation objects indexed by context.

1 call to i18n_string_textgroup_default::multiple_translation_search()
i18n_string_textgroup_default::multiple_translate in i18n_string/i18n_string.inc
Translate array of source strings

File

i18n_string/i18n_string.inc, line 934
API for internationalization strings

Class

i18n_string_textgroup_default
Textgroup handler for i18n_string API

Code

public function multiple_translation_search($context, $langcode) {

  // First, build conditions and identify the variable field.
  $keys = array(
    'type',
    'objectid',
    'property',
  );
  $conditions = array_combine($keys, $context) + array(
    'language' => $langcode,
  );

  // Find existing searches in cache, compile remaining ones.
  $translations = $search = array();
  foreach ($this
    ->multiple_combine($conditions) as $combination) {
    $cached = $this
      ->multiple_cache_get($combination);
    if (isset($cached)) {

      // Cache hit. Merge and remove value from search.
      $translations += $cached;
    }
    else {

      // Not in cache, add to search conditions skipping duplicated values.
      // As array_merge_recursive() has some bug in PHP 5.2, http://drupal.org/node/1244598
      // we use our simplified version here, instead of $search = array_merge_recursive($search, $combination);
      foreach ($combination as $key => $value) {
        if (!isset($search[$key]) || !in_array($value, $search[$key], TRUE)) {
          $search[$key][] = $value;
        }
      }
    }
  }

  // If we've got any search values left, find translations.
  if ($search) {

    // Load translations for conditions and set them to the cache
    $loaded = $this
      ->multiple_translation_load($search, $langcode);
    if ($loaded) {
      $translations += $loaded;
    }

    // Set cache for each of the multiple search keys.
    foreach ($this
      ->multiple_combine($search) as $combination) {
      $list = $loaded ? $this
        ->string_filter($loaded, $combination) : array();
      $this
        ->multiple_cache_set($combination, $list);
    }
  }
  return $translations;
}