Localizing a Number in Rails

[Sorry for the new post, but my first focused on the Arabic / Persian issue, but it seems the problem is bigger.]

I wonder if someone did a jewel handling the localization of a numeric number in ruby ​​/ rails. I18n official places ( https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale ) doesn't seem to take care of this.

This is difficult to do with the help of assistants.

Arabic is simple:

٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩

Persian too:

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

But all languages ​​do not correspond to the conversion 1-1 with English, for example, Chinese:

 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 another language with similar problems. It seems strange that no one has come across this before.

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

+4
source share
1 answer

Ok, I came up with this:

  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 

In another language, custom localization is not required. I.e. The Chinese / Japanese understand our number, and it will be strange to maintain their local number, as local residents use our number on the Internet.

+7
source

Source: https://habr.com/ru/post/1495931/


All Articles