Question

Consider my translation key

en:
 test: "This is a test with %{another_variable} to explain translation"
 another_variable: "some text"

I wanted to bold the contents of the test key from "test with %{another_variable} to". I can do this by including a strong element or a b tag before the text "test" in the test key and changing the test key to test_html key.

But my question is this a correct way to include html elements in the translation file. I mean this files is only for the texts that are to be translated. If someone is translating my application into Spanish he/she who doesn't have any idea about HTML will find it difficult the html tags in the appropriate place.

What is the best way to do so that the translation file has only the text and we can format the translation texts in a better way.

Thanks

Was it helpful?

Solution

There are many ways you could achieve this. First is yes, you could add the html tags within the translation file.

Another solution would be to use a helper for this. Helpers are meant to help view creation. Within your helper file, you could define the following method

def bolded_translated_test
  t('test', another_variable: "<b>#{t('another_variable')}</b>"
end

Then in your view, you can use it

<%= bolded_translated_test %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top