Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-polyglot' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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
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);
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
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)
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 {
function onMissingKey(key, opts, locale) {
if (locale !== defaultLang) {
console.warn('Missing translation for key: "' + key + '"');
}
return Polyglot.transformPhrase(key, opts, locale);
}
function onMissingKey(key, opts, locale) {
if (locale !== defaultLang) {
console.warn('Missing translation for key: "' + key + '"')
}
return Polyglot.transformPhrase(key, opts, locale)
}
({ locale, messages = {} }) => {
const polyglot = new Polyglot({
locale,
phrases: defaultsDeep({}, messages, defaultMessages),
});
return {
locale,
translate: polyglot.t.bind(polyglot),
};
}
);
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}`);
},
};
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()
})
}