/ Published in: Rails
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
URL: http://67ideas.com/blog/easily-translate-rails-form-inputs-saving