mashpie / i18n-node

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

Get string from locale file without substitution #494

Closed tehmas closed 2 years ago

tehmas commented 2 years ago

Consider the following key/value example pair in en.json file:

"required": "The {{key}} is required"

Using i18n.__("required") returns:

The is required

I have a usecase in which I do not want the {{key}} to be substituted by i18n.__() and I need to get the string without substitution i.e.: The {{key}} is required

mashpie commented 2 years ago

there is config for such:

  // use mustache with customTags (https://www.npmjs.com/package/mustache#custom-delimiters) or disable mustache entirely
  mustacheConfig: {
    tags: ['{{', '}}'],
    disable: false
  }
tehmas commented 2 years ago

@mashpie Thanks for the answer. Ah, I guess it is a very naive follow-up question but are you suggesting to do it in the global configuration as suggested in the README and here?

I only need it to ignore the custom tags while calling i18n.__() in a particular situation.

mashpie commented 2 years ago

Cite from https://www.npmjs.com/package/mustache

If you want {{name}} not to be interpreted as a mustache tag, but rather to appear exactly as {{name}} in the output, you must change and then restore the default delimiter. See the Custom Delimiters section for more information.

so, for now you could choose custom delimiters for backend and use defaults for frontend.

Or... create two i18n instances similar to how it's done in this test https://github.com/mashpie/i18n-node/blob/master/test/i18n.configureMustache.js

tehmas commented 2 years ago

Thanks for the response.