@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
80 lines • 2.88 kB
JavaScript
export class FlowLocale {
constructor(botonicContext, flowLocales, defaultLocaleCode) {
this.botonicContext = botonicContext;
this.flowLocales = flowLocales;
this.defaultLocaleCode = defaultLocaleCode;
}
resolve() {
const priorityLocale = this.isLanguageDetectionEnabled()
? this.getPriorityLocale()
: this.botonicContext.session.user.systemLocale;
if (priorityLocale) {
const exactMatch = this.matchExactLocale(priorityLocale);
if (exactMatch) {
return this.applyLocale(exactMatch);
}
const languageMatch = this.matchLanguage(priorityLocale);
if (languageMatch) {
return this.applyLocale(languageMatch);
}
}
return this.applyLocale(this.getDefaultLocale());
}
isLanguageDetectionEnabled() {
return !!this.botonicContext.settings.languageDetectionEnabled;
}
/**
* Rules:
* - If user and system languages differ, user locale takes priority.
* - If both share the same language, the more specific locale wins.
* - If both have the same specificity, user locale wins.
*/
getPriorityLocale() {
const userLocale = this.botonicContext.session.user.locale;
const systemLocale = this.botonicContext.session.user.systemLocale;
if (!userLocale || !systemLocale) {
return undefined;
}
const userLanguage = this.getLanguage(userLocale);
const systemLanguage = this.getLanguage(systemLocale);
if (userLanguage !== systemLanguage) {
return userLocale;
}
const userIsSpecific = this.isSpecificLocale(userLocale);
const systemIsSpecific = this.isSpecificLocale(systemLocale);
if (userIsSpecific && !systemIsSpecific) {
return userLocale;
}
if (!userIsSpecific && systemIsSpecific) {
return systemLocale;
}
return userLocale;
}
matchExactLocale(locale) {
return this.flowLocales.includes(locale) ? locale : undefined;
}
matchLanguage(locale) {
const language = this.getLanguage(locale);
return this.flowLocales.includes(language) ? language : undefined;
}
getDefaultLocale() {
return this.defaultLocaleCode || 'en';
}
applyLocale(locale) {
this.botonicContext.session.user.systemLocale = locale;
this.botonicContext.updateBotSession({
user: {
systemLocale: locale,
},
});
// TODO: We need to update the webchat user.system_locale
return locale;
}
getLanguage(locale) {
return locale.split('-')[0];
}
isSpecificLocale(locale) {
return locale.includes('-');
}
}
//# sourceMappingURL=flow-locale.js.map