1

I'm using github.com/fnando/i18n-js gem for JavaScript translation. I noticed when I view source the page and check the translations.js it loads the complete translations file, even if that page is using only one translation word.

Any idea how JavaScript can load the required translations for a the current page rather loading the complete file.

Boris Callens
  • 90,659
  • 85
  • 207
  • 305
kashif
  • 1,097
  • 4
  • 17
  • 32

2 Answers2

4

The best you can do is have i18n-js generate several translation files, each containing only a subset of the translations that you need. E.g. see this piece from the documentation:

translations:
- file: 'public/javascripts/path-to-your-messages-file.js'
  only: '*.date.formats'
- file: 'public/javascripts/path-to-your-second-file.js'
  only: ['*.activerecord', '*.admin.*.title']

By including the appropriate file in your HMTL page, you just load the translations that you need in that HTML page. So the challenge then is to organize your webpages and translations well.

Pascal Lindelauf
  • 4,782
  • 4
  • 40
  • 55
  • i added the above mentioned Configuration in config/i18n-js.yml file now the translation should read the files present in public/javascripts/en.js file. but it is not pulling the translations now. I'm using rails 3.2.12. am i missing something ? – kashif Jun 13 '13 at 08:47
  • I'm keeping complete app translation in one different file for each language. say in config/en.yml for English . Java script loads the complete app translation I just want a new separate file for each language which only contains translations used by JavaScript. so-that it shouldn't load all the translations for java script. I tried and put the file in 'public/javascripts/en.js but it didnt work out. any idea how i can get this implemented. using rails 3.2.12 and i18n-js 2.1.2 in this way: I'll have two files for each lang: config/en.yml for complete app javascipt/en.js for javascipt – kashif Jun 13 '13 at 09:52
  • Thanks Guys - I find the solution. I need to add app/asserts/javascipt/i18n/translation.js this one file translation.js will have all the js related translations. This file will have different section for all the languages like I18n.translations = { en: { valid_email: "Valid email address is required", company_name_required: "Company name is required" } fn: { valid_email: "some french", company_name_required: "sss french" } } – kashif Jun 13 '13 at 10:19
0

There's no way to achieve that!

Reason why translations.js include all keys is that Rails can't know which phrases you'd like to use in your js beforehand.

A hack I did myself was that I'm not generating translations.js by hand/at asset compilation, but instead I created it manually inclucing only things I need. This approach have of course pitfall - if you start using other translations you will need to add them by hand again.

Another possibility may be to follow Configuration section and:

  • put all transations used in javascript under one key
  • config i18n-js so it exports only this single key
Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63