Though a call to module_exists() is not very expensive, in a tight loop like this, it might be an idea to store the result of that call into a local variable that gets initialized outside the loop:
function _hs_taxonomy_hierarchical_select_terms_to_options($terms) {
$options = array();
$use_i18n = module_exists('i18n_taxonomy');
foreach ($terms as $key => $term) {
// Use the translated term when available!
$options[$term->tid] = $use_i18n ? i18n_taxonomy_term_name($term) : t($term->name);
}
return $options;
}
Even better would be to get all translations at once, but there does not yet seem to be a function in i18n that does so. there are some _multiple_ functions over there, but none that only executes one query.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | 1220830-1.patch | 631 bytes | wim leers |
Comments
Comment #1
wim leersmodule_exists() uses module_list(), which has a cached list. Nevertheless, this is indeed a minor performance improvement.
However, you're using i18n_taxonomy_term_name() instead of the current code's tt(). Which is it? Partial patch attached.
Comment #2
fietserwinI don't know tt, I guess that one is from i18n_string or D6? i18n_taxonomy offers a more specialized function name: i18n_taxonomy_term_name(). So that looks like the one to use, especially as tt seems to require knowledge about how i18n_taxnomy formats the context field in the locale tables.
Comment #3
wim leersSo you're certain that this is safe to commit? Because it requires too much additional work to set up a i18n testing site — I rely on reports only to make sure this works.
Comment #4
fietserwinI see, but how can someone from Belgium not have multilingual installs? :)
Anyway, I did some research into the tt() function and found this in a D6 install:
file: i18nstrings..module:
I couldn't find any reference to it in D7 i18n. So yes, I'm sure you should use the i18n_taonomy_term_name:
file: i18n_taxonomy.module
So this function even does more than just looking up. It looks like your D6 code contains an error here: trying to translate terms from not localized vocabularies? (or probably not an error but at least a performance hit, on the other hand: perhaps some remains are still there in the locale tables giving false results)
Comment #5
wim leersBecause I avoid both Dutch & French and prefer English? :) It's a valid question though :)
This has now already been committed through #1216214: D7 i18n Taxonomy compatibility :)