正如我们所知,我们可以在Ruby 1.9.3中使用带有Iconv
标志的TRANSLIT
,它将用等效的ASCII替换重音字符,只有在目标编码中不存在< /强>
使用示例:
require 'iconv'
z = "Håkan"
Iconv.conv("windows-1250//TRANSLIT", "UTF-8", z)
# => outputs "Hakan" (with diactric removed)
pl = "zażółć"
Iconv.conv("windows-1250//TRANSLIT", "UTF-8", pl)
# => outputs "zażółć" (because windows-1250 contains all this characters)
# well, to be honest it outputs "za\xBF\xF3\xB3\xE6" because of terminal settings
# but I hope you understand
但不推荐Iconv
,建议改为使用String#encode
。
然而,当使用#encode
时会出现问题:
z.encode('windows-1250', 'utf-8')
Encoding::UndefinedConversionError: U+00E5 to WINDOWS-1250 in conversion from UTF-8 to WINDOWS-1250
使用iconv
而不是Ruby 2 +,有没有办法让行为与TRANSLIT
String#encode
旗帜相似?
答案 0 :(得分:-1)
如果您知道会发生什么,那么您可以在哈希中指定替换:
z = "Håkan"
p z.encode('windows-1250', 'utf-8', fallback: {"å"=>"a"}) # => Hakan