Rails 3 change default english numbers to arabic numbers

浪子不回头ぞ 提交于 2020-01-01 19:48:06

问题


I want to change the default numbers from english to arabic when the user switches to the arabic interface.

13 => ١٣

89 => ٨٩

What is the best way to tackle this problem?


回答1:


I add in helper module

ARABIC_NUMBERS = %w(٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩)
def ta numbers
  numbers = numbers.to_s if numbers.is_a? Integer
  results = numbers.chars.map { |char| ARABIC_NUMBERS[char.to_i] }.join
end



回答2:


Check this code : https://github.com/gdotdesign/rails-arabic-convert/blob/master/app/helpers/convert_helper.rb.

It's a helper to convert a english number to an arabic number.




回答3:


I came up with this quick solution. I added the following function in the ApplicationHelper

def tn(num)
  num.to_s.split(//).map{|r|t("n"+r)}.join
end

Then added translations for each number from 0 to 9 in the config/locals/ar.yml with the format below:

n1: "١"
n2: "٢"
n3: "٣"
.
.
.

Now we can call the new numeric translation function by tn(13) which will output ١٣ in arabic localization



来源:https://stackoverflow.com/questions/11800798/rails-3-change-default-english-numbers-to-arabic-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!