سؤال

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

هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

require 'iconv'
p Iconv.iconv('ascii//translit//ignore', 'utf-8',  'Gévry') #=> ["Gevry"]
# not         'ascii//ignore//translit'

For Cyrillic the translit gem might work.

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!"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top