Localization of numeric number in Rails

蹲街弑〆低调 提交于 2019-12-07 10:42:08

问题


[Sorry for the new post, but my first one was focusing to arabic/persian numbers but it seems the issue is larger.]

I wonder if someone had done a gem to handle the localization of numeric number in ruby/rails. I18n official locales (https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) seems not to take care of that.

It's kind of complex to do by helpers.

Arabic is simple:

٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩

Persian too:

۰   ١   ۲   ۳   ۴   ۵   ۶   ۷   ۸   ۹   ۱۰

But all languages don't match 1-1 conversion with english, Chinese for example:

0: 〇 (零) líng
1: 一 (壹) yī
2: 二 (Simplified:贰;Traditional:貳) èr
3: 三 (Simplified:叁;Traditional:叄、參) sān
4: 四 (肆) sì
5: 五 (伍) wǔ
6: 六 (Simplified:陆;Traditional:陸) liù
7: 七 (柒) qī
8: 八 (捌) bā
9: 九 (玖) jiǔ
10: 十 (拾) shí
100: 百 (佰) bǎi
1000: 千 (仟) qiān
10,000: Simplified:万;Traditional萬 wàn
100,000,000: Simplified:亿;Traditional億 yì
1,000,000,000,000: 兆 zhào                                           

We have other language with similar issues. It seems weird that nobody seems to have face that before.

Do you know the best way to handle the number in all locales?


回答1:


Ok, I've come up with that:

  def number to_convert, locale, text = nil,
    to_convert = to_convert.to_i.to_s
    case locale
    when 'ar'
      to_convert = to_convert.unpack('U*').map{ |e| e + 1584 }.pack('U*')
      text ? to_convert + ' ' + text : to_convert
    when 'fa'
      to_convert = to_convert.unpack('U*').map{ |e| e + 1728 }.pack('U*')
      text ? to_convert + ' ' + text : to_convert
    when 'hi'
      to_convert = to_convert.unpack('U*').map{ |e| e + 2358 }.pack('U*')
      text ? to_convert + ' ' + text : to_convert
    else
      text ? to_convert + ' ' + text : to_convert
    end
  end

Other language don't need custom localization. Ie. Chineese/Japanese people understand our number and it will be weird to support their local number as local people are using our number on the web.



来源:https://stackoverflow.com/questions/18124562/localization-of-numeric-number-in-rails

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