多语言网站:在哪里存储语言列表?
我们的网站被翻译成近三十种语言。每篇文章都有一个字段说明它是为哪种语言编写的。
我想知道实现此目的的最佳 Rails 方法是什么。使用如下内容创建完整的模型语言:
Class Language
has_many :article
end
Class Article
belongs_to :language
end
或者直接在文章表的字段中硬编码,并使用常量列出所有可用的语言。 (主要是在下拉列表中显示):
LANG_LIST = {:en => 'english', :de => 'german', :fr => 'french' ...}
两者都有效,但是最好的 Rails 方式和最干净的维护方式是什么?
感谢您的意见!
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 => 'german', :fr => 'french' ...}
The two work, but what's the best rails way and the cleanest to maintain ?
Thanks for your opinion !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在文章模型上创建一个语言属性来存储信息,您的第二个选项可能就足够了。
我认为,语言模型太多了,没有任何实际好处。
如果您想进一步封装它,您可以创建一个模块,将模型扩展到处理语言的实例方法。如果你想让事情变得干净,这可能是首选的方式。
如果您希望坚持使用模型,请记住您可以使用委托助手使事情对您的语言模型透明。因此,您可以直接调用article.language_locale(一种非常微妙但重要的设计模式),而不是article.language.locale。
你可以做很多事情,但我可能会在这里用一个模块来保持简单。
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.