summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--i18n_string/i18n_string.module36
-rw-r--r--i18n_taxonomy/i18n_taxonomy.module40
-rw-r--r--i18n_taxonomy/i18n_taxonomy.pages.inc17
3 files changed, 57 insertions, 36 deletions
diff --git a/i18n_string/i18n_string.module b/i18n_string/i18n_string.module
index b1a651d..19f81ad 100644
--- a/i18n_string/i18n_string.module
+++ b/i18n_string/i18n_string.module
@@ -299,9 +299,7 @@ function i18n_string_locale_translate_import_form_submit($form, &$form_state) {
* the following to your settings.php
*
* @param $langcode
- * @param $reset
- * Whether to reset the internal cache for the translated langcode.
- *
+ * Optional language code to check. It will default to current request language.
* @code
* // Enable translation of specific language. Language code is 'xx'
* $conf['i18n_string_translate_langcode_xx'] = TRUE;
@@ -309,8 +307,9 @@ function i18n_string_locale_translate_import_form_submit($form, &$form_state) {
* $conf['i18n_string_translate_langcode_yy'] = FALSE;
* @endcode
*/
-function i18n_string_translate_langcode($langcode, $reset = FALSE) {
- $translate = &drupal_static(__FUNCTION__ , array(), $reset);
+function i18n_string_translate_langcode($langcode = NULL) {
+ $translate = &drupal_static(__FUNCTION__);
+ $langcode = isset($langcode) ? $langcode : i18n_langcode();
if (!isset($translate[$langcode])) {
$translate[$langcode] = variable_get('i18n_string_translate_langcode_' . $langcode, language_default('language') != $langcode);
}
@@ -713,6 +712,8 @@ function i18n_string_object_info($type = NULL, $property = NULL) {
function i18n_string_object_translate($type, $object, $options = array()) {
$langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode();
if (i18n_string_translate_langcode($langcode)) {
+ // Object properties will be returned without filtering as in the original one.
+ $options += array('sanitize' => FALSE);
return i18n_object($type, $object)->translate($langcode, $options);
}
else {
@@ -756,6 +757,31 @@ function i18n_string_object_translate_page($object_type, $object_value, $langcod
}
/**
+ * Preload all strings for this textroup/context.
+ *
+ * This is a performance optimization to load all needed strings with a single query.
+ *
+ * Examples of valid string name to search are:
+ * - 'taxonomy:term:*:title'
+ * This will find all titles for taxonomy terms
+ * - array('taxonomy', 'term', array(1,2), '*')
+ * This will find all properties for taxonomy terms 1 and 2
+ *
+ * @param $name
+ * Specially crafted string name, it may take '*' and array parameters for each element.
+ * @param $langcode
+ * Language code to search translations. Defaults to current language.
+ *
+ * @return array()
+ * String objects indexed by context.
+ */
+function i18n_string_translation_search($name, $langcode = NULL) {
+ $langcode = isset($langcode) ? $langcode : i18n_langcode();
+ list ($textgroup, $context) = i18n_string_context($name);
+ return i18n_string_textgroup($textgroup)->multiple_translation_search($context, $langcode);
+}
+
+/**
* Update / create translation for a certain source.
*
* @param $name
diff --git a/i18n_taxonomy/i18n_taxonomy.module b/i18n_taxonomy/i18n_taxonomy.module
index e7d6515..22f4be0 100644
--- a/i18n_taxonomy/i18n_taxonomy.module
+++ b/i18n_taxonomy/i18n_taxonomy.module
@@ -140,15 +140,16 @@ function i18n_taxonomy_menu_alter(&$items) {
// Also views module takes over this page so this won't work if views enabled.
if (variable_get('page_manager_term_view_disabled', TRUE)) {
// Taxonomy term page. Localize terms.
- $items['taxonomy/term/%taxonomy_term']['module'] = 'i18n_taxonomy';
$items['taxonomy/term/%taxonomy_term']['page callback'] = 'i18n_taxonomy_term_page';
+ $items['taxonomy/term/%taxonomy_term']['title callback'] = 'i18n_taxonomy_term_name';
$items['taxonomy/term/%taxonomy_term']['file'] = 'i18n_taxonomy.pages.inc';
+ $items['taxonomy/term/%taxonomy_term']['module'] = 'i18n_taxonomy';
}
// Localize autocomplete
- $items['taxonomy/autocomplete']['module'] = 'i18n_taxonomy';
$items['taxonomy/autocomplete']['page callback'] = 'i18n_taxonomy_autocomplete_field';
$items['taxonomy/autocomplete']['file'] = 'i18n_taxonomy.pages.inc';
+ $items['taxonomy/autocomplete']['module'] = 'i18n_taxonomy';
}
/**
@@ -995,30 +996,31 @@ function i18n_taxonomy_translate_terms($taxonomy, $langcode, $fullterms = TRUE)
* Localize taxonomy terms for localizable vocabularies.
*
* @param $terms
- * Array of term objects.
- * @param $fields
- * Object properties to localize.
+ * Array of term objects or term object.
* @return
* Array of terms with the right ones localized.
*/
-function i18n_taxonomy_localize_terms($terms, $fields = array('name')) {
+function i18n_taxonomy_localize_terms($terms) {
+ // If not localizable language just return. Performance optimizations.
+ if (!i18n_string_translate_langcode()) {
+ return $terms;
+ }
$object_info = i18n_object_info('taxonomy_term');
- $terms = is_array($terms) ? $terms : array($terms);
- foreach ($terms as $index => $term) {
+ $list = is_array($terms) ? $terms : array($terms);
+ foreach ($list as $index => $term) {
if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE)) {
- foreach ($fields as $property) {
- // Allow to translate descriptions, therefore the format is needed.
- $info = $object_info['string translation']['properties'][$property];
- if (!is_array($info) || !isset($info['format'])) {
- $term->$property = i18n_string(array('taxonomy', 'term', $term->tid, $property), $term->$property);
- }
- else {
- $term->$property = i18n_string(array('taxonomy', 'term', $term->tid, $property), $term->$property, array('format' => $term->format, 'sanitize' => FALSE));
- }
- }
+ $localize[$index] = $term->tid;
}
}
- return $terms;
+ // If multiple terms, preload all translations, then run object translation.
+ if (!empty($localize)) {
+ i18n_string_translation_search(array('taxonomy', 'term', $localize, '*'));
+ foreach ($localize as $index => $tid) {
+ $list[$index] = i18n_string_object_translate('taxonomy_term', $list[$index]);
+ }
+ }
+ // Return array or simple object, depending on incoming format.
+ return is_array($terms) ? $list : reset($list);
}
/**
diff --git a/i18n_taxonomy/i18n_taxonomy.pages.inc b/i18n_taxonomy/i18n_taxonomy.pages.inc
index e32d865..7f1312e 100644
--- a/i18n_taxonomy/i18n_taxonomy.pages.inc
+++ b/i18n_taxonomy/i18n_taxonomy.pages.inc
@@ -14,7 +14,7 @@
* The page content.
*/
function i18n_taxonomy_term_page($term) {
- i18n_taxonomy_localize_terms($term);
+ $term = i18n_taxonomy_localize_terms($term);
// Build breadcrumb based on the hierarchy of the term.
$current = (object) array(
'tid' => $term->tid,
@@ -23,7 +23,7 @@ function i18n_taxonomy_term_page($term) {
// presumption. Make this behavior configurable per vocabulary or term.
$breadcrumb = array();
while ($parents = taxonomy_get_parents($current->tid)) {
- i18n_taxonomy_localize_terms($parents);
+ $parents = i18n_taxonomy_localize_terms($parents);
$current = array_shift($parents);
$breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
}
@@ -77,18 +77,11 @@ function theme_i18n_taxonomy_term_page($tids, $result) {
// Only display the description if we have a single term, to avoid clutter and confusion.
if (count($tids) == 1) {
- $term = taxonomy_get_term($tids[0]);
- if (i18n_taxonomy_vocabulary($term->vid) & I18N_MODE_LOCALIZE) {
- $description = i18n_string("taxonomy:term:$term->tid:description", $term->description);
- }
- else {
- $description = $term->description;
- }
-
+ $term = i18n_taxonomy_localize_terms(taxonomy_get_term($tids[0]));
// Check that a description is set.
- if (!empty($description)) {
+ if (!empty($term->description)) {
$output .= '<div class="taxonomy-term-description">';
- $output .= filter_xss_admin($description);
+ $output .= filter_xss_admin($term->description);
$output .= '</div>';
}
}