将 umlaute (äüö) 替换为 rails 中的 SEO 链接 - 最佳方式 [英] Replace umlaute (äüö) for SEO link in rails - best way

查看:32
本文介绍了将 umlaute (äüö) 替换为 rails 中的 SEO 链接 - 最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 permalink_fu 插件从标题创建永久链接.我的问题是:如果标题包含德语字符,它们只是替换为_".

I'm using the permalink_fu plugin to create permalinks from titles. My problem is: If the title contains german characters, they are just replaced with '_'.

我需要的是替代品ä 与 aeü 与 ueö 与 oe

What I need is something that replaces ä with ae ü with ue ö with oe

我找到了 String.tr 但这里的问题是它用 1 个替换替换了 1 个字符,因此它可以用于替换

I fount String.tr but the problem here is that it replaces 1 character with 1 replacement, so it would work for replacing

é 加 eø 带 o

有没有人有一个好的和干净的解决方案?

Does anyone have a nice and clean solution for that?

谢谢

推荐答案

使用 String.gsub():

"ich bin doch nicht blöd, mann!".gsub(/[äöü]/) do |match|
    case match
        when "ä"
          'ae'
        when "ö"
          'oe'
        when "ü"
          'ue'
    end
end

当然可以使用查找表来改进查找,但原理要明确.

Of course, the lookup can be improved by using a lookup table, but the principle should be clear.

这篇关于将 umlaute (äüö) 替换为 rails 中的 SEO 链接 - 最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆