Ruby 1.9:我怎样才能正确地大写&小写多字节字符串?
因此,matz 决定在 ruby 1.9.1 中将 upcase
和 downcase
限制为 /[AZ]/i
。
ActiveSupport::Multibyte
长期以来通过 String#mb_chars
在 ruby 1.8.x 中实现了出色的国际化大小写调整。
然而,当在 ruby 1.9.1 下尝试时,它似乎不起作用。这是我编写的一个简单的测试脚本,以及我得到的输出:
$ cat test.rb
# encoding: UTF-8
puts("@ #{RUBY_VERSION} " + (__ENCODING__ rescue $KCODE).to_s)
sd, su = "Iñtërnâtiônàlizætiøn", "IÑTËRNÂTIÔNÀLIZÆTIØN"
def ps(u, d, k); puts "%-30s: %24s / %-24s" % [k, u, d] end
ps sd.upcase, su.downcase, "Plain ruby"
require 'rubygems'; require 'active_support'
ps sd.upcase, su.downcase, "With active_support"
ps sd.mb_chars.upcase.to_s, su.mb_chars.downcase.to_s, "With active_support mb_chars"
$ ruby -KU test.rb
@ 1.8.7 UTF8
Plain ruby : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars : IÑTËRNÂTIÔNÀLIZÆTIØN / iñtërnâtiônàlizætiøn
$ ruby1.9 test.rb
@ 1.9.1 UTF-8
Plain ruby : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
那么,如何使用 ruby 1.9.1 获得国际化的 upcase
和 downcase
?
更新
我应该补充一点,我还使用来自当前 master
、2-3-*
和 3-0-unstable
的 Rails 分支的 ActiveSupport 进行了测试,网址为GitHub。结果相同。
So matz made the decision to keep upcase
and downcase
limited to /[A-Z]/i
in ruby 1.9.1.
ActiveSupport::Multibyte
has long had great i18n case jiggering in ruby 1.8.x via String#mb_chars
.
However, when tried under ruby 1.9.1, it doesn't seem to work. Here's a simple test script I wrote, along with the output I'm getting:
$ cat test.rb
# encoding: UTF-8
puts("@ #{RUBY_VERSION} " + (__ENCODING__ rescue $KCODE).to_s)
sd, su = "Iñtërnâtiônàlizætiøn", "IÑTËRNÂTIÔNÀLIZÆTIØN"
def ps(u, d, k); puts "%-30s: %24s / %-24s" % [k, u, d] end
ps sd.upcase, su.downcase, "Plain ruby"
require 'rubygems'; require 'active_support'
ps sd.upcase, su.downcase, "With active_support"
ps sd.mb_chars.upcase.to_s, su.mb_chars.downcase.to_s, "With active_support mb_chars"
$ ruby -KU test.rb
@ 1.8.7 UTF8
Plain ruby : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars : IÑTËRNÂTIÔNÀLIZÆTIØN / iñtërnâtiônàlizætiøn
$ ruby1.9 test.rb
@ 1.9.1 UTF-8
Plain ruby : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars : IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
So, how do I get internationalized upcase
and downcase
with ruby 1.9.1?
update
I should add that I also tested with ActiveSupport from the current master
, 2-3-*
and 3-0-unstable
rails branches at GitHub. Same results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于通过
ruby upcase utf8
来自 Google 的任何人:解决方案是使用
mb_chars
。文档:
for anybody coming from Google by
ruby upcase utf8
:solution is to use
mb_chars
.Documentation:
大小写转换取决于语言环境,并不总是往返,这就是为什么 Ruby 1.9 没有涵盖它(请参阅 此处 和 此处)
unicode-util gem 应该满足您的需求。
Case conversion is locale dependent and doesn't always round-trip, which is why Ruby 1.9 doesn't cover it (see here and here)
The unicode-util gem should address your needs.
大小写转换很复杂并且依赖于区域设置。幸运的是,Martin Dürst 在 Ruby 中添加了完整的 Unicode 大小写映射 2.4:
输出:
Case conversion is complicated and locale-dependent. Fortunately, Martin Dürst added full Unicode case mapping in Ruby 2.4:
Output:
如果您只想在 HTML 中以大写形式显示字符串,CSS 文本-transform 可能是一个更好的解决方案:
If you just wanna display the strings in uppercase in HTML, CSS text-transform could be a better solution: