How do you customize transliterations in a Rails 3 app?

爱⌒轻易说出口 提交于 2020-01-02 09:58:51

问题


Ultimately, I would like to use Inflector.parameterize to create slugs for article heading that have a bunch of unicode chars in them (e.g. "ḤellẒ no" => "hellz-no"). According to http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate it says to put them in the locales/en.yml file.

# Store the transliterations in locales/en.yml
i18n:
  transliterate:
    rule:
      Ḥ: "h"
      Ẓ: "z"

I tried that but the following does not work:

"ḤellẒ no".parameterize
# => "ell-no"

However, when I change it in Ruby like the second paragraph suggests, it works.

I18n.backend.store_translations(:en, :i18n => {
   :transliterate => {
     :rule => {
       "Ḥ" => "H",
       "Ẓ" => "Z"
     }
   }
})

"ḤellẒ no".parameterize
# => "hellz-no"

I guess I would like to know why putting the custom transliterations in locales/en.yml doesn't work.

And even if someone give the answer for that, being a Rails noob, I would also like to know where one usually puts code like the second block to manually set the I18n.backend.store_translations?


回答1:


Ehh, I've got a part of the answer. Unlike what the doc at http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate says, the yml files should still specify the language - i.e. de:

# Store the transliterations in locales/de.yml
de:
  i18n:
    transliterate:
      rule:
        ü: "ue"
        ö: "oe"

Please still answer the second part of the question, where should code like I18n.backend.store_translations(:en,... live in a Rails 3 app?




回答2:


[...] where should code like I18n.backend.store_translations(:en,... live in a Rails 3 app?

I know. I might be a little late on this but I would put it into an initializer file: config/initializers/i18n.rb



来源:https://stackoverflow.com/questions/4976070/how-do-you-customize-transliterations-in-a-rails-3-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!