在 Ruby 中将所有非 ASCII 字符替换为其 ASCII 等效字符的最简单方法是什么?

发布于 2025-01-08 16:52:28 字数 294 浏览 0 评论 0原文

可能的重复:
Ruby 中的音译

我正在寻找一种简单的方法来将此类字符串转换为:

  • “spaß”到“spass” “
  • “über”到“ueber”
  • 等。

这是从人名生成有效用户名所必需的。

Possible Duplicate:
Transliteration in ruby

I am searching for a simple way to convert strings like these:

  • "spaß" to "spass"
  • "über" to "ueber"
  • etc.

This is needed for generating valid usernames from names of people.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2025-01-15 16:52:28

这称为音译。可以使用 Iconv 类。

尝试以下操作之一(首先需要“iconv”):

Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
Iconv.iconv('ascii//translit', 'utf-8', string).to_s

irb(main):013:0> Iconv.iconv('ascii//translit', 'utf-8', 'spaß').to_s
=> "spass"
irb(main):014:0> Iconv.iconv('ascii//translit', 'utf-8', 'crêpes').to_s
=> "crepes"
irb(main):017:0> Iconv.iconv('ascii//translit', 'utf-8', 'über').to_s
=> "uber"

还有一个 iconv 命令行实用程序。有关该内容和一些 Ruby 示例的更多信息(搜索“ruby”)此处

另一种方法是 Unidecode< /a>,我猜这是受到原始 Perl 实现的启发。我还没有在 Ruby 版本中使用过它,但它应该可以更好地进行多字符扩展(这显然是您想要的)。

最后,如果您正在运行 Rails,您可能会找到此线程 有趣。它详细介绍了替代音译方法之间的一些差异,并展示了在 Rails 核心内执行此操作的方法 (ActiveSupport::Inflector.transliterate)

This is called transliteration. An approximation of this (see examples) can be performed using the Iconv class.

Try one of the following (require 'iconv' first):

Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
Iconv.iconv('ascii//translit', 'utf-8', string).to_s

irb(main):013:0> Iconv.iconv('ascii//translit', 'utf-8', 'spaß').to_s
=> "spass"
irb(main):014:0> Iconv.iconv('ascii//translit', 'utf-8', 'crêpes').to_s
=> "crepes"
irb(main):017:0> Iconv.iconv('ascii//translit', 'utf-8', 'über').to_s
=> "uber"

There's also an iconv command line utility. More information on that and some Ruby examples (search for 'ruby') here.

An alternative to this is Unidecode, which I guess was inspired by the original Perl implementation. I haven't used it in its Ruby incarnation, but it should do multi-char expansions (which apparently you want) better.

Finally, if you're running Rails, you may find this thread interesting. It details some differences between alternative approaches to transliteration, and shows a way to do this within the Rails core (ActiveSupport::Inflector.transliterate)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文