6

I have a translated taxonomy term whose name is Autos Y Motores (Spanish). How can I get the taxonomy term object from it?

If the term name were in English, I would use taxonomy_get_term_by_name('Vehicles & Motors') to get the taxonomy term object.

How can I do with a translated taxonomy term name?

2
  • How is the term translated - manually thru a field or automatically thru a module like i18n? Dec 15, 2015 at 17:49
  • Actually I have the translated string and if any way i am able to get actual string, i would be able to solve my problem.
    – user50407
    Dec 16, 2015 at 6:54

3 Answers 3

5

You can try to translate it before pass it to taxonomy_get_term_by_name function, try some thing like

$string= 'De Lema'; // string in not default site language I suppose in French
$default_lang_short = 'un'; // I supposes you default site language is english change it to your default
$en_value = t($string, array(), array('langcode' => $default_lang_short));
taxonomy_get_term_by_name($en_value);
7
  • I have 'Autos Y Motores' in a variable, and not Vehicles & Motors
    – user50407
    Dec 16, 2015 at 6:28
  • @PurushotamRai it is just a example.
    – Yuseferi
    Dec 16, 2015 at 6:29
  • You are not getting me... The problem is i have translated string and i need actual string. The above code doesn't work.
    – user50407
    Dec 16, 2015 at 7:11
  • @PurushotamRai I don't know your language, I just provide simple solution, first translate it default site language ( I supposed your default language is english), then try to taxonomy_get_term_by_name
    – Yuseferi
    Dec 16, 2015 at 7:58
  • @PurushotamRai check the answer again, I got you a clue not exact the solution, change your data with what you want !!!!
    – Yuseferi
    Dec 16, 2015 at 8:00
4

I don't find any function in Drupal which can translate the translated string into actual string. So, what I did was hit db to get the actual string and hence the string.

    $translated = 'Autos Y Motores';
    $query = db_select('locales_target', 'lt');
    $query
      ->condition('lt.translation', $target)
      ->fields('lt', array('translation'))
      ->fields('ls', array('source'))
      ->join('locales_source', 'ls', 'ls.lid=lt.lid');
    $results = $query->execute()->fetchAll();
3

If you have this set:

  • ES: Autos Y Motores
  • EN: Vehicles & Motors

Use this code:

 $term = taxonomy_get_term_by_name('Autos Y Motores');
 //getting the term object
 $term = array_values($term)[0];
 //searching the translation
 $translated_term = i18n_taxonomy_term_get_translation($term, 'en');
 //$translated_term is your term in English
 echo $translated_term->name;

Output:

Vehicles & Motors

4
  • the downvoter can explain where is the problem? I can improve my answer Dec 16, 2015 at 13:49
  • 1
    Don't worry to much about anonymous downvotes ... It only takes 1 upvote to compensate for 5 downvotes ... Apr 22, 2016 at 20:24
  • @Pierre.Vriens the problem is not the rep, is always learn form the mistakes Apr 22, 2016 at 20:28
  • Maybe there was just no mistake, but some other whatever reason "just" to downvote ... Apr 22, 2016 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.