Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top