i18next / i18nextify

enables localization of any page with zero effort.
MIT License
61 stars 21 forks source link

Images example? #75

Closed Shyam-Chen closed 7 years ago

Shyam-Chen commented 8 years ago
<img src="./assets/images/foo.svg" alt="Foo">
jamuhl commented 8 years ago

you mean if alt attribute gets translated? not the case right now...but we could add that.

Shyam-Chen commented 8 years ago

or ...

<!-- without framework-->
<img src="./assets/images/{{ file }}" alt="{{ alt }}">
jamuhl commented 8 years ago

what you mean by without framework? could you explain a little more what you want to achieve?

Shyam-Chen commented 8 years ago

what you mean by without framework?

Just only vanilla JS. No Angular, React, and Vue.

could you explain a little more what you want to achieve?

I want to change different picture in different languages.

jamuhl commented 8 years ago

you got access to the underlaying i18next instance via:

i18nextify.i18next from there you could get the current local or locals (ordered by prio) http://i18next.com/docs/api/#language

you can take that to replace those values, pseudo code:

const lng = i18nextify.i18next.language;
const ele = document.getElementById('imgID');
ele.src = ele.src.replace('{file}', `filename_${lng}`);
ele.setAttribute('alt', i18nextify.i18next.t('An awesome cat image'));

Will think about a way to solve this inside the framework. Alt attribute is no big deal...

For the file i might make something like:

<img src="./assets/images/{{foo.svg}}" alt="Foo">

foo.svg will then be a key in the translation files and could be replaced to whatever you want for a specific language.

jamuhl commented 8 years ago

coming back i18nextify@1.1.0 includes fragment replacement: https://github.com/i18next/i18nextify#fragment-replacement-for-links-and-images

and alt tags are now added to the translatable attributes.