开发者

Rails I18n: Days of week

I have a Practice model that is storing start_time, end_time and day.

That information (like the rest of the site) will need to be displayed in 3 different languages.

Start_time and end_time are both stored as Datetime types in the DB.

Day is not yet implemented, but will be shown as a select box. I have see people suggest an array of 开发者_开发技巧constants and storing it as an integer in the DB. While that seems reasonable, I am having trouble imagining that working well with different languages (using either I18n or Globalize2).

What is the cleanest way to implement this so it works well in different languages?


I can't see a good reason to store the days of the week in the database; I'd be surprised if such names will change and we are talking about 7 * 3 strings, that is a small amount of data to handle for your application.

i18n is the way to go. You can browser this repository to find day_names already traslated in different languages.


If you can't store or calculate the day of the week from the start and end DateTime objects, then I would recommend using an ENUM in the database - this causes the data to be stored as an integer - which takes up less space and is easier to index, and the conversion to the string types is done automatically for you so that you can query and insert strings, but the actual database values are integers.

Definitely do not store these values as strings though, it's harder to deal with them and they take up more space.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜