Ruby On Rails: pluralize for other languages

孤人 提交于 2019-12-04 19:21:57

问题


I am building apps for a non-english audience. Right now, I use english nouns to name my models, yet I prefer to use native dutch ones. As the convention uses the plural of the class name for tables, I assume it is the pluralize method inside Rails (where it resides, I wouldn't know). How can I change the pluralize method and where is it located? Would this break Rails?

I am using Rails 2.3.5 and Ruby 1.8.7

Example: The Book class becomes books now. My Boek class becomes boeks, but it is grammatically correct to use boeken


回答1:


Add your rules to an inflections.rb file in config/initializers. See the API documentation:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'boek', 'boeken'
end



回答2:


Perhaps won't help you because you want Dutch language, but for Spanish, French, Kazakh, Turkish or Norwegian, there is this:

https://github.com/davidcelis/inflections




回答3:


This is not answering the question specifically, but if a language has too much irregularities one can disable the inflector according to the discussion.

ActiveRecord::Base.pluralize_table_names = false




回答4:


In addition, as far as views are concerned my preferred way of dealing with pluralizing foreign strings is i18n pluralization. Take a look at a straightforward example below.

# config/locales/en.yml

en:
  message:
    one: You have 1 message #Your foreign string
    other: You have %{count} messages #Your foreign string

Then in view you can do

# app/views/messages/index.html.erb

<%= t("message", count: current_user.messages.count) %>

Check official documentation.
Hope that helps!



来源:https://stackoverflow.com/questions/2998551/ruby-on-rails-pluralize-for-other-languages

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