jellyfish-web/src/scripts/globalize.js

301 lines
8 KiB
JavaScript
Raw Normal View History

import isEmpty from 'lodash-es/isEmpty';
import { currentSettings as userSettings } from './settings/userSettings';
import Events from '../utils/events.ts';
2022-10-13 13:59:48 -04:00
const Direction = {
2022-10-14 17:06:25 -04:00
rtl: 'rtl',
ltr: 'ltr'
2022-10-13 13:59:48 -04:00
};
2020-07-18 09:21:15 +01:00
/* eslint-disable indent */
2018-10-23 01:05:09 +03:00
2020-07-18 09:21:15 +01:00
const fallbackCulture = 'en-us';
2022-07-08 12:41:58 -04:00
const RTL_LANGS = ['ar', 'fa', 'ur', 'he'];
2020-07-18 09:21:15 +01:00
const allTranslations = {};
let currentCulture;
let currentDateTimeCulture;
2022-07-02 15:50:52 -04:00
let isRTL = false;
2020-07-18 09:21:15 +01:00
export function getCurrentLocale() {
return currentCulture;
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
export function getCurrentDateTimeLocale() {
return currentDateTimeCulture;
2018-10-23 01:05:09 +03:00
}
function getDefaultLanguage() {
2020-07-18 09:21:15 +01:00
const culture = document.documentElement.getAttribute('data-culture');
if (culture) {
return culture;
}
if (navigator.language) {
return navigator.language;
}
if (navigator.userLanguage) {
return navigator.userLanguage;
}
if (navigator.languages && navigator.languages.length) {
return navigator.languages[0];
}
return fallbackCulture;
2018-10-23 01:05:09 +03:00
}
2022-07-02 15:50:52 -04:00
export function getIsRTL() {
return isRTL;
}
2022-07-08 12:41:58 -04:00
function checkAndProcessDir(culture) {
2022-07-08 13:25:59 -04:00
isRTL = false;
2022-10-13 13:59:48 -04:00
console.log(culture);
2022-07-08 12:41:58 -04:00
for (const lang of RTL_LANGS) {
if (culture.includes(lang)) {
isRTL = true;
break;
}
}
2022-10-13 13:59:48 -04:00
setDocumentDirection(isRTL ? Direction.rtl : Direction.ltr);
2022-07-08 12:41:58 -04:00
}
2022-10-13 13:59:48 -04:00
function setDocumentDirection(direction) {
document.getElementsByTagName('body')[0].setAttribute('dir', direction);
document.getElementsByTagName('html')[0].setAttribute('dir', direction);
if (direction === Direction.rtl)
import('../styles/rtl.scss');
2022-07-08 12:41:58 -04:00
}
2022-10-13 13:59:48 -04:00
export function getIsElementRTL(element) {
2022-07-05 19:58:02 -04:00
if (window.getComputedStyle) { // all browsers
return window.getComputedStyle(element, null).getPropertyValue('direction') == 'rtl';
2022-07-05 19:58:02 -04:00
}
return element.currentStyle.direction == 'rtl';
2022-07-05 19:58:02 -04:00
}
2020-07-18 09:21:15 +01:00
export function updateCurrentCulture() {
let culture;
2018-10-23 01:05:09 +03:00
try {
culture = userSettings.language();
} catch (err) {
2020-02-16 03:44:43 +01:00
console.error('no language set in user settings');
}
culture = culture || getDefaultLanguage();
2022-07-08 12:41:58 -04:00
checkAndProcessDir(culture);
currentCulture = normalizeLocaleName(culture);
2020-07-18 09:21:15 +01:00
let dateTimeCulture;
2018-10-23 01:05:09 +03:00
try {
dateTimeCulture = userSettings.dateTimeLocale();
} catch (err) {
2020-02-16 03:44:43 +01:00
console.error('no date format set in user settings');
}
if (dateTimeCulture) {
currentDateTimeCulture = normalizeLocaleName(dateTimeCulture);
} else {
currentDateTimeCulture = currentCulture;
}
ensureTranslations(currentCulture);
2018-10-23 01:05:09 +03:00
}
function ensureTranslations(culture) {
2020-07-19 17:38:42 +02:00
for (const i in allTranslations) {
ensureTranslation(allTranslations[i], culture);
}
if (culture !== fallbackCulture) {
2020-07-19 17:38:42 +02:00
for (const i in allTranslations) {
ensureTranslation(allTranslations[i], fallbackCulture);
}
}
2018-10-23 01:05:09 +03:00
}
function ensureTranslation(translationInfo, culture) {
if (translationInfo.dictionaries[culture]) {
return Promise.resolve();
}
return loadTranslation(translationInfo.translations, culture).then(function (dictionary) {
translationInfo.dictionaries[culture] = dictionary;
});
2018-10-23 01:05:09 +03:00
}
function normalizeLocaleName(culture) {
return culture.replace('_', '-').toLowerCase();
2018-10-23 01:05:09 +03:00
}
function getDictionary(module, locale) {
if (!module) {
module = defaultModule();
}
2020-07-18 09:21:15 +01:00
const translations = allTranslations[module];
if (!translations) {
return {};
}
return translations.dictionaries[locale];
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
export function register(options) {
2018-10-23 01:05:09 +03:00
allTranslations[options.name] = {
translations: options.strings || options.translations,
dictionaries: {}
};
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
export function loadStrings(options) {
const locale = getCurrentLocale();
const promises = [];
let optionsName;
if (typeof options === 'string') {
2019-04-20 20:44:42 +02:00
optionsName = options;
} else {
2019-04-20 20:44:42 +02:00
optionsName = options.name;
register(options);
}
2019-04-20 20:44:42 +02:00
promises.push(ensureTranslation(allTranslations[optionsName], locale));
promises.push(ensureTranslation(allTranslations[optionsName], fallbackCulture));
return Promise.all(promises);
2018-10-23 01:05:09 +03:00
}
function loadTranslation(translations, lang) {
lang = normalizeLocaleName(lang);
2020-07-18 09:21:15 +01:00
let filtered = translations.filter(function (t) {
return normalizeLocaleName(t.lang) === lang;
2018-10-23 01:05:09 +03:00
});
if (!filtered.length) {
lang = lang.replace(/-.*/, '');
filtered = translations.filter(function (t) {
return normalizeLocaleName(t.lang) === lang;
});
if (!filtered.length) {
filtered = translations.filter(function (t) {
return normalizeLocaleName(t.lang) === fallbackCulture;
});
}
}
2021-01-26 22:20:12 -05:00
return new Promise(function (resolve) {
if (!filtered.length) {
resolve();
return;
}
const url = filtered[0].path;
2020-08-16 20:24:45 +02:00
import(`../strings/${url}`).then((fileContent) => {
resolve(fileContent);
}).catch(() => {
resolve({});
2020-08-16 20:24:45 +02:00
});
});
2018-10-23 01:05:09 +03:00
}
function translateKey(key) {
2020-07-18 09:21:15 +01:00
const parts = key.split('#');
let module;
if (parts.length > 1) {
module = parts[0];
key = parts[1];
}
return translateKeyFromModule(key, module);
2018-10-23 01:05:09 +03:00
}
function translateKeyFromModule(key, module) {
2020-07-18 09:21:15 +01:00
let dictionary = getDictionary(module, getCurrentLocale());
2021-09-08 13:11:20 -04:00
if (dictionary && dictionary[key]) {
return dictionary[key];
}
dictionary = getDictionary(module, fallbackCulture);
if (dictionary && dictionary[key]) {
return dictionary[key];
}
2021-09-08 13:11:20 -04:00
if (!dictionary || isEmpty(dictionary)) {
console.warn('Translation dictionary is empty.');
} else {
console.error(`Translation key is missing from dictionary: ${key}`);
}
return key;
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
export function translate(key) {
let val = translateKey(key);
for (let i = 1; i < arguments.length; i++) {
val = val.replaceAll('{' + (i - 1) + '}', arguments[i].toLocaleString(currentCulture));
}
return val;
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
export function translateHtml(html, module) {
2020-08-16 20:24:45 +02:00
html = html.default || html;
if (!module) {
module = defaultModule();
}
if (!module) {
throw new Error('module cannot be null or empty');
}
2020-07-18 09:21:15 +01:00
let startIndex = html.indexOf('${');
if (startIndex === -1) {
return html;
}
2018-10-23 01:05:09 +03:00
startIndex += 2;
2020-07-18 09:21:15 +01:00
const endIndex = html.indexOf('}', startIndex);
if (endIndex === -1) {
return html;
}
2020-07-18 09:21:15 +01:00
const key = html.substring(startIndex, endIndex);
const val = translateKeyFromModule(key, module);
html = html.replace('${' + key + '}', val);
return translateHtml(html, module);
2018-10-23 01:05:09 +03:00
}
2020-07-18 09:21:15 +01:00
let _defaultModule;
export function defaultModule(val) {
if (val) {
_defaultModule = val;
}
return _defaultModule;
2018-10-23 01:05:09 +03:00
}
updateCurrentCulture();
2020-09-08 02:05:02 -04:00
Events.on(userSettings, 'change', function (e, name) {
if (name === 'language' || name === 'datetimelocale') {
updateCurrentCulture();
}
});
2020-07-18 09:21:15 +01:00
export default {
translate,
translateHtml,
loadStrings,
defaultModule,
getCurrentLocale,
getCurrentDateTimeLocale,
register,
2022-07-02 15:50:52 -04:00
updateCurrentCulture,
2022-07-05 19:58:02 -04:00
getIsRTL,
2022-10-13 13:59:48 -04:00
getIsElementRTL
2020-07-18 09:21:15 +01:00
};
/* eslint-enable indent */