How to find the i18n key from the value

Viewed 5035

I want to make a helper function which can take an English string as input and translate it to the desired language chosen by the user.

As all the locale files would be having key in common so I am looking for a way to find key using the string value. I am using default.yml files for storing translations.

After finding the key, I can use <%= t() %> for translating key to other languages.

1 Answers

I don't know how your yaml file looks like and it probably depends on what kind of I18n backend you use. If it's just key-value pairs for every language then something like this could work

I18n.backend.translations[:en].key "English string"

There is also the simple way. Just load the yaml file with the translations.

YAML.load_file("path_to_translations.yml").key "English string"
Related