gctGenerator/site/.vuepress/i18n/localeHelper.js

60 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2020-07-03 10:42:43 +09:00
import enUS from './en-US.json';
import deCH from './de-CH.json';
import frFR from './fr-FR.json';
2020-09-23 04:45:59 +09:00
import jaJP from './ja-JP.json';
2020-07-03 10:42:43 +09:00
const translations = {
enUS,
deCH,
frFR,
2020-09-23 04:45:59 +09:00
jaJP,
2020-07-03 10:42:43 +09:00
};
const getNestedProp = (obj, path) => {
const props = path.split('.');
try {
2020-07-10 07:57:16 +09:00
let currentProp = obj[props.shift()];
while (props.length > 0) currentProp = currentProp[props.shift()];
2020-07-03 10:42:43 +09:00
return currentProp;
} catch {
return null;
}
};
const translateCodeProp = (code, prop, locale, fallbackLocale, html = false) => {
const targetProp = html ? 'html' : 'content';
const title = code[prop].find((t) => t.lang === locale);
const fallbackTitle = code[prop].find((t) => t.lang === fallbackLocale);
if (title && title[targetProp]) code[prop] = title[targetProp];
else if (fallbackTitle && fallbackTitle[targetProp]) code[prop] = fallbackTitle[targetProp];
else code[prop] = null;
};
const translateInternal = (identifier, locale) => {
if (locale) {
const localeId = locale.replace('-', '');
if (translations[localeId] != null) {
const translatedAttribute = getNestedProp(translations[localeId], identifier);
if (translatedAttribute) return translatedAttribute;
}
}
return null;
};
export const translate = (identifier, locale, fallbackLocale = 'en-US') => {
const translatedAttribute = translateInternal(identifier, locale);
if (translatedAttribute) return translatedAttribute;
return translateInternal(identifier, fallbackLocale);
};
export const translateCode = (code, locale, fallbackLocale = 'en-US') => {
const cpy = {};
Object.assign(cpy, code);
translateCodeProp(cpy, 'title', locale, fallbackLocale);
translateCodeProp(cpy, 'description', locale, fallbackLocale, true);
return cpy;
};