jquery - 翻译国名和排序 [英] jquery - translating country names and sorting them

查看:102
本文介绍了jquery - 翻译国名和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户更改页面的语言代码后(通过语言选择列表),我使用jquery对国名列表进行排序。这是一个相关的



这是我的jQuery代码:

 函数sortAddressTypes(){
//在语言代码更改后,按字母顺序对地址国家/地区样式类型列表(国家/地区名称)进行排序。
var selected = $(#id_address_country_style_type)。val(); //保存用户选择的值。
var options = $('#id_address_country_style_type option');
var arr = options.map(function(_,o){
return {t:$(o).text(),v:o.value};
})。get ();

arr.sort(function(o1,o2){
return o1.t.toLowerCase()> o2.t.toLowerCase()?1:o1.t.toLowerCase() < o2.t.toLowerCase()?-1:0;
});
$ b $ options.each(function(i,o){
console.log(i);
o.value = arr [i] .v;
$ (o).text(arr [i] .t);
});

$(#id_address_country_style_type)。val(selected); //分配保存的用户选择的值。


解决方案

您可以使用<$ c在你的 sort()实现中使用$ c> localeCompare()来比较包含变音符的字符串:

  arr.sort(函数(o1,o2){
return o1.t.toLowerCase()。localeCompare(o2.t.toLowerCase());
});

localCompare()参考

I have used jquery to sort a list of country names after the user changes the language code of the page (via a language select list). Here is a related post.

The translaion of country names appears to operate correctly, but the sorting (A-Z) of the translated country names does not quite work.

I have noticed that when a country name has a diaeresis / diacratic mark to the 1st letter of the country name, the said country name is relegated to the bottom of the list.

When the English country names have been translated to the German language. The country names with diaeresis / diacratic marks appear last. For example, the counrty name Österreich (Austria - in the English language) should be sorted after country names beginning with the letter O.

Is there a certain trick to sorting that will include letters of words with diaeresis / diacratic marks?

Here is a visual example of my dilema:

Here is my jQuery code:

function sortAddressTypes() {
    // sort the address country style types list (country name) in alphabetical order after language code has changed.
    var selected = $("#id_address_country_style_type").val();  // preserve the users selected value.
    var options = $('#id_address_country_style_type option');
    var arr = options.map(function(_, o) {
        return {t: $(o).text(), v: o.value};
    }).get();

    arr.sort(function(o1, o2) {
        return o1.t.toLowerCase() > o2.t.toLowerCase() ? 1 : o1.t.toLowerCase() < o2.t.toLowerCase() ? -1 : 0;
    });

    options.each(function(i, o) {
        console.log(i);
        o.value = arr[i].v;
        $(o).text(arr[i].t);
    });

    $("#id_address_country_style_type").val(selected);  // assign the preserved users selected value.
}

解决方案

You can use localeCompare() within your sort() implementation to compare strings containing diacritics:

arr.sort(function(o1, o2) {
    return o1.t.toLowerCase().localeCompare(o2.t.toLowerCase());
});

localCompare() reference

这篇关于jquery - 翻译国名和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆