Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import mapTranslations from 'docs/src/modules/utils/mapTranslations';
const req = require.context('docs/translations', false, /translations.*\.json$/);
const translations = mapTranslations(req, 'json');
function getPath(obj, path) {
if (!path || typeof path !== 'string') {
return null;
}
return path.split('.').reduce((acc, item) => (acc && acc[item] ? acc[item] : null), obj);
}
const warnOnce = {};
const getT = memoize(userLanguage => (key, options = {}) => {
const { ignoreWarning = false } = options;
const wordings = translations[userLanguage];
if (!wordings) {
console.error(`Missing language: ${userLanguage}.`);
return '…';
}
const translation = getPath(wordings, key);
if (!translation) {
const fullKey = `${userLanguage}:${key}`;
// No warnings in CI env
if (!ignoreWarning && !warnOnce[fullKey] && typeof window !== 'undefined') {
console.error(`Missing translation for ${fullKey}.`);
warnOnce[fullKey] = true;