Rails to use i18n lazy look up with file structure
-
20-12-2019 - |
Question
referring to
[1] This Rails Internationalization: i18n look up with nested locales directories
[2] this http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
In which the rails guide only show how you could structure it but it cuts off after that. It did not mention anything about how to look up with the given file structure
So I was wondering if there is anyways to use lazy look with the file structure
consider the following directories
config/locales app/views
|locales |views
|-defaults |-fruits
|--en.yml |--apple
|--views |--banana
|---fruits |-meats
|----en.yml |--chicken
|---meat |--beef
|----en.yml
config/application.rb
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/**/*.{rb,yml}"]
.yml
locales/views/fruits/en.yml| locales/views/meats/en.yml
|
en: | en:
produce: "Produce" | meat_section: "Meat Section" <--- shared key within folder
|
apple: | chicken:
red: "Red" | white_meat: "White Meat"
|
banana: | beef:
yellow: "Yellow" | red_meat: "Red Meat"
locales/defaults/en.yml
en:
repeat_key:
market: "Market" <------- shared key across folders
Is there any way that i can do this ? or anything close to that ??
In views/fruits/apple.html.haml
= t('.red') <------- unique key in apple
= t('produce') <------- shared key within fruits folder
= t('repeat_key.maket') <------- repeat key across fruits and meat folders
I know that fact that the following code will work but since I am in the apple directory, there must be a way to use the lazy look up for the unique key.
In views/fruits/apple.html.haml
= t('apple.red') <------- unique key in apple
= t('produce') <------- shared key within fruits folder
= t('repeat_key.maket') <------- repeat key across fruits and meat folders
I think that if this is possible. It can minimize duplicate keys and if we need extra alias to be added, we can add it when it's necessary since I believe most translation team charge per words
Solution
I found out that rails would actually pulls all the .yml file for a selected nested level and combine them to one but that final .yml which happens under the hood and something that we can't see (at least I couldn't find a way to see it); In additional, the .yml name doesn't matter as long as the .yml inside is structured and Lazy look up would work.
At the end I did organize the locales like the following way, I think it's a better way to organize it.
|config
|-locales
|--en
|---en.yml
|---fruits.en.yml
|---meat.en.yml
.yml
locales/en/fruits.en.yml| locales/en/meats.en.yml
|
en: | en:
fruits: | meats:
produce: "Produce" | meat_section: "Meat Section" <-shared key within folder
|
apple: | chicken:
red: "Red" | white_meat: "White Meat"
|
banana: | beef:
yellow: "Yellow" | red_meat: "Red Meat"
locales/en/en.yml
en:
repeat_key:
market: "Market" <------- shared key across folders
In views/fruits/apple.html.haml
= t('.red') <------- unique key in apple
= t('fruits.produce') <------- shared key within fruits folder
= t('repeat_key.maket') <------- repeat key across fruits and meat folders
By this convention
- The key with the "." aka Lazy look up this which indicate which .yml it's in
- The key with the file name in front show that shared key as well as it's location
- The key with a lot of repeat across folders can go under repeat_key and those are presence in the en/en.yml
OTHER TIPS
@AirWick219.
If you'd like to use lazy lookup, I think you need to prepare the file config/locales/views/fruits/apple/en.yml
.
If you want shared key within folder, how about to add something like this, though it's a bit redundant.
config/locales/views/shared/en.yml
,
en:
shared:
fruits:
produce: "Produce"
= t('shared.fruits.produce')
Or give up to use lazy lookup :(