Fetches and formats translated strings.

Usage no npm install needed!

<script type="module">
  import localizedString from 'https://cdn.skypack.dev/localized-string';



Fetches and formats translated strings.

If you're using .properties files, grunt-concat-i18n is recommended.


$ npm i localized-string --save


var localizedString = require('localized-string')(translations);

var myDefaultLanguageString = localizedString.getText('', 'some.key', 'parameter');
var myGermanString = localizedString.getText('de_DE', 'some.key', ['more', 'params']);

translations is expected to be a hash of translations containing the translated strings, for example:

var translations = {
    '' : { // Default language
        'some.key': 'This is the parameter: {1}'
    'de_DE': {
        'some.key': 'Zwei parameters: {1} {2}'

It can also be a string pointing to a file which exports the default language. If files are named according to the pattern filename_de_DE.js then it will automatically pick up the languages. For example:

Directory contains

  1. translations.js
  2. translations_de_DE.js

And the files contain

module.exports = {
    'some.key': 'This is the parameter: {1}'

This could then be used with localized-string

var localizedString = require('localized-string')('path/to/file/translations);