Reputation: 2508
I am currently working in a project where, for some reason, there are two files for each language that have almost the same content:
config/locales/en/my_translations.yml
config/locales/en/my_special_translations.yml
config/locales/pt/my_translations.yml
config/locales/pt/my_special_translations.yml
The files with the same name have the same content, translated to different languages. The problem here is that my_translations
and my_special_translations
have a really lot of duplicated code. Below I show an example with fake data to show where is the duplication and the differences:
config/locales/en/my_translations.yml
es:
animals:
happy_animals:
dog: Dog
cat: Cat
horse: Horse
sad_animals:
cow: Cow
elephant: Elephant
config/locales/en/my_special_translations.yml
es:
special_animals: # This line is different
happy_animals:
dog: Dog
cat: Cat
horse: Horse
sad_animals:
cow: Farm cow # This line is different
elephant: Elephant
This is just an example, but I have files with hundreds of lines where just a few or them are different. Is there any way to reduce or avoid this duplication between files?
EDIT
I am using this code the following way:
animals_scope = special? ? 'special_animals' : 'animals'
animals_hash = I18n.translate(animals_scope, locale: current_locale)
animals_hash.keys.each do|animal_key|
# Stuff
end
That is, for each element in the .yml file I am performing some actions
Upvotes: 2
Views: 733
Reputation: 107142
You could use YAML anchors and aliases and only override the values that are different:
es:
animals: &ANIMALS
happy_animals:
dog: Dog
cat: Cat
horse: Horse
sad_animals:
cow: Cow
elephant: Elephant
special_animals:
<<: *ANIMALS
sad_animals:
cow: Farm cow
Upvotes: 3