You are here

function i18nstrings_cache in Internationalization 6

Retrieves and stores translations in page (static variable) cache.

Parameters

$context: String id or context object

$langcode: Language code to translate to

$string: Source string when available

$translation: Translated string to store into the cache

Return value

  • Translation if chached (may be false if no translation)
  • NULL if no value cached
2 calls to i18nstrings_cache()
i18nstrings_get_string in i18nstrings/i18nstrings.module
Get string for a language.
i18nstrings_prefetch in i18nstrings/i18nstrings.module
Prefetch a number of object strings.

File

i18nstrings/i18nstrings.module, line 894
Internationalization (i18n) package - translatable strings.

Code

function i18nstrings_cache($context, $langcode, $string = NULL, $translation = NULL) {
  static $strings;
  $context = i18nstrings_context($context, $string);
  if (!$context->objectid && $context->source) {

    // This is a type indexed by string.
    $context->objectid = $context->source;
  }

  // At this point context must have at least textgroup and type.
  if (isset($translation)) {
    if ($context->property) {
      $strings[$langcode][$context->textgroup][$context->type][$context->objectid][$context->property] = $translation;
    }
    elseif ($context->objectid) {
      $strings[$langcode][$context->textgroup][$context->type][$context->objectid] = $translation;
    }
    else {
      $strings[$langcode][$context->textgroup][$context->type] = $translation;
    }
  }
  else {

    // Search up the tree for the object or a default.
    $search =& $strings[$langcode];
    $default = NULL;
    $list = array(
      'textgroup',
      'type',
      'objectid',
      'property',
    );
    while (($field = array_shift($list)) && !empty($context->{$field})) {
      if (isset($search[$context->{$field}])) {
        $search =& $search[$context->{$field}];
        if (isset($search['#default'])) {
          $default = $search['#default'];
        }
      }
      else {

        // We dont have cached this tree so we return the default.
        return $default;
      }
    }

    // Returns the part of the array we got to.
    return $search;
  }
}