Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom error message processor without monkey patch
    primarykey
    data
    text
    <p>I've implemented a custom error message processor for Korean language. In Korean, postpositions take different forms depending on the sound of the preceding noun or pronoun.</p> <p>For example, when marking a subject, <strong>ka (가)</strong> is used following a vowel and <strong>i (이)</strong> is used following a consonant.</p> <p>Examples (hyphens are to denote morpheme boundaries):</p> <pre><code>Romanization: sakwa-ka ppalkah-ta. Gloss: Apple-SUBJECT red-PRESENT. Translation: Apple is red. Romanization: phainayphul-i tal-ta. Gloss: Pineapple-SUBJECT sweet-PRESENT. Translation: Pineapple is sweet. </code></pre> <p>Therefore, the standard error message system implemented in ActiveModel::Errors is not adequate for Korean. You should either include the attribute in the message making a lot of duplicates ("A is blank", "B is blank", "C is blank", ...), or avoid postpositions after the attribute which is often difficult or makes awkward sentences.</p> <p>I monkey patched ActiveModel::Errors and altered <code>generate_message</code> to solve this problem. Following is the code (<a href="https://gist.github.com/3988479" rel="nofollow">Gist</a>) which is currently in <code>config/initializers</code> in my Rails app.</p> <pre><code># encoding: UTF-8 # Original article: http://dailyupgrade.me/post/6806676778/rubyonrails-full-messages-for-korean # Modified to support more postpositions and client_side_validations gem. # # Add Korean translations like this: # ko: # errors: # format: "%{message}" # messages: # blank: "%{attribute}((이)) 입력되지 않았습니다" # taken: "이미 사용 중인 %{attribute}입니다" # invalid: "잘못된 %{attribute}입니다" # too_short: "%{attribute}((이)) 너무 짧습니다" # class Korean POSTPOSITIONS = {"은" =&gt; "는", "이" =&gt; "가", "을" =&gt; "를", "과" =&gt; "와", "으로" =&gt; "로"} def self.select_postposition(last_letter, postposition) return postposition unless last_letter &gt;= "가" &amp;&amp; last_letter &lt;= "힣" final = last_letter.mb_chars.last.decompose[2] if final.nil? # 받침 없음 POSTPOSITIONS[postposition] || postposition elsif final == "ㄹ" &amp;&amp; (postposition == "으로" || postposition == "로") # 'ㄹ 받침 + (으)로'를 위한 특별 규칙 "로" else # 받침 있음 POSTPOSITIONS.invert[postposition] || postposition end end end module ActiveModel class Errors old_generate_message = instance_method("generate_message") define_method("generate_message") do |attribute, type = :invalid, options = {}| msg = old_generate_message.bind(self).(attribute, type, options) return msg unless I18n.locale == :ko msg.gsub(/(?&lt;=(.{1}))\(\((은|는|이|가|을|를|과|와|으로|로)\)\)/) do Korean.select_postposition $1, $2 end end end end </code></pre> <p>My first question is whether it is possible to achieve the same goal without monkey patching. I'm new to Rails (and Ruby too) so couldn't come up with a better solution.</p> <p>The second question is about extracting this code from my Rails app and making it into a separate gem. I'm cutting my teeth on developing gems and recently made <a href="https://github.com/clee704/virtual_attributes" rel="nofollow">my first gem</a>. In what place should I put this code in a gem? <code>config/initializers</code> doesn't seem right.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload