【问题标题】:how to replace non ascii character with their respective character in ruby 1.8.7如何在 ruby​​ 1.8.7 中用它们各自的字符替换非 ascii 字符
【发布时间】:2016-09-27 13:58:22
【问题描述】:

我用iconv替换了字符:

<%= Iconv.iconv("ascii//translit", "utf-8", "ENDÜœSTRIYEL").to_s %>

它显示, END?oeSTRIYEL

而在 irb 中显示如下:

irb(main):006:0> Iconv.iconv('ascii//translit', 'utf-8', 'ENDÜœSTRIYEL').to_s
=> "ENDUoeSTRIYEL"

如何获得非ASCII字符的完整翻译,如irb?

谢谢。

【问题讨论】:

    标签: ascii non-ascii-characters iconv ruby-1.8.7


    【解决方案1】:

    glibc 的 iconv 工具有一个取决于语言环境的音译:

    $ echo "ENDÜœSTRIYEL" | LC_ALL=C iconv -f utf-8 -t ascii//translit
    END?oeSTRIYEL
    $ echo "ENDÜœSTRIYEL" | LC_ALL=de_DE.UTF-8 iconv -f utf-8 -t ascii//translit
    ENDUEoeSTRIYEL
    $ echo "ENDÜœSTRIYEL" | LC_ALL=ja_JP.UTF-8 iconv -f utf-8 -t ascii//translit
    ENDUoeSTRIYEL
    

    如您所见,三种不同语言环境的三种不同结果。

    如果您托管的服务器旨在处理来自不同国家/地区的用户的输入,您有两种选择:

    • 为所有用户使用单一语言环境,希望它对所有人都足够好。
    • 为每次转换临时切换区域设置(使用uselocale,而不是setlocale)。但是,我不知道 uselocale 是否在 Ruby 中可用。

    【讨论】:

    • 但我使用的是 ruby​​ 1.8.7 .. 我不能像上面提到的那样使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2017-04-13
    • 2015-08-14
    • 1970-01-01
    • 2013-09-08
    • 2017-12-10
    相关资源
    最近更新 更多