I have a lot of static preferences for one entity. Do I need to store them in database table? Or I could use YAML configuration file, with approximate structure:
name: "Lights"
descriptions: ""
items:
title: "Fog lighs"
description: "Should be used in foggy environment only!"
title: "Xenon"
description: "6500K"
name "Safety"
description: "Safety systems"
items:
title: "Airbag"
description: ""
title: "ABS"
description: "bla bla bla"
with ruby/object model markup. Can this be done with a nesting structure like in my example above?
Can somebody tell, if it is a good practice to do so? I don’t want to use database, because these settings would be pretty much static, with low probability of need to change anything.
If it is possible, I would also like to know how to implement translation (i18n) for the elements of the structure (names and titles). I don’t know where to put it in this particular situation.
Or I should use an entirely different approach?
You can of course use YAML in your rails app the same way you would from any ruby app:
For translation just use the existing Rails I18n framework.
For example, given the classes:
And the following YAML data file:
You can add the translation in config/locale/things.nb.yml:
Similar for other languages, of course. Now let’s implement a method to describe a thing:
Assuming the current language is Norwegian, you can now do:
and get:
Notice you may either drop the description tags from the original YAML file, or have them as default fallbacks if there is no translation available.
Hope this helps.