Rails 3 change default english numbers to arabic numbers

孤街醉人 提交于 2019-12-04 19:33:39

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

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.

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

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