Numbers localization in Web applications

前端 未结 8 658
渐次进展
渐次进展 2020-12-23 14:30

How can I set the variant of Arabic numeral without changing character codes ?

Eastern Arabic      ۰   ۱   ۲   ۳   ٦   ٥   ٤   ۷   ۸   ۹
Persian variant              


        
8条回答
  •  有刺的猬
    2020-12-23 15:11

    I have created a jquery plugin that can convert Western Arabic numbers to Eastern ones (Persian only). But it can be extended to convert a number to any desired numeral system. My jQuery plugin has two advantages:

    1. Detect and convert numbers properly in child nodes.
    2. Detect and convert point characters appropriately.

    You can clone this plugin from github. My plugin code:

    (function( $ ){
      $.fn.persiaNumber = function() {
          var groupSelection = this;
          for(i=0; i< groupSelection.length ; i++){
              var htmlTxt = $(groupSelection[i]).html();
              var trueTxt = convertDecimalPoint(htmlTxt);
              trueTxt = convertToPersianNum(trueTxt);
              $(groupSelection[i]).html(trueTxt);
          }
    function convertToPersianNum(htmlTxt){
              var otIndex = htmlTxt.indexOf("<");
              var ctIndex = htmlTxt.indexOf(">");
              if(otIndex == -1 && ctIndex == -1 && htmlTxt.length > 0){
                var trueTxt = htmlTxt.replace(/1/gi, "۱").replace(/2/gi, "۲").replace(/3/gi, "۳").replace(/4/gi, "۴").replace(/5/gi, "۵").replace(/6/gi, "۶").replace(/7/gi, "۷").replace(/8/gi, "۸").replace(/9/gi, "۹").replace(/0/gi, "۰");
                return trueTxt;
              }
                  var tag = htmlTxt.substring(otIndex,ctIndex + 1);
                  var str = htmlTxt.substring(0,otIndex);
                  str = convertDecimalPoint(str);
                  str = str.replace(/1/gi, "۱").replace(/2/gi, "۲").replace(/3/gi, "۳").replace(/4/gi, "۴").replace(/5/gi, "۵").replace(/6/gi, "۶").replace(/7/gi, "۷").replace(/8/gi, "۸").replace(/9/gi, "۹").replace(/0/gi, "۰");
                  var refinedHtmlTxt = str + tag;
                  var htmlTxt = htmlTxt.substring(ctIndex + 1, htmlTxt.length);
                  if(htmlTxt.length > 0 && otIndex != -1 || ctIndex != -1){
                      var trueTxt = refinedHtmlTxt;
                      var trueTxt =  trueTxt + convertToPersianNum(htmlTxt); 
                  }else{
                        return refinedHtmlTxt+ htmlTxt;
                  }
                  return trueTxt;         
    
        }
    function convertDecimalPoint(str){
        for(j=1;j 47 &&  str.charCodeAt(j-1) < 58 && str.charCodeAt(j+1) > 47 &&  str.charCodeAt(j+1) < 58 && str.charCodeAt(j) == 46)
                str = str.substring(0,j) + '٫' + str.substring(j+1,str.length);
        }
        return str;
    }
    };
    })( jQuery );
    

    http://jsfiddle.net/VPWmq/2/

提交回复
热议问题