Guest User

Untitled

a guest
Jun 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. require 'iconv'
  2. require 'unicode'
  3.  
  4. class StringUtil
  5.  
  6. def self.to_ascii(s)
  7. # split in muti-byte aware fashion and translate characters over 127
  8. # and dropping characters not in the translation hash
  9. s.chars.split('').collect { |c| (c[0] <= 127) ? c : translation_hash[c[0]] }.join
  10. end
  11.  
  12. protected
  13.  
  14. def self.translation_hash
  15. accented_chars = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüý".chars.split('')
  16. unaccented_chars = "AAAAAACEEEEIIIIDNOOOOOxOUUUUYaaaaaaceeeeiiiinoooooouuuuy".split('')
  17.  
  18. translation_hash = {}
  19. accented_chars.each_with_index { |char, idx| translation_hash[char[0]] = unaccented_chars[idx] }
  20. translation_hash["Æ".chars[0]] = 'AE'
  21. translation_hash["æ".chars[0]] = 'ae'
  22. translation_hash
  23. end
  24.  
  25. end
Add Comment
Please, Sign In to add comment