
* web: fix esbuild issue with style sheets Getting ESBuild, Lit, and Storybook to all agree on how to read and parse stylesheets is a serious pain. This fix better identifies the value types (instances) being passed from various sources in the repo to the three *different* kinds of style processors we're using (the native one, the polyfill one, and whatever the heck Storybook does internally). Falling back to using older CSS instantiating techniques one era at a time seems to do the trick. It's ugly, but in the face of the aggressive styling we use to avoid Flashes of Unstyled Content (FLoUC), it's the logic with which we're left. In standard mode, the following warning appears on the console when running a Flow: ``` Autofocus processing was blocked because a document already has a focused element. ``` In compatibility mode, the following **error** appears on the console when running a Flow: ``` crawler-inject.js:1106 Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. at initDomMutationObservers (crawler-inject.js:1106:18) at crawler-inject.js:1114:24 at Array.forEach (<anonymous>) at initDomMutationObservers (crawler-inject.js:1114:10) at crawler-inject.js:1549:1 initDomMutationObservers @ crawler-inject.js:1106 (anonymous) @ crawler-inject.js:1114 initDomMutationObservers @ crawler-inject.js:1114 (anonymous) @ crawler-inject.js:1549 ``` Despite this error, nothing seems to be broken and flows work as anticipated. * web: fix locale prioritization scheme The locale priority algorithm had two problems: first, the order was incorrect, allowing the global default from globalAK() to override a lot of more precise settings; second, the algorithm would take outside locale overrides from the event handler, which was not necessary. This commit revises the locale prioritization scheme. It continues to watch for "change of locale" events from all sources (URL, browser, and user/brand/site internal settings), but if the event carries a suggested locale, that suggestion is ignored. Instead, when a change of locale event occurs, it re-runs the algorithm in priority order. That order is: - The URL query parameter `locale=` - The User's stated preference in `CurrentUser.attributes` - The Browser's stated locale - The Brand's stated preference in `CurrentBrand.attributes` - The authentik instance's setting `from window.globalAK()` - The default locale complied into the UI at build time. Note to @tanberry: We should note this order somewhere in the documentation, so that users are not "surprised" that their user preference (set in User Interface -> Settings -> User Details -> Locale) is not overriden by the browser's preference. (The setting they need is "Based on your browser" to make browser locale detection work.) * web: fix locale prioritization scheme The locale priority algorithm had two problems: first, the order was incorrect, allowing the global default from globalAK() to override a lot of more precise settings; second, the algorithm would take outside locale overrides from the event handler, which was not necessary. This commit revises the locale prioritization scheme. It continues to watch for "change of locale" events from all sources (URL, browser, and user/brand/site internal settings), but if the event carries a suggested locale, that suggestion is ignored. Instead, when a change of locale event occurs, it re-runs the algorithm in priority order. That order is: - The URL query parameter `locale=` - The User's stated preference in `CurrentUser.attributes` - The Browser's stated locale - The Brand's stated preference in `CurrentBrand.attributes` - The authentik instance's setting `from window.globalAK()` - The default locale complied into the UI at build time. Note to @tanberry: We should note this order somewhere in the documentation, so that users are not "surprised" that their user preference (set in User Interface -> Settings -> User Details -> Locale) is not overriden by the browser's preference. (The setting they need is "Based on your browser" to make browser locale detection work.) * web: locale patch for currentUser.settings Temporarily skipping currentUser.settings.locale as a source of truth because it's not portable between User/Admin and Flow; Flow in a logged-out state has no access to `/me`, but we need to probe `/me` for user settings. This conflict currently triggers a bug in the session heartbeat handler.
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { globalAK } from "@goauthentik/common/global";
|
|
|
|
import { LOCALES as RAW_LOCALES, enLocale } from "./definitions";
|
|
import { AkLocale } from "./types";
|
|
|
|
export const DEFAULT_LOCALE = "en";
|
|
|
|
export const EVENT_REQUEST_LOCALE = "ak-request-locale";
|
|
|
|
const TOMBSTONE = "⛼⛼tombstone⛼⛼";
|
|
|
|
// NOTE: This is the definition of the LOCALES table that most of the code uses. The 'definitions'
|
|
// file is relatively pure, but here we establish that we want the English locale to loaded when an
|
|
// application is first instantiated.
|
|
|
|
export const LOCALES = RAW_LOCALES.map((locale) =>
|
|
locale.code === "en" ? { ...locale, locale: async () => enLocale } : locale,
|
|
);
|
|
|
|
export function getBestMatchLocale(locale: string): AkLocale | undefined {
|
|
return LOCALES.find((l) => l.match.test(locale));
|
|
}
|
|
|
|
// This looks weird, but it's sensible: we have several candidates, and we want to find the first
|
|
// one that has a supported locale. Then, from *that*, we have to extract that first supported
|
|
// locale.
|
|
|
|
export function findSupportedLocale(candidates: string[]) {
|
|
const candidate = candidates.find((candidate: string) => getBestMatchLocale(candidate));
|
|
return candidate ? getBestMatchLocale(candidate) : undefined;
|
|
}
|
|
|
|
export function localeCodeFromUrl(param = "locale") {
|
|
const url = new URL(window.location.href);
|
|
return url.searchParams.get(param) || "";
|
|
}
|
|
|
|
// Get all locales we can, in order
|
|
// - Global authentik settings (contains user settings)
|
|
// - URL parameter
|
|
// - A requested code passed in, if any
|
|
// - Navigator
|
|
// - Fallback (en)
|
|
|
|
const isLocaleCandidate = (v: unknown): v is string =>
|
|
typeof v === "string" && v !== "" && v !== TOMBSTONE;
|
|
|
|
export function autoDetectLanguage(userReq = TOMBSTONE, brandReq = TOMBSTONE): string {
|
|
const localeCandidates: string[] = [
|
|
localeCodeFromUrl("locale"),
|
|
userReq,
|
|
window.navigator?.language ?? TOMBSTONE,
|
|
brandReq,
|
|
globalAK()?.locale ?? TOMBSTONE,
|
|
DEFAULT_LOCALE,
|
|
].filter(isLocaleCandidate);
|
|
|
|
const firstSupportedLocale = findSupportedLocale(localeCandidates);
|
|
|
|
if (!firstSupportedLocale) {
|
|
console.debug(
|
|
`authentik/locale: No locale found for '[${localeCandidates}.join(',')]', falling back to ${DEFAULT_LOCALE}`,
|
|
);
|
|
return DEFAULT_LOCALE;
|
|
}
|
|
|
|
return firstSupportedLocale.code;
|
|
}
|
|
|
|
export default autoDetectLanguage;
|