1

Hi i been trying to achieve this. Need to transform all the numbers 1,2,3,4,5 (or 1956,1986 etc) to arabic numbers ١،٢،٣،٤،٥،٦،٧،٨،٩ I achieved this on a test like this

I have to use the value as >1< so the value="1" wont be affected, cause in db i need to store this as 1, not ١

So, before this the same code using just 1 as value, works great, replace everynumber for its similar in arabic , but also in value="" so this screw all the app. cause in other pages when i get the info i get ١ instead of 1

<script>
$(document).ready(function() {
    $("select").html(String($('select').html()).replace(/>1</g, '>١<'));
    $("select").html(String($('select').html()).replace(/>2</g, '>٢<'));
    $("select").html(String($('select').html()).replace(/>3</g, '>٣<'));
    $("select").html(String($('select').html()).replace(/>4</g, '>٤<'));
    $("select").html(String($('select').html()).replace(/>5</g, '>٥<'));
    $("select").html(String($('select').html()).replace(/>6</g, '>٦<'));
    $("select").html(String($('select').html()).replace(/>7</g, '>٧<'));
    $("select").html(String($('select').html()).replace(/>8</g, '>٨<'));
    $("select").html(String($('select').html()).replace(/>9</g, '>٩<'));
});
</script>

Does anyone has an idea, to make this happend in all body (not just selects) and also without altering the value="" numbers, on inputs, selects, or checks, or radio buttons,

thanks!

Ok just to clarify why i need to do this way.. Im using Rails

Im also using i18n but i couldn´t find a way to "translate" numbers

 <div id="altura">
    <%= f.label  :height %>
    <%= f.select :height, Player::HEIGHT.collect {|h| [ t("generales.#{h}"), h ] } , :include_blank => true %> <%= f.select :height_measure, Player::HEIGHT_MEASURE.collect {|h| [ t("generales.#{h}"), h ] } %>
    <%= f.select :inches, Player::INCH.collect {|h| [ t("generales.#{h}"), h ] }, {}, :style =>  (@player.inches.present? && @player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
    <%= f.label :inches, :id => 'inch_label', :style => (@player.inches.present? && @player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
  </div>
Moncho Chavez
  • 694
  • 2
  • 10
  • 31
  • Seems like a horrible approach to me. It seems obvious that these numbers are the contents of some tags. Interesting that you decided not to show what your markup looks like. I would never do it this way. – Jonathan Wood Sep 23 '13 at 21:53
  • Agree with @JonathanWood, there must be a better way than this. – Kyle Muir Sep 23 '13 at 21:53
  • 2
    Why are you doing this on the client? Send the data with Arabic font from the server, then make the value of option elements the appropriate English character. That way the Arabic digit will be displayed but the English character will be returned to the server. – RobG Sep 23 '13 at 21:54
  • [you could select all textNodes](http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery) and run the replaces on that (in other words: attributes are all left as is). – Wrikken Sep 23 '13 at 21:54
  • Im on Rails i will paste The tags. Now you can see an example of the HTML tags, im on rails, if you can figure out a better way to do it, pls be my guest.. Also note This page is in 10 Lenguages.. so thats why 9 lenguajes use normal numbers, thats why i store the numbers in database using normal numbers. thanks.. Also Rails send error if value 1 is converted into arabic number – Moncho Chavez Sep 23 '13 at 22:00

1 Answers1

1

I really don't understand why you just want to convert the digits, you really should be converting the entire text. But anyway, just for the heck of it, here's a function that does as you want.

Be aware that browsers will recognise sequences of Arabic numbers such as "21 23" and make them read right to left, i.e. "23 21" even though the digits in the number still read left to right (because that's how Arabic numbers work—even though their writing is right to left, their numbers read left to right, just like English).

function replaceDigits(id) {
  var el, text;
  var arabicCharCodes = {
    '0': '1632',
    '1': '1633',
    '2': '1634',
    '3': '1635',
    '4': '1636',
    '5': '1637',
    '6': '1638',
    '7': '1639',
    '8': '1640',
    '9': '1641'
  }

  // Do not process script elements, add others that have content but 
  // shouldn't be processed, e.g. maybe object
  var elementsToIgnore = { script:'script'};

  // If passed a string, expect ID so get element
  el = typeof id == 'string'? document.getElementById(id) : id;

  // If no element, return
  if (!el) return;

  var node, childNodes = el.childNodes;

  for (var i=0, iLen=childNodes.length; i<iLen; i++) {
    node = childNodes[i];

    // Recurse over all nodes and only replace the content of text nodes
    // in elements that are not in the ignore list
    if (node.nodeType == 1 && node.tagName && !(node.tagName in elementsToIgnore)) {
      replaceDigits(node);

    } else if (node.nodeType == 3) {
      text = node.data.split('');

      for (var j=0, jLen=text.length; j<jLen; j++) {

        if (text[j] in arabicCharCodes) {
          text[j] = String.fromCharCode(arabicCharCodes[text[j]]);
        }
      }
      node.data = text.join('');
    }
  }
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • All text its already translated using yml lenguage files with gem i18n, thanks i`ll check and update, – Moncho Chavez Sep 24 '13 at 04:31
  • Ah, so you may be using a database extract an only want to update option element text? In that case, just feed the above a select element reference or its ID and it will do the job, it won't affect the value attribute of the options (hopefully you aren't using the text as the value). – RobG Sep 24 '13 at 05:20
  • i have been trying to translate numbers, i was able to translate countries, using id, also everything like that, but numbers, that was a big issue cause i couldnt translate them :(, i will update question – Moncho Chavez Sep 24 '13 at 06:52
  • Can you let me know why "that browsers will recognise sequences of Arabic numbers such as "21 23" and make them read right to left, i.e. "23 21" .. or any link to translate numbers in rails, thanks – Moncho Chavez Sep 24 '13 at 14:31
  • I may have jumped the gun there. While Firefox will change 345-678 to ٣٤٥-٦٧٨, IE keeps the order some times (e.g. the text of an option) or swaps it like Firefox (e.g. in an LI), depending on the type of element. – RobG Sep 24 '13 at 23:12