How to use node-polyglot - 10 common examples

To help you get started, we’ve selected a few node-polyglot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github flow-typed / flow-typed / definitions / npm / node-polyglot_v2.x.x / test_node-polyglot_v2.x.x.js View on Github external
phrases: {},
  locale: "foo",
  allowMissing: true,
  onMissingKey: (key, opts, locale) => {
    console.log(key.length, Object.keys(opts), locale.length);

    return "foo";
  }
});
new Polyglot();

// $ExpectError
new Polyglot("foo");

Polyglot.transformPhrase("foo").length;
Polyglot.transformPhrase("foo", {}).length;
Polyglot.transformPhrase("foo", {}, "en-US").length;

instance.extend({ foo: "bar" });

// $ExpectError
instance.extend("foo");

instance.has("foo");

// $ExpectError
instance.has(1);

instance.t("foo").length;
instance.t("foo", {}).length;

// $ExpectError
github flow-typed / flow-typed / definitions / npm / node-polyglot_v2.x.x / test_node-polyglot_v2.x.x.js View on Github external
locale: "foo",
  allowMissing: true,
  onMissingKey: (key, opts, locale) => {
    console.log(key.length, Object.keys(opts), locale.length);

    return "foo";
  }
});
new Polyglot();

// $ExpectError
new Polyglot("foo");

Polyglot.transformPhrase("foo").length;
Polyglot.transformPhrase("foo", {}).length;
Polyglot.transformPhrase("foo", {}, "en-US").length;

instance.extend({ foo: "bar" });

// $ExpectError
instance.extend("foo");

instance.has("foo");

// $ExpectError
instance.has(1);

instance.t("foo").length;
instance.t("foo", {}).length;

// $ExpectError
instance.t(2);
github cozy / cozy-bar / src / lib / I18n.js View on Github external
const initTranslation = (lang, dictRequire, context, defaultLang = DEFAULT_LANG) => {
  _polyglot = new Polyglot({
    phrases: dictRequire(defaultLang),
    locale: defaultLang
  })

  // Load global locales
  if (lang && lang !== defaultLang) {
    try {
      const dict = dictRequire(lang)
      _polyglot.extend(dict)
      _polyglot.locale(lang)
    } catch (e) {
      console.warn(`The dict phrases for "${lang}" can't be loaded`)
    }
  }

  // Load context locales
github cozy / create-cozy-app / packages / cozy-scripts / template-vue / app / src / lib / I18n / translation.js View on Github external
export const initTranslation = (
  lang,
  dictRequire,
  defaultLang = DEFAULT_LANG
) => {
  _polyglot = new Polyglot({
    phrases: dictRequire(defaultLang),
    locale: defaultLang
  })

  // Load global locales
  if (lang && lang !== defaultLang) {
    try {
      const dict = dictRequire(lang)
      _polyglot.extend(dict)
      _polyglot.locale(lang)
    } catch (e) {
      // console.warn(`The dict phrases for "${lang}" can't be loaded`)
    }
  }

  return _polyglot.t.bind(_polyglot)
github ashlkv / sapsanasap / polyglot.js View on Github external
const Polyglot = require('node-polyglot');
const _ = require('lodash');
// TODO Determine locale
// TODO Per-user locale, not per-instance locale
const locale = process.env.LOCALE || 'en';

const moment = require('moment');
moment.locale(locale);

const SapsanPolyglot = function() {
	Polyglot.call(this);
};

SapsanPolyglot.prototype = Object.create(Polyglot.prototype);

// Extending polyglot extend method to allow non-string values (e.g. regexps and functions)
SapsanPolyglot.prototype.extend = function(morePhrases, prefix) {
	_.forEach(morePhrases, function(phrase, key) {
		var prefixedKey = prefix ? prefix + '.' + key : key;
		this.phrases[prefixedKey] = phrase;
	}.bind(this));
};

// Extending polyglot translate method to allow non-string values (e.g. regexps and functions)
SapsanPolyglot.prototype.t = function(key, options) {
	var phrase = this.phrases[key];
	var result;
	if (typeof phrase === 'function' || typeof phrase === 'object' ||  _.isRegExp(phrase)) {
		result = phrase
	} else  {
github Aalto-LeTech / a-plus / assets / js / translate.js View on Github external
function onMissingKey(key, opts, locale) {
  if (locale !== defaultLang) {
    console.warn('Missing translation for key: "' + key + '"');
  }
  return Polyglot.transformPhrase(key, opts, locale);
}
github Aalto-LeTech / a-plus / assets_src / translate-js / main.js View on Github external
function onMissingKey(key, opts, locale) {
  if (locale !== defaultLang) {
    console.warn('Missing translation for key: "' + key + '"')
  }
  return Polyglot.transformPhrase(key, opts, locale)
}
github marmelab / react-admin / packages / ra-core / src / i18n / TranslationProvider.js View on Github external
({ locale, messages = {} }) => {
        const polyglot = new Polyglot({
            locale,
            phrases: defaultsDeep({}, messages, defaultMessages),
        });

        return {
            locale,
            translate: polyglot.t.bind(polyglot),
        };
    }
);
github hand-dot / taskontable / src / i18n / index.js View on Github external
import Polyglot from 'node-polyglot';
import en from './en';
import ja from './ja';

const SUPPORTLANGAGES = ['en', 'ja'];

const polyglot = new Polyglot();
polyglot.extend({
  en,
  ja,
});
polyglot.locale((window.navigator.languages && window.navigator.languages[0])
|| window.navigator.language
|| window.navigator.userLanguage
|| window.navigator.browserLanguage);
const language = (() => (SUPPORTLANGAGES.indexOf(polyglot.locale()) >= 0 ? polyglot.locale() : 'en'))();
export default {
  t(key, arg) {
    return arg ? polyglot.t(`${language}.${key}`, arg) : polyglot.t(`${language}.${key}`);
  },
};
github germanrcuriel / conan-exiles-admin-map / src / middleware / language.js View on Github external
const languageMiddleware = (app) => {

  let language = config.SETTINGS.language

  if (!languages[language]) {
    language = 'en'
  }

  const polyglot = new Polyglot({
    locale: language,
    phrases: languages[language]
  })

  app.use((req, res, next) => {
    res.lang = {
      locale: language,
      phrases: polyglot.phrases
    }
    return next()
  })

}

node-polyglot

Give your JavaScript the ability to speak many languages.

BSD-2-Clause
Latest version published 5 months ago

Package Health Score

84 / 100
Full package analysis