We have a website translated in almost thirty languages. Each article will have a field saying which language it've been written for.
I wonder what's the best rails way to implement this. Create a full model Language with something like this :
Class Language
has_many :article
end
Class Article
belongs_to :language
end
Or hardcode this directly in a field of the articles table with a constant to list all the available langs. (Mainly to show it in a drop-down list) :
LANG_LIST = {:en => 'english', :de => 'germ开发者_Go百科an', :fr => 'french' ...}
The two work, but what's the best rails way and the cleanest to maintain ?
Thanks for your opinion !
You can just create a language attribute on the article model to store the information, your second option may be just enough.
A language model would just be too much for no real gain, i think.
If you want to encapsulate it further, you can create a module that extends your model to instance methods that handle languages. And that is probably the preferred way, if you want to make things clean.
Should you wish to stick with a model, remember that you can use the delegate helper to make things transparent to your language model. So, instead of article.language.locale, you can directly call article.language_locale (a very subtle but important design pattern).
Many things that you can do, but i will probably keep it simple with a module here.
精彩评论