i18n-node icon indicating copy to clipboard operation
i18n-node copied to clipboard

File support

Open nicosommi opened this issue 12 years ago • 0 comments

I have a use case in which I need to translate long texts. The json file gets pretty uggly with long texts, so I think on supporting files.

Basically in your locale entry you put a file path like this:

{
  'content.file': '/locales/parts/mycontent.en.part'
}

And in your code you can use this with arguments included using the current sprintf like this:

i18n.__f('content.file','myparam1');

For this I added this function right after __p (which is similar to __):

i18n.__f = function i18nTranslateUsingFile(phrase) {
  // get translated filename with locale from scope (deprecated) or object
  var msg = translate(getLocaleFromObject(this), phrase);

  //reads the file
  var fileContent = fs.readFileSync("./" + msg, 'utf8');

  // if we have extra arguments with strings to get replaced,
  // an additional substition injects those strings afterwards
  if (arguments.length > 1) {
    fileContent = vsprintf(fileContent, Array.prototype.slice.call(arguments, 1));
  }

  //return file content
  return fileContent;
};

Hope it helps. You can add it into the module if you want to.

Regards.

nicosommi avatar Apr 01 '13 15:04 nicosommi