Transliteration with Iconv in Ruby

2019-02-17 04:28发布

When I'm trying to transliterate a Cyrillic utf-8 string with

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

(see questions/1726404/transliteration-in-ruby)

I'm getting everything but those symbols that have to be transliterated.

For example: 'r-строка' → 'r-' and 'Gévry' → 'Gvry'.

What's wrong?

Ruby 1.8.7 / Rails 2.3.5 / WSeven

3条回答
叼着烟拽天下
2楼-- · 2019-02-17 04:44

Another way is to create custom translit by tr and gsub methods of String without using iconv.

# encoding: UTF-8

def russian_translit(text)
    translited = text.tr('абвгдеёзийклмнопрстуфхэыь', 'abvgdeezijklmnoprstufhey\'')
    translited = translited.tr('АБВГДЕЁЗИЙКЛМНОПРСТУФХЭ', 'ABVGDEEZIJKLMNOPRSTUFHEY\'')

    translited = translited.gsub(/[жцчшщъюяЖЦЧШЩЪЮЯ]/,
        'ж' => 'zh', 'ц' => 'ts', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ю' => 'ju', 'я' => 'ja',
        'Ж' => 'ZH', 'Ц' => 'TS', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '', 'Ю' => 'JU', 'Я' => 'JA')
    return translited
end

p russian_translit("В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!")
#=> "V chaschah juga zhil by tsitrus? Da, no fal'shivyj ekzempljar!"
查看更多
forever°为你锁心
3楼-- · 2019-02-17 04:51
require 'iconv'
p Iconv.iconv('ascii//translit//ignore', 'utf-8',  'Gévry') #=> ["Gevry"]
# not         'ascii//ignore//translit'

For Cyrillic the translit gem might work.

查看更多
做自己的国王
4楼-- · 2019-02-17 04:54

It seems the solution is too tricky for me. Problem solved using stringex gem.

查看更多
登录 后发表回答