You are here

function i18ntaxonomy_translate_terms in Internationalization 6

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

2 calls to i18ntaxonomy_translate_terms()
i18nsync_node_taxonomy in i18nsync/i18nsync.module
Synchronize taxonomy.
i18ntaxonomy_nodeapi in i18ntaxonomy/i18ntaxonomy.module
Implementation of hook_nodeapi().

File

i18ntaxonomy/i18ntaxonomy.module, line 777
i18n taxonomy module

Code

function i18ntaxonomy_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 = i18ntaxonomy_vocabulary($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_TAXONOMY_LANGUAGE || $mode == I18N_TAXONOMY_TRANSLATE) {
          $translation[$index] = i18ntaxonomy_translate_terms($tdata, $langcode, $filter, $fullterms);
        }
        elseif ($fullterms) {
          $translation[$index] = array_map('_i18ntaxonomy_filter_terms', $tdata);
        }
        else {
          $translation[$index] = array_map('_i18ntaxonomy_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_get_term($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) {
        $translated_terms = i18ntaxonomy_term_get_translations(array(
          'tid' => $term->tid,
        ));
        if ($translated_terms && !empty($translated_terms[$langcode])) {
          $newterm = $translated_terms[$langcode];
          $translation[$newterm->tid] = $fullterms ? $newterm : $newterm->tid;
        }
      }
      else {

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