/ Published in: Rails
URL: http://67ideas.com/blog/easily-translate-rails-form-inputs-saving
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
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
You need to login to post a comment.
