You are here

function i18n_taxonomy_translate_terms in Internationalization 7

Translate an array of taxonomy terms.

Translates all terms with language, just passing over terms without it. Filter out terms with a different language

Parameters

$taxonomy: Array of term objects or tids or multiple arrays or terms indexed by vid

$langcode: Language code of target language

$fullterms: Whether to return full $term objects, returns tids otherwise

Return value

Array with translated terms: tid -> $term Array with vid and term array

File

i18n_taxonomy/i18n_taxonomy.module, line 995
i18n taxonomy module

Code

function i18n_taxonomy_translate_terms($taxonomy, $langcode, $fullterms = TRUE) {
  $translation = array();
  if (is_array($taxonomy) && $taxonomy) {
    foreach ($taxonomy as $index => $tdata) {
      if (is_array($tdata)) {

        // Case 1: Index is vid, $tdata is an array of terms
        $mode = i18n_taxonomy_vocabulary_mode($index);

        // We translate just some vocabularies: translatable, fixed language
        // Fixed language ones may have terms translated, though the UI doesn't support it
        if ($mode & I18N_MODE_LANGUAGE || $mode & I18N_MODE_TRANSLATE) {
          $translation[$index] = i18n_taxonomy_translate_terms($tdata, $langcode, $fullterms);
        }
        elseif ($fullterms) {
          $translation[$index] = array_map('_i18n_taxonomy_filter_terms', $tdata);
        }
        else {
          $translation[$index] = array_map('_i18n_taxonomy_filter_tids', $tdata);
        }
        continue;
      }
      elseif (is_object($tdata)) {

        // Case 2: This is a term object
        $term = $tdata;
      }
      elseif (is_numeric($tdata) && ($tid = (int) $tdata)) {

        // Case 3: This is a term tid, load the full term
        $term = taxonomy_term_load($tid);
      }

      // Translate the term if we got it
      if (empty($term)) {

        // Couldn't identify term, pass through whatever it is
        $translation[$index] = $tdata;
      }
      elseif ($term->language && $term->language != $langcode) {
        $translation_set = i18n_translation_set_load($term->i18n_tsid);
        $translations = $translation_set ? $translation_set
          ->get_translations() : NULL;
        if ($translations && !empty($translations[$langcode])) {
          $newterm = $translations[$langcode];
          $translation[$newterm->tid] = $fullterms ? $newterm : $newterm->tid;
        }
      }
      else {

        // Term has no language. Should be ok.
        $translation[$index] = $fullterms ? $term : $term->tid;
      }
    }
  }
  return $translation;
}