Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带有非拉丁字符的Rails友好id_Ruby On Rails_Ruby On Rails 4_Arabic_Hebrew_Friendly Id - Fatal编程技术网

Ruby on rails 带有非拉丁字符的Rails友好id

Ruby on rails 带有非拉丁字符的Rails友好id,ruby-on-rails,ruby-on-rails-4,arabic,hebrew,friendly-id,Ruby On Rails,Ruby On Rails 4,Arabic,Hebrew,Friendly Id,我有一个模型,我使用它的友好id作为slug: extend FriendlyId friendly_id :slug_candidates, :use => :scoped, :scope => :account def slug_candidates :title_and_sequence end def title_and_sequence slug = normalize_friendly_id(title) : # some login to

我有一个模型,我使用它的友好id作为slug:

extend FriendlyId
friendly_id :slug_candidates, :use => :scoped, :scope => :account

def slug_candidates
  :title_and_sequence
end


def title_and_sequence
  slug = normalize_friendly_id(title)
      :
  # some login to add sequence in case of collision
      :
end
我的问题是,当我使用非拉丁字符(阿拉伯、希伯来语等)时,我会得到一个空的slug。 有什么好的简单的解决办法吗


更新 为了澄清我的问题,我希望有与WordPress相同的行为,这意味着:

+--------------------+----------------------------------------------------+
| Title              | url                                                |
+--------------------+----------------------------------------------------+
| Hello World!!      | /hello-world                                       |
+--------------------+----------------------------------------------------+
| Helló Világ        | /hello-vilag                                       |
+--------------------+----------------------------------------------------+
| שלום עולם          | /%D7%A9%D7%9C%D7%95%D7%9D-%D7%A2%D7%95%D7%9C%D7%9D |
+--------------------+----------------------------------------------------+
| مرحبا              | %D9%85%D8%B1%D8%AD%D8%A8%D8%A7                     |
+--------------------+----------------------------------------------------+

(阿拉伯语和希伯来语在现代浏览器中都被翻译成原始的可读字符)。

有一种Rails API方法可以实现这一点

示例用法:

transliterate('Ãrøskøbing')
# => "AEroskobing"
默认情况下,它只支持拉丁语和俄语,但您也应该能够找到其他字母表的规则(如链接文档中所述)

编辑
要实现与wordpress相同的行为,只需使用url编码,如下例所示

URI::encode('שלום') => "%D7%A9%D7%9C%D7%95%D7%9D"

感谢@michalszyndel注释和想法,我成功地获得了以下解决方案,希望它能对更多人有所帮助

首先,如何在slug中生成非unicode字符:

extend FriendlyId
friendly_id :slug_candidates, :use => :scoped, :scope => :account

def slug_candidates
  :title_and_sequence
end

def title_and_sequence
  # This line switch all special chars to its unicode
  title_unicode = heb_to_unicode(title)

  slug = normalize_friendly_id(title_unicode)
      :
  # some login to add sequence in case of collision
  # and whatever you need from your slug
      :
end

def heb_to_unicode(str)
  heb_chars = 'אבגדהוזחטיכךלמםנןסעפףצץקרשת'
  heb_map = {}
  heb_chars.split("").each {|c| heb_map.merge!({c => URI::encode(c)})}
  # This regex replace all Hebrew letters to their unicode representation
  heb_re = Regexp.new(heb_map.keys.map { |x| Regexp.escape(x) }.join('|'))

  return str.gsub(heb_re, heb_map)
end
我还需要修改
规范化\u-friendly\u-id
,以避免它,从而摆脱
%

我只是简单地使用方法代码,并将
%
添加到正则表达式中:

def normalize_friendly_id(string)
  # replace accented chars with their ascii equivalents
  parameterized_string = I18n.transliterate(string)

  sep = '-'

  # Turn unwanted chars into the separator
  # We permit % in order to allow unicode in slug
  parameterized_string.gsub!(/[^a-zA-Z0-9\-_\%]+/, sep)
  unless sep.nil? || sep.empty?
    re_sep = Regexp.escape(sep)
    # No more than one of the separator in a row.
    parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
    # Remove leading/trailing separator.
    parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/, '')
  end
  parameterized_string.downcase
end
现在,如果我保存一个标题为
的模型,其slug将保存为
%D7%A9%D7%9C%D7%95%D7%9D

为了使用
friendly
方法查找实例,我需要执行以下操作:

id = URI::encode(params[:id]).downcase
Page.friendly.find(id)

你需要用一些东西把非拉丁字母音译成拉丁字母。你有没有看过
String#parameterize
(它是Rails添加的)请查看我的更新。如果我理解你的想法,我需要使用
音译
?@guyaloni将每个字符转换为unicode好吧,要完成你在更新中写的东西,你可以使用url编码,就像这样
URI::encode('ש1500;ום')=>%D7%A9%D7%9C%D7%95%D7%9D“
,不需要音译。音译可以让你用拉丁字母替换其他字母。没错。。。所以,简单地说,我怎样才能达到像WordPress一样的行为呢。。。wordpress会将带有重音的拉丁字符转换成非重音字符(
HellóVilág->/hello vilag
),在这种情况下不会发生这种情况。所以我卷起袖子,仔细研究这段代码。标题到段塞转换似乎发生在这里,我看了代码,以及。。。玩得高兴