开发者

Rails 3 I18n approaches

I am new to Rails and I need to implement internationalization in an application. Till now I looked through r18n-rails, globalize3 and gettext_i18n_rails.

I have to internationalize the templates text along with some DB data (some categories as an example).

I like the way gettext_i18n_rails works with templates text but it doesn't pr开发者_如何学Pythonovide any solution for DB data (or does it?). From other side r18n-rails and globalize3 seem to do the job for DB data but not for templates. At first I thought to combine them (globalize or r18n + gettext_i18n_rails) but this could nod be a good idea, especially if this is gonna be a big project.

So my question is:

What is the best I18n approach for Rails 3 considering templates text + DB text + project size?


The best approach in Rails 3 is using the built-in mechanism, the I18n gem. I am not sure if I understand you problem with the DB data. If you define the categories in the database, just add an i18n key to them and in the model return the translated title.

e.g.

class Category
  # with a column that holds the i18n_lookup_key (maybe you think of a better name)
  ...

  def translated_title
    I18n.translate(i18n_lookup_key)
  end

end

you then can use the translated_title method for displaying the category's title.

If you have more questions about I18n in Rails, we just did a series on our blog:

Internationalization (I18n) API in Ruby on Rails 3 (Part I)

  • Definition of Internationalization & Localization
  • How i18n mechanism works in Rails
  • Setup/Configuration about the i18n mechanism
  • Using the I18n with interpolation
  • Using the I18n with pluralization

Internationalization (I18n) API in Ruby on Rails 3 (Part II)

  • Localization of Times/Dates
  • Using views for translation
  • Translating ActiveRecord errors
  • Organization of resource files
  • Setting the Locale from the Domain Name
  • Setting the Locale from the URL Params

Part 3 is coming out next week, with a PDF including all the topics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜