ayac1q
Last Updated: February 25, 2016
·
2.307K
· kazlan
Satisfaccion

Using i18next with Meteor

Just spent some time trying to figure out how to work i18n into a Meteor project and thought it was worth a share.

there's already a i18next module in atmosphere, so just

mrt add i18next-meteor

and the lib will be included.

check http://i18next.com/ to see how to write your json files and store them in
/public/locales/LANG/translation.json
ex. /public/locales/en/translation.json
/public/locales/es/translation.json

make sure you add the 'data-i18n="ns.key"' tag to the elements to be i18nized. Next we'll initialize the stuff (i'm using coffescript notation):

Meteor.startup -> 
  i18n.init {lng: 'es', load: 'unespecific', preload: 'en'},(t)->
    $('[data-i18n]').i18n()

then add the events to swap your locale. In my case I used two links (#i18n-es and #i18n-en):

Template.mytemplate.events
  'click #i18n-en': (e)->
    e.preventDefault()
    i18n.setLng 'en', ->
      $("[data-i18n]").i18n()

  'click #i18n-es': (e) ->
    e.preventDefault()
    i18n.setLng 'es', ->
      $("[data-i18n]").i18n()

all set and done. Happy coding!

Say Thanks
Respond

2 Responses
Add your response

11362
D2fdf78b30cb140aec0f6fb3f945df22

Can you show us a sample template that is i18n enabled?

over 1 year ago ·
16782
5b6307f7fcdd7a1e67193ca18fb303a9

Check the tap:i18n package, the comprehensive internationalization solution for Meteor, which uses i18next as its internationalization engine.

https://github.com/TAPevents/tap-i18n

over 1 year ago ·