tags:

views:

25

answers:

2

Digging into the api looking for methods to translate a property of a category term I could only find methods that take a string as the param, but what happens when I have the id of the object which property I want to translate? I'd like to be able to just pass this id and get the translated string (if exists). I achieved this with a relatively simple query:

   function translate_term_description($tid) {
    $result = db_query('SELECT term_data.tid, term_data.description, locales_target.translation 
                        FROM {term_data} 
                        left join ({i18n_strings}, {locales_target}) 
                        on (term_data.tid = i18n_strings.objectid and i18n_strings.lid = locales_target.lid) 
                        where term_data.tid = %d', $tid);

    $term_data = db_fetch_object($result);

    // If a translated string is available return that, otherwise return back the untranslated description
    return !is_null($term_data->translation) ? $term_data->translation : $term_data->description;
}

I just needed the description and I'm just using this function in a context where's it's assumed that the target language is the non-default. Still, it can be expanded to take both the target language and the term property as params.

So what do you think? Should something like this exist on Drupal, or does it have it already (if so, please point me where) ?

+1  A: 

For terms and other items you can use tt(), if you have i18n installed. This is not part of Drupal core, but is a contrib module.

The tt() function has become a wrapper for i18nstrings(), which is what is used now. The input format is a string, but you can lookup per id, with the special notation the function allows:

$term_name = tt('taxonomy:term:'. $term->tid .':name', $term->name);
googletorp
Thing is that tt() is deprecated and takes a string as the source param :s
JoseMarmolejos
+1  A: 

What about i18ntaxonomy_translate_terms from Internationalization ?

mongolito404
Ya, this would be the closest to what I was talking abouut.
JoseMarmolejos