开发者

If there isn't something like this in Drupal then it should be added: translating object properties

开发者 https://www.devze.com 2023-01-05 01:38 出处:网络
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 p

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) ?


What about i18ntaxonomy_translate_terms from Internationalization ?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消