Return to Snippet

Revision: 23427
at February 7, 2010 11:04 by 67ideas


Initial Code
class Word < ActiveRecord::Base
  before_save :replace_pinyin
  
  @@replacements = {
    'a1' => 'ā',
    'e1' => 'Ä“',
    'i1' => 'Ä«',
    'o1' => 'ō',
    'u1' => 'Å«',
    'a2' => 'á',
    'e2' => 'é',
    'i2' => 'í',
    'o2' => 'ó',
    'u2' => 'ú',
    'a3' => 'ÇŽ',
    'e3' => 'Ä›',
    'i3' => 'ǐ',
    'o3' => 'Ç’',
    'u3' => 'Ç”',
    'a4' => 'à',
    'e4' => 'è',
    'i4' => 'ì',
    'o4' => 'ò',
    'u4' => 'ù',
  }

  def replace_pinyin
    @@replacements.each do |key, value|
      self.pinyin.gsub!(key, value)
    end
  end
end

Initial URL
http://67ideas.com/blog/easily-translate-rails-form-inputs-saving

Initial Description
This lets you type Mandarin tones like a1, o2, e3, and so on instead of having to type the real pinyin. The tones are saved with the proper characters in the database.

Initial Title
Convert Between Simple Tone Input and Pinyin in Rails model

Initial Tags
rails

Initial Language
Rails