I have ordered the terms of my vocabulary alphabetically.
I use localize terms for translation.
The terms are translated correctly and are in the same order as the default language.
As a result the translated terms are not listed alphabetically.
How can I fix this?
Maybe I should submit this issue at another module?

Thanks in advance!

Comments

alesand’s picture

I have solved this problem in my case (6.x-1.0) by making some changes in /i18n/i18ntaxonomy/i18ntaxonomy.module:

        $terms[$index]->$property = tt("taxonomy:term:$term->tid:$property", $term->$property);
      }
    }
  }
-  return $terms;
+  $hash = array();
+  foreach ($terms as $term) {
+    $hash[$term->name] = $term;
+  }
+  ksort ($hash);
+  return $hash;
}

And thanks for i18n!

tomsm’s picture

Yes, that worked! Thank you very much, alesand.
After I updated the i18taxonomy module with update.php everything was alphabetical for each language.

I think that this feature should be included in the module.

tomsm’s picture

Hi alesand,

My menu is alphabetical. But when I create a node that uses this vocabulary, the items are sorted alphabetically without keeping their parent-child hierarchy in the edit node screen. For example I have:

parent item 1
- child 1
- child 2
parent item 2
- child 1
- child 2

it becomes:
- child 1
- child 1
- child 2
- child 2
parent 1
parent 2

Can you fix this?

magoo’s picture

hello,

There is also a jQuery solution. As the other solution, it does not support hierarchical taxonomies.

However, sorting with any of those methods is weird because it short circuits the weights an administrator could give by ordering the terms.

At least, this solution avoids hacking the modules.

This is inspired by somebody else's webpage. I'll add the ref, when I find it again.

$(document).ready(function(){

$('select[id^=edit-taxonomy-]').not('#edit-taxonomy-3').not('#edit-taxonomy-7')
  .each(function() {
    var vals = [],
        selection = $(this).val()
        options = $('option', $(this));
    
    if (options.length == 0) return;
    
    options
      .each(function() {
        o = $(this);
        vals.push({
          value     : o.attr('value'),
          text      : o.html()
        })
      });
      
    vals.sort(function(a, b) {
      if(a.text>b.text){
        return 1;
      }
      else if (a.text==b.text){
        return 0;
      }
      else {
        return -1;
      }
    });
    

    var newOptions = [];
    for (var i = 0, l = vals.length; i < l; i++) {
        newOptions[i] = '<option value="'+ vals[i].value +'">'+ vals[i].text +'</option>';
    }
    
    $(this).empty().html(newOptions.toString()).val(selection);
      
  });
  
});

NOTE: the .not('#edit-taxonomy-X') is used to avoid applying on some taxonomies.

tomsm’s picture

Thanks for the tip. But where do I add this code?

magoo’s picture

in your theme's page.tpl.php

in a <script type="text/javascript"></script>

BR

Kars-T’s picture

Status: Active » Closed (fixed)

This was inactive over a year so I am closing this.

HakS’s picture

Version: 6.x-1.x-dev » 7.x-1.4
Status: Closed (fixed) » Needs review

Hello,

I'm trying to get this code to work on D7 version but I couldn't

This is the code:

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');
  $list = is_array($terms) ? $terms : array($terms);
  foreach ($list as $index => $term) {
    if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE)) {
      $localize[$index] = $term->tid;
    }
  }
  // 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);
  
+  $list=is_array($terms) ? $list : reset($list);
+  $hash = array();
+  foreach ($list as $term) {
+    $hash[$term->name] = $term;
+  }
+  ksort ($hash);
+  return $hash;
}
Jose Reyero’s picture

Status: Needs review » Needs work

Since this may be an array of objects, maybe we need some sorting function, see http://es.php.net/manual/es/function.usort.php

And we should be sorting only when the previous array (before translating) is oredered alphabetically, so we need some extra options here.

Cyclodex’s picture

Question: I tried to discover what this code should fix, but somehow the whole function is not reflecting any changes for my site. Don't understand it exactly what's going on...

What I could fix for now is my exposed filters in views, see #1312074: Sort exposed translated filters by their value. with a hook_form_alter().
But I think this solution is not the correct one, we should try to fix this more generally when generating the term list. But somehow I do understand the things not good enough, so for now could not find a way how and especially where we should fix this.

i18n_taxonomy ?
i18n_views ?

Could it be that this code here is not working for me due I am using i18n_views ?
So then I would have to contact i18n_views team for some help...

I would like to help out whit this case, but would be nice to get some feedback to get me back on track :)

Regards Cyclodex

Cyclodex’s picture

Due this seems to be hard to fix I just wanted to note, that I implemented some other hook_form_alter() to order all my select and checkboxes fields, finally I got a correct ordered list in a foreign language.
So the problem is done for me with this workaround.

Jose Reyero’s picture

Component: Miscellaneous » Taxonomy
Issue summary: View changes

Moving to the right component

A.Kotov’s picture

Hi, is there any chance this happens for 7 ? ))

nerdcore’s picture

Although the suggestion in #11 sounds sensible for forms, I'm having an issue where displaying a localized taxonomy in the node display shows a secondary-language list in the primary language's sort order. Also the primary language doesn't appear sorted properly itself (this is clearly not an i18n-specific issue).

This issue is nearly 5 years old. I was going to submit a patch which would address the form display suggested in #11 but I'm unsure how to affect other places where this sort is incorrect, such as node display list of terms.

Any suggestion on how to order the terms as displayed by the Views module?

I still need forms sorted alphabetically using a localized taxonomy as well.

nerdcore’s picture

Version: 7.x-1.4 » 7.x-1.11
nerdcore’s picture

Version: 7.x-1.11 » 7.x-1.x-dev

I've confirmed I am still experiencing primary-language sort order on secondary language lists in 7.x-1.x-dev.

nerdcore’s picture

Adding this related issue in case someone finds it useful:

#2283537: I18n of taxonomy in exposed filter : translations and orders

jasom’s picture

Is there any progress? This is similar issue from i18n_views #1645304: Sort by "Taxonomy term: Name (translated)" field

noah’s picture

Here's a quick and dirty solution to sorting based on translated term name in Views. Doesn't require i18n_views, but does use transliteration (though it could be easily modified to work without if it's not required). This is the "Style output" template:

<?php
global $language;
if ($language->language != 'en') {
  $sort_rows = array();
  foreach ($rows as $id => $row) $sort_rows[$id] = strip_tags($row);
  uasort($sort_rows, '[THEMENAME]_transliterate_comparison');
}
else $sort_rows = $rows;
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($sort_rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $rows[$id]; ?>
  </div>
<?php endforeach; ?>
<?php
function [THEMENAME]_transliterate_comparison($a, $b) {
  $at = transliteration_get($a);
  $bt = transliteration_get($b);
  return strcmp($at, $bt);
}
?>