在RoR中对UTF-8字符串进行排序 [英] Sorting UTF-8 strings in RoR
问题描述
我正在尝试找出在Ruby on Rails中对UTF-8字符串进行排序的正确"方法.
I am trying to figure out a 'proper' way of sorting UTF-8 strings in Ruby on Rails.
在我的应用程序中,我有一个选择框,其中填充了国家/地区.当我的应用程序已本地化时,每个现有的语言环境都有一个country.yml文件,该文件将一个国家/地区的ID与该国家/地区的本地化名称相关联.我无法在yml文件中手动对字符串进行排序,因为我需要ID在所有语言环境中都保持一致.
In my application, I have a select box that is populated with countries. As my application is localized, each existing locale has a countries.yml file that relates a country's id to the localized name for that country. I can't sort the strings manually in the yml file because I need the ID to be consistent across all locales.
我所做的是创建一个ascii_name
方法,该方法使用 unidecode
gem将带重音符号和非拉丁字母的字符转换为它们的ascii等效字符(例如,Afeganistão"将变为"Afeganistao"),然后对其进行排序:
What I have done is create a ascii_name
method which uses the unidecode
gem to convert accented and non-latin characters to their ascii equivalent (for instance, "Afeganistão" would become "Afeganistao"), and then sort on that:
require 'unidecode'
class Country
def ascii_name
Unidecoder.decode(name).gsub("[?]", "").gsub(/`/, "'").strip
end
end
Country.all.sort_by(:&ascii_name)
但是,这存在明显的问题:
However, there are obvious issues with this:
- 由于可能没有直接相似的拉丁字符,因此无法正确地对非拉丁语言环境进行排序.
- 在字母和字母的所有重音形式之间没有区别(例如,A和Ä可以互换)
有人知道我可以对字符串进行排序的更好方法吗?
Does anyone know of a better way that I could sort my strings?
推荐答案
http://github.com/grosser/sort_alphabetical
该宝石应有帮助.它将sort_alphabetical
和sort_alphabetical_by
方法添加到Enumberable.
This gem should help. It adds sort_alphabetical
and sort_alphabetical_by
methods to Enumberable.
这篇关于在RoR中对UTF-8字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!