2023-05-23 19:17:28 +00:00
|
|
|
import {hooks} from 'app/client/Hooks';
|
2022-09-29 08:01:37 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2022-10-19 11:43:55 +00:00
|
|
|
import {DomContents} from 'grainjs';
|
2022-09-29 08:01:37 +00:00
|
|
|
import i18next from 'i18next';
|
2022-10-19 18:44:56 +00:00
|
|
|
import {G} from 'grainjs/dist/cjs/lib/browserGlobals';
|
2022-09-29 08:01:37 +00:00
|
|
|
|
|
|
|
export async function setupLocale() {
|
|
|
|
const now = Date.now();
|
|
|
|
const supportedLngs = getGristConfig().supportedLngs ?? ['en'];
|
2023-01-24 13:13:18 +00:00
|
|
|
const lng = detectCurrentLang();
|
2022-10-13 09:23:55 +00:00
|
|
|
const ns = getGristConfig().namespaces ?? ['client'];
|
2022-09-29 08:01:37 +00:00
|
|
|
// Initialize localization plugin
|
|
|
|
try {
|
|
|
|
// We don't await this promise, as it is resolved synchronously due to initImmediate: false.
|
|
|
|
i18next.init({
|
|
|
|
// By default we use english language.
|
|
|
|
fallbackLng: 'en',
|
|
|
|
// We will load resources ourselves.
|
|
|
|
initImmediate: false,
|
|
|
|
// Read language from navigator object.
|
|
|
|
lng,
|
2022-10-13 09:23:55 +00:00
|
|
|
// By default we use client namespace.
|
|
|
|
defaultNS: 'client',
|
2022-09-29 08:01:37 +00:00
|
|
|
// Read namespaces that are supported by the server.
|
|
|
|
// TODO: this can be converted to a dynamic list of namespaces, for async components.
|
|
|
|
// for now just import all what server offers.
|
2022-10-13 09:23:55 +00:00
|
|
|
// We can fallback to client namespace for any addons.
|
|
|
|
fallbackNS: 'client',
|
2023-01-24 13:13:18 +00:00
|
|
|
ns
|
2022-09-29 08:01:37 +00:00
|
|
|
}).catch((err: any) => {
|
|
|
|
// This should not happen, the promise should be resolved synchronously, without
|
|
|
|
// any errors reported.
|
|
|
|
console.error("i18next failed unexpectedly", err);
|
|
|
|
});
|
|
|
|
// Detect what is resolved languages to load.
|
|
|
|
const languages = i18next.languages;
|
|
|
|
// Fetch all json files (all of which should be already preloaded);
|
2023-05-23 19:17:28 +00:00
|
|
|
const loadPath = `${hooks.baseURI || document.baseURI}locales/{{lng}}.{{ns}}.json`;
|
2022-09-29 08:01:37 +00:00
|
|
|
const pathsToLoad: Promise<any>[] = [];
|
|
|
|
async function load(lang: string, n: string) {
|
2023-01-24 13:13:18 +00:00
|
|
|
const resourceUrl = loadPath.replace('{{lng}}', lang.replace("-", "_")).replace('{{ns}}', n);
|
2022-09-29 08:01:37 +00:00
|
|
|
const response = await fetch(resourceUrl);
|
|
|
|
if (!response.ok) {
|
2023-02-03 23:56:24 +00:00
|
|
|
// Throw only if we don't have any fallbacks.
|
|
|
|
if (lang === i18next.options.fallbackLng && n === i18next.options.defaultNS) {
|
|
|
|
throw new Error(`Failed to load ${resourceUrl}`);
|
|
|
|
} else {
|
|
|
|
console.warn(`Failed to load ${resourceUrl}`);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-29 08:01:37 +00:00
|
|
|
}
|
|
|
|
i18next.addResourceBundle(lang, n, await response.json());
|
|
|
|
}
|
2023-01-24 13:13:18 +00:00
|
|
|
for (const lang of languages.filter((l) => supportedLngs.includes(l))) {
|
2022-09-29 08:01:37 +00:00
|
|
|
for (const n of ns) {
|
|
|
|
pathsToLoad.push(load(lang, n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await Promise.all(pathsToLoad);
|
|
|
|
console.log("Localization initialized in " + (Date.now() - now) + "ms");
|
|
|
|
} catch (error: any) {
|
|
|
|
reportError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:13:18 +00:00
|
|
|
export function detectCurrentLang() {
|
|
|
|
const { userLocale, supportedLngs } = getGristConfig();
|
|
|
|
const detected = userLocale
|
|
|
|
|| document.cookie.match(/grist_user_locale=([^;]+)/)?.[1]
|
|
|
|
|| window.navigator.language
|
|
|
|
|| 'en';
|
|
|
|
const supportedList = supportedLngs ?? ['en'];
|
|
|
|
// If we have this language in the list (or more general version) mark it as selected.
|
|
|
|
// Compare languages in lower case, as navigator.language can return en-US, en-us (for older Safari).
|
|
|
|
const selected = supportedList.find(supported => supported.toLowerCase() === detected.toLowerCase()) ??
|
|
|
|
supportedList.find(supported => supported === detected.split(/[-_]/)[0]) ?? 'en';
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setAnonymousLocale(lng: string) {
|
|
|
|
document.cookie = lng ? `grist_user_locale=${lng}; path=/; max-age=31536000`
|
|
|
|
: 'grist_user_locale=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC';
|
|
|
|
}
|
|
|
|
|
2022-09-29 08:01:37 +00:00
|
|
|
/**
|
2022-10-19 11:43:55 +00:00
|
|
|
* Resolves the translation of the given key using the given options.
|
2022-09-29 08:01:37 +00:00
|
|
|
*/
|
2022-10-19 18:44:56 +00:00
|
|
|
export function tString(key: string, args?: any, instance = i18next): string {
|
|
|
|
if (!instance.exists(key, args || undefined)) {
|
2022-09-29 08:01:37 +00:00
|
|
|
const error = new Error(`Missing translation for key: ${key} and language: ${i18next.language}`);
|
|
|
|
reportError(error);
|
|
|
|
}
|
2022-10-19 11:43:55 +00:00
|
|
|
return instance.t(key, args);
|
|
|
|
}
|
|
|
|
|
2022-10-20 08:34:38 +00:00
|
|
|
// We will try to infer result from the arguments passed to `t` function.
|
|
|
|
// For plain objects we expect string as a result. If any property doesn't look as a plain value
|
|
|
|
// we assume that it might be a dom node and the result is DomContents.
|
|
|
|
type InferResult<T> = T extends Record<string, string | number | boolean>|undefined|null ? string : DomContents;
|
2022-10-19 11:43:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the translation of the given key and substitutes. Supports dom elements interpolation.
|
|
|
|
*/
|
2022-12-09 19:14:59 +00:00
|
|
|
export function t<T extends Record<string, any>>(key: string, args?: T|null, instance = i18next): InferResult<T> {
|
|
|
|
return domT(key, args, instance.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
function domT(key: string, args: any, tImpl: typeof i18next.t) {
|
2022-10-19 11:43:55 +00:00
|
|
|
// If there are any DomElements in args, handle it with missingInterpolationHandler.
|
2022-10-19 18:44:56 +00:00
|
|
|
const domElements = !args ? [] : Object.entries(args).filter(([_, value]) => isLikeDomContents(value));
|
2022-10-19 11:43:55 +00:00
|
|
|
if (!args || !domElements.length) {
|
2022-12-27 18:35:03 +00:00
|
|
|
return tImpl(key, args || undefined);
|
2022-10-19 11:43:55 +00:00
|
|
|
} else {
|
|
|
|
// Make a copy of the arguments, and remove any dom elements from it. It will instruct
|
|
|
|
// i18next library to use `missingInterpolationHandler` handler.
|
|
|
|
const copy = {...args};
|
|
|
|
domElements.forEach(([prop]) => delete copy[prop]);
|
|
|
|
|
|
|
|
// Passing `missingInterpolationHandler` will allow as to resolve all missing keys
|
|
|
|
// and replace them with a marker.
|
2022-12-09 19:14:59 +00:00
|
|
|
const result: string = tImpl(key, {...copy, missingInterpolationHandler});
|
2022-10-19 11:43:55 +00:00
|
|
|
|
|
|
|
// Now replace all markers with dom elements passed as arguments.
|
2022-10-19 18:44:56 +00:00
|
|
|
const parts = result.split(/(\[\[\[[^\]]+?\]\]\])/);
|
2022-10-19 11:43:55 +00:00
|
|
|
for (let i = 1; i < parts.length; i += 2) { // Every second element is our dom element.
|
2022-10-19 18:44:56 +00:00
|
|
|
const propName = parts[i].substring(3, parts[i].length - 3);
|
2022-10-19 11:43:55 +00:00
|
|
|
const domElement = args[propName] ?? `{{${propName}}}`; // If the prop is not there, simulate default behavior.
|
|
|
|
parts[i] = domElement;
|
|
|
|
}
|
2022-10-20 08:34:38 +00:00
|
|
|
return parts.filter(p => p !== '') as any; // Remove empty parts.
|
2022-10-19 11:43:55 +00:00
|
|
|
}
|
2022-09-29 08:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given key exists in the any supported language.
|
|
|
|
*/
|
|
|
|
export function hasTranslation(key: string) {
|
|
|
|
return i18next.exists(key);
|
|
|
|
}
|
2022-10-19 11:43:55 +00:00
|
|
|
|
|
|
|
function missingInterpolationHandler(key: string, value: any) {
|
2022-10-19 18:44:56 +00:00
|
|
|
return `[[[${value[1]}]]]`;
|
2022-10-19 11:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Very naive detection if an element has DomContents type.
|
|
|
|
*/
|
|
|
|
function isLikeDomContents(value: any): boolean {
|
|
|
|
// As null and undefined are valid DomContents values, we don't treat them as such.
|
|
|
|
if (value === null || value === undefined) { return false; }
|
|
|
|
return value instanceof G.Node || // Node
|
|
|
|
(Array.isArray(value) && isLikeDomContents(value[0])) || // DomComputed
|
|
|
|
typeof value === 'function'; // DomMethod
|
|
|
|
}
|
2022-10-20 08:34:38 +00:00
|
|
|
|
|
|
|
/**
|
2022-12-09 19:14:59 +00:00
|
|
|
* Helper function to create a scoped t function. Scoped t function is bounded to a specific
|
|
|
|
* namespace and a key prefix (a scope).
|
2022-10-20 08:34:38 +00:00
|
|
|
*/
|
2022-12-09 19:14:59 +00:00
|
|
|
export function makeT(scope: string, instance?: typeof i18next) {
|
2022-12-12 10:29:16 +00:00
|
|
|
// Can't create the scopedInstance yet as it might not be initialized.
|
2022-12-09 19:14:59 +00:00
|
|
|
let scopedInstance: null|typeof i18next = null;
|
|
|
|
let scopedResolver: null|typeof i18next.t = null;
|
|
|
|
return function<T extends Record<string, any>>(key: string, args?: T|null) {
|
|
|
|
// Create a scoped instance with disabled namespace and nested features.
|
|
|
|
// This enables keys like `key1.key2:key3` to be resolved properly.
|
|
|
|
if (!scopedInstance) {
|
|
|
|
scopedInstance = (instance ?? i18next).cloneInstance({
|
|
|
|
keySeparator: false,
|
|
|
|
nsSeparator: false,
|
2023-01-05 11:10:10 +00:00
|
|
|
saveMissing: true,
|
|
|
|
missingKeyHandler: (lng, ns, _key) => console.warn(`Missing translation for key: ${_key}`)
|
2022-12-09 19:14:59 +00:00
|
|
|
});
|
2023-01-05 11:10:10 +00:00
|
|
|
|
2022-12-09 19:14:59 +00:00
|
|
|
// Create a version of `t` function that will use the provided prefix as default.
|
2023-01-05 11:10:10 +00:00
|
|
|
const fixedResolver = scopedInstance.getFixedT(null, null, scope);
|
|
|
|
|
|
|
|
// Override the resolver with a custom one, that will use the argument as a default.
|
|
|
|
// This will remove all the overloads from the function, but we don't need them.
|
|
|
|
scopedResolver = (_key: string, _args?: any) => fixedResolver(_key, {defaultValue: _key, ..._args});
|
2022-12-09 19:14:59 +00:00
|
|
|
}
|
|
|
|
return domT(key, args, scopedResolver!);
|
2022-12-27 18:35:03 +00:00
|
|
|
};
|
2022-10-20 08:34:38 +00:00
|
|
|
}
|