Rails中数字编号的本地化(Localization of numeric number in Rails)

编程入门 行业动态 更新时间:2024-10-23 16:24:28
Rails中数字编号的本地化(Localization of numeric number in Rails)

[对不起这篇新帖子,但我的第一篇专注于阿拉伯语/波斯语数字,但似乎问题更大。]

我想知道是否有人做过宝石来处理ruby / rails中数字数字的本地化。 I18n官方语言环境( https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale )似乎没有照顾到这一点。

帮助者做的很复杂。

阿拉伯语很简单:

٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩

波斯语:

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

但是所有语言都不匹配1-1英语转换,例如中文:

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

我们有其他类似问题的语言。 以前没有人似乎有过这样的表面,这似乎很奇怪。

您知道在所有语言环境中处理号码的最佳方法吗?

[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?

最满意答案

好的,我想出了:

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

其他语言不需要自定义本地化。 IE浏览器。 中国/日本人了解我们的号码,因为当地人在网上使用我们的号码,支持他们的本地号码会很奇怪。

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.

更多推荐

本文发布于:2023-07-06 07:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编号   数字   Rails   number   numeric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!