jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

Extract strings beforehand #87

Closed samin closed 8 years ago

samin commented 8 years ago

Is there a way to extract the translation strings beforehand, like with gulp or grunt? Seems foolish to translate only on page request, it would be better if it was done before build.

gjuchault commented 8 years ago

And how would you make plural sentences ? What would be the result of the gulp file ? You do realize that the translations files are plain JS object and there is nothing to « optimize » (if that's what you mean)

samin commented 8 years ago

I guess all possibilities need to be extracted at once, plurals included. Gettext does it, the extraction happens beforehand, the plain JS object should have all strings that need to be translated. Think of the following scenarios in a large production app: Scenario 1:

  1. Write i18n strings in app
  2. Before deployment, open every single page possible to get all strings to the translation file.
  3. Do it for multiple languages
  4. Go to translation files and translate.
  5. Deploy to production (or stage).

Scenario 2:

  1. Write i18n strings in app
  2. Before deployment, run gulp script to extract all translation strings.
  3. Go to translation files and translate.
  4. Deploy to production (or stage).

Which one do you prefer?

gjuchault commented 8 years ago

I guess you don't know how i18n-node-2 works so. If you would have read a bit the code, you would have seen this line (https://github.com/jeresig/i18n-node-2/blob/master/i18n.js#L59) and so files are loaded once at the beginning. And you mention gulp to extract translations and store them where ? Gulp can't pass data to the app and there would be no interest to that as the lib does the same thing

samin commented 8 years ago

I'm not discussing the loading of the translation files on runtime, I'm discussing extracting the translation strings from the app. i18n.__('Hello %s;', 'world'); -> "Hello %s": "Olá %s" (on locales/pt.js) This (i18n.__('Hello %s;', 'world');) is saved to file (locales/pt.js) when the string is requested. So you have to request all i18n.__(...) to get all strings in locales/pt.js. This is the line you want: https://github.com/jeresig/i18n-node-2/blob/master/i18n.js#L405

This seems silly. I'm not sure you know how gettext works, but you can extract all __(...) beforehand, and translate them all at once on the .po files. It even clears out deprecated strings from the translation files.

gjuchault commented 8 years ago

i18n-node-2 does not work at all like gettext. Not at all. There is no string extraction.

Using gettext you have to analyze the sourcecode first to extract _ function calls. i18n-node-2 does not do anything like it as it just loads a json file and match the key to the value directly.

If you want something closer to gettext, you might check other libraries like i18nnext-conv.

ghost commented 5 years ago

He didn't ask if it was similar to gettext, he didn't ask if it was only possible to use Gulp.

He simply asked if it's somehow possible to extract all the strings automatically, without manually visiting every single page.

Which would be extremely, insanely useful. This library is pretty much a pain for any larger project.

gjuchault commented 5 years ago

@fjeddy Yet, I don't see how i18n-node-2 is or should be related to Gulp. It was never meant for this