Skip to content

Instantly share code, notes, and snippets.

@mindhalt
Forked from devsergiy/gist:2023743
Created December 21, 2012 14:49
Show Gist options
  • Select an option

  • Save mindhalt/4353242 to your computer and use it in GitHub Desktop.

Select an option

Save mindhalt/4353242 to your computer and use it in GitHub Desktop.

Revisions

  1. spetrunin revised this gist Mar 12, 2012. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #place this in config/locales/translit.rb

    # encoding: utf-8

    # I18n transliteration delegates to Russian::Transliteration (we're unable
    @@ -16,11 +18,11 @@
    }
    }

    place it in config/locales/translit.rb

    and this into libs with file name transliteration.rb
    don't forget to enable autoloading files from libs by adding next line into application.rb
    config.autoload_paths += %W(#{config.root}/lib)

    #and this into libs with file name transliteration.rb
    #don't forget to enable autoloading files from libs by adding next line into application.rb
    #config.autoload_paths += %W(#{config.root}/lib)


    # encoding: utf-8
    @@ -92,4 +94,4 @@ def transliterate(str)

    result
    end
    end
    end
  2. spetrunin renamed this gist Mar 12, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. spetrunin created this gist Mar 12, 2012.
    95 changes: 95 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    # encoding: utf-8

    # I18n transliteration delegates to Russian::Transliteration (we're unable
    # to use common I18n transliteration tables with Russian)
    #
    # Правило транслитерации для I18n использует Russian::Transliteration
    # (использовать обычный механизм и таблицу транслитерации I18n с
    # русским языком не получится)
    {
    :ru => {
    :i18n => {
    :transliterate => {
    :rule => lambda { |str| Transliteration.transliterate(str) }
    }
    }
    }
    }

    place it in config/locales/translit.rb

    and this into libs with file name transliteration.rb
    don't forget to enable autoloading files from libs by adding next line into application.rb
    config.autoload_paths += %W(#{config.root}/lib)


    # encoding: utf-8

    # Russian transliteration
    #
    # Транслитерация для букв русского алфавита
    module Transliteration
    extend self

    # Transliteration heavily based on rutils gem by Julian "julik" Tarkhanov and Co.
    # <http://rutils.rubyforge.org/>
    # Cleaned up and optimized.

    LOWER_SINGLE = {
    "і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
    "ї"=>"yi","а"=>"a","б"=>"b",
    "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
    "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
    "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
    "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
    "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
    "ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
    }
    LOWER_MULTI = {
    "ье"=>"ie",
    "ьё"=>"ie",
    }

    UPPER_SINGLE = {
    "Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
    "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
    "Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
    "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
    "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
    "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
    "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"Y","Ь"=>"",
    "Э"=>"E","Ю"=>"YU","Я"=>"YA",
    }
    UPPER_MULTI = {
    "ЬЕ"=>"IE",
    "ЬЁ"=>"IE",
    }

    LOWER = (LOWER_SINGLE.merge(LOWER_MULTI)).freeze
    UPPER = (UPPER_SINGLE.merge(UPPER_MULTI)).freeze
    MULTI_KEYS = (LOWER_MULTI.merge(UPPER_MULTI)).keys.sort_by {|s| s.length}.reverse.freeze

    # Transliterate a string with russian characters
    #
    # Возвращает строку, в которой все буквы русского алфавита заменены на похожую по звучанию латиницу
    def transliterate(str)
    chars = str.scan(%r{#{MULTI_KEYS.join '|'}|\w|.})

    result = ""

    chars.each_with_index do |char, index|
    if UPPER.has_key?(char) && LOWER.has_key?(chars[index+1])
    # combined case
    result << UPPER[char].downcase.capitalize
    elsif UPPER.has_key?(char)
    result << UPPER[char]
    elsif LOWER.has_key?(char)
    result << LOWER[char]
    else
    result << char
    end
    end

    result
    end
    end