Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18n Attaching variable to translation string

I am currently using i18n to translate strings and I'm looking for a way to attach a variable onto the path.

This variable could be about 8 different values and each need a different translation. My locale files already include the translations but I don't know how to add my variable onto the translation path. I don't think variable interpolation helps me here.

content.state //variable
t ('.state') //locale path

I'm looking for a way to add the content.state variable to the end of the locale path so that it translates depending on what content.state is. Currently my only solution is a large if/else covering all the states.

Solved: I was trying to find a way to add a variable to the locale path inside handlebars and ended up using the concat helper.

{{ t (concat "state." content.state)}}
like image 630
L. Green Avatar asked Aug 04 '17 15:08

L. Green


People also ask

What is an I18N string?

Internationalization (or i18n) is the process of marking strings for translation, so that the strings can be extracted from the source code and given to translators. Localization (l10n) is the process of translating the marked strings into different languages.

What is I18N translation?

Internationalization (sometimes shortened to "I18N , meaning "I - eighteen letters -N") is the process of planning and implementing products and services so that they can easily be adapted to specific local languages and cultures, a process called localization .


1 Answers

You can do that like this (example):

I18n.t('helpers.links.add', variable: "Your Variable Value")

then, on your translation file (yml):

helpers:
  links:
    add: "Add %{variable}"

That way, in my example, the output would be: Add Your Variable Value.

Hope this helps!

like image 58
Ronan Lopes Avatar answered Oct 16 '22 05:10

Ronan Lopes