Helper function for locale detection

This commit is contained in:
Rainer Simon 2020-05-18 10:26:43 +02:00
parent 9e4f0bd3f3
commit bb09e7e3fe
2 changed files with 20 additions and 1 deletions

View File

@ -1,10 +1,10 @@
export { default as Environment } from './Environment'; export { default as Environment } from './Environment';
export { default as I18n } from './i18n';
export { default as TextAnnotator } from './TextAnnotator'; export { default as TextAnnotator } from './TextAnnotator';
export { default as WebAnnotation } from './WebAnnotation'; export { default as WebAnnotation } from './WebAnnotation';
export * from './editor'; export * from './editor';
export * from './highlighter'; export * from './highlighter';
export * from './i18n';
export * from './relations'; export * from './relations';
export * from './selection'; export * from './selection';
export * from './utils'; export * from './utils';

View File

@ -1,3 +1,5 @@
import I18n from '../i18n';
/** /**
* 'Deflates' the HTML contained in the given parent node. * 'Deflates' the HTML contained in the given parent node.
* Deflation will completely drop empty text nodes, and replace * Deflation will completely drop empty text nodes, and replace
@ -48,6 +50,9 @@ const deflateNodeList = parents => {
} }
/**
* Adds MS Edge polyfills for Element.matches and .closest methods.
*/
export const addPolyfills = () => { export const addPolyfills = () => {
if (!Element.prototype.matches) { if (!Element.prototype.matches) {
@ -67,4 +72,18 @@ export const addPolyfills = () => {
}; };
} }
}
/**
* Helper to init the i18n class with a pre-defined or auto-detected locale.
*/
export const setLocale = locale => {
try {
const l = locale === 'auto' ?
window.navigator.userLanguage || window.navigator.language : locale;
I18n.init(l);
} catch (error) {
console.warn(`Unsupported locale '${locale}'. Falling back to default en.`);
}
} }