shared-lib-web 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +381 -127
- package/dist/components/app-brand.d.ts.map +1 -1
- package/dist/components/app-brand.js +9 -5
- package/dist/components/app-brand.js.map +1 -1
- package/dist/components/coming-soon.d.ts.map +1 -1
- package/dist/components/coming-soon.js +3 -2
- package/dist/components/coming-soon.js.map +1 -1
- package/dist/components/language-switcher.d.ts +5 -1
- package/dist/components/language-switcher.d.ts.map +1 -1
- package/dist/components/language-switcher.js +20 -7
- package/dist/components/language-switcher.js.map +1 -1
- package/dist/components/settings-menu.d.ts +4 -0
- package/dist/components/settings-menu.d.ts.map +1 -1
- package/dist/components/settings-menu.js +15 -2
- package/dist/components/settings-menu.js.map +1 -1
- package/dist/context/use-ui.d.ts.map +1 -1
- package/dist/context/use-ui.js +4 -3
- package/dist/context/use-ui.js.map +1 -1
- package/dist/i18n/browser-language.d.ts +9 -0
- package/dist/i18n/browser-language.d.ts.map +1 -0
- package/dist/i18n/browser-language.js +89 -0
- package/dist/i18n/browser-language.js.map +1 -0
- package/dist/i18n/default-messages.d.ts.map +1 -1
- package/dist/i18n/default-messages.js +3 -0
- package/dist/i18n/default-messages.js.map +1 -1
- package/dist/i18n/locale-groups.d.ts +18 -0
- package/dist/i18n/locale-groups.d.ts.map +1 -0
- package/dist/i18n/locale-groups.js +74 -0
- package/dist/i18n/locale-groups.js.map +1 -0
- package/dist/i18n/locale-service.d.ts +22 -4
- package/dist/i18n/locale-service.d.ts.map +1 -1
- package/dist/i18n/locale-service.js +95 -27
- package/dist/i18n/locale-service.js.map +1 -1
- package/dist/i18n/locale-signal.d.ts +18 -0
- package/dist/i18n/locale-signal.d.ts.map +1 -0
- package/dist/i18n/locale-signal.js +142 -0
- package/dist/i18n/locale-signal.js.map +1 -0
- package/dist/i18n/translator.d.ts +8 -1
- package/dist/i18n/translator.d.ts.map +1 -1
- package/dist/i18n/translator.js +36 -12
- package/dist/i18n/translator.js.map +1 -1
- package/dist/index.d.ts +21 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -8
- package/dist/index.js.map +1 -1
- package/dist/interfaces/app-types.d.ts +6 -0
- package/dist/interfaces/app-types.d.ts.map +1 -1
- package/dist/interfaces/ui-config.d.ts +162 -15
- package/dist/interfaces/ui-config.d.ts.map +1 -1
- package/dist/interfaces/ui-context.d.ts +1 -1
- package/dist/interfaces/ui-context.d.ts.map +1 -1
- package/dist/layouts/auth-layout.js +1 -1
- package/dist/layouts/auth-layout.js.map +1 -1
- package/dist/providers/provider-effects.d.ts +2 -2
- package/dist/providers/provider-effects.d.ts.map +1 -1
- package/dist/providers/provider-effects.js +4 -1
- package/dist/providers/provider-effects.js.map +1 -1
- package/dist/providers/register-config.d.ts.map +1 -1
- package/dist/providers/register-config.js +2 -0
- package/dist/providers/register-config.js.map +1 -1
- package/dist/providers/shared-web-ui-provider.d.ts +7 -19
- package/dist/providers/shared-web-ui-provider.d.ts.map +1 -1
- package/dist/providers/shared-web-ui-provider.js +21 -22
- package/dist/providers/shared-web-ui-provider.js.map +1 -1
- package/dist/query/query-param.d.ts +4 -1
- package/dist/query/query-param.d.ts.map +1 -1
- package/dist/query/query-param.js +6 -3
- package/dist/query/query-param.js.map +1 -1
- package/dist/state/api-loader.d.ts +19 -0
- package/dist/state/api-loader.d.ts.map +1 -0
- package/dist/state/api-loader.js +187 -0
- package/dist/state/api-loader.js.map +1 -0
- package/dist/state/auth-service.d.ts +4 -0
- package/dist/state/auth-service.d.ts.map +1 -1
- package/dist/state/auth-service.js +47 -3
- package/dist/state/auth-service.js.map +1 -1
- package/dist/state/branch-service.d.ts +1 -1
- package/dist/state/branch-service.js +3 -3
- package/dist/state/branch-service.js.map +1 -1
- package/dist/state/company-service.d.ts +16 -0
- package/dist/state/company-service.d.ts.map +1 -0
- package/dist/state/company-service.js +97 -0
- package/dist/state/company-service.js.map +1 -0
- package/dist/state/language-service.d.ts +5 -2
- package/dist/state/language-service.d.ts.map +1 -1
- package/dist/state/language-service.js +12 -7
- package/dist/state/language-service.js.map +1 -1
- package/dist/state/layout-choice-service.d.ts +10 -0
- package/dist/state/layout-choice-service.d.ts.map +1 -0
- package/dist/state/layout-choice-service.js +34 -0
- package/dist/state/layout-choice-service.js.map +1 -0
- package/dist/state/menu-service.d.ts +1 -1
- package/dist/state/menu-service.js +2 -2
- package/dist/state/menu-service.js.map +1 -1
- package/dist/state/preference-sync.d.ts +1 -1
- package/dist/state/preference-sync.d.ts.map +1 -1
- package/dist/state/preference-sync.js +11 -3
- package/dist/state/preference-sync.js.map +1 -1
- package/dist/state/signals.d.ts +9 -1
- package/dist/state/signals.d.ts.map +1 -1
- package/dist/state/signals.js +28 -4
- package/dist/state/signals.js.map +1 -1
- package/dist/theme/custom-themes.d.ts +28 -0
- package/dist/theme/custom-themes.d.ts.map +1 -0
- package/dist/theme/custom-themes.js +218 -0
- package/dist/theme/custom-themes.js.map +1 -0
- package/dist/theme/dark-themes.d.ts +9 -1
- package/dist/theme/dark-themes.d.ts.map +1 -1
- package/dist/theme/dark-themes.js +74 -2
- package/dist/theme/dark-themes.js.map +1 -1
- package/dist/theme/logo.d.ts +6 -3
- package/dist/theme/logo.d.ts.map +1 -1
- package/dist/theme/logo.js +17 -3
- package/dist/theme/logo.js.map +1 -1
- package/dist/theme/theme-service.d.ts +14 -3
- package/dist/theme/theme-service.d.ts.map +1 -1
- package/dist/theme/theme-service.js +64 -10
- package/dist/theme/theme-service.js.map +1 -1
- package/dist/utils/app-http.d.ts +29 -1
- package/dist/utils/app-http.d.ts.map +1 -1
- package/dist/utils/app-http.js +113 -11
- package/dist/utils/app-http.js.map +1 -1
- package/dist/utils/content-util.d.ts +6 -0
- package/dist/utils/content-util.d.ts.map +1 -0
- package/dist/utils/content-util.js +45 -0
- package/dist/utils/content-util.js.map +1 -0
- package/dist/utils/error-util.d.ts +3 -0
- package/dist/utils/error-util.d.ts.map +1 -0
- package/dist/utils/error-util.js +58 -0
- package/dist/utils/error-util.js.map +1 -0
- package/dist/utils/language-util.d.ts +3 -3
- package/dist/utils/language-util.d.ts.map +1 -1
- package/dist/utils/language-util.js +7 -7
- package/dist/utils/language-util.js.map +1 -1
- package/dist/utils/menu-util.js +1 -1
- package/dist/utils/menu-util.js.map +1 -1
- package/dist/utils/signal-source.d.ts +4 -0
- package/dist/utils/signal-source.d.ts.map +1 -0
- package/dist/utils/signal-source.js +13 -0
- package/dist/utils/signal-source.js.map +1 -0
- package/dist/utils/storage-keys.d.ts +5 -0
- package/dist/utils/storage-keys.d.ts.map +1 -1
- package/dist/utils/storage-keys.js +5 -0
- package/dist/utils/storage-keys.js.map +1 -1
- package/package.json +3 -3
- package/src/components/app-brand.tsx +9 -5
- package/src/components/coming-soon.tsx +3 -2
- package/src/components/language-switcher.tsx +26 -7
- package/src/components/settings-menu.tsx +28 -2
- package/src/context/use-ui.ts +4 -3
- package/src/i18n/browser-language.ts +98 -0
- package/src/i18n/default-messages.ts +3 -0
- package/src/i18n/locale-groups.ts +87 -0
- package/src/i18n/locale-service.ts +99 -27
- package/src/i18n/locale-signal.ts +173 -0
- package/src/i18n/translator.ts +38 -12
- package/src/index.ts +71 -3
- package/src/interfaces/app-types.ts +6 -0
- package/src/interfaces/ui-config.ts +168 -15
- package/src/interfaces/ui-context.ts +1 -1
- package/src/layouts/auth-layout.tsx +1 -1
- package/src/providers/provider-effects.ts +5 -2
- package/src/providers/register-config.ts +2 -0
- package/src/providers/shared-web-ui-provider.tsx +26 -43
- package/src/query/query-param.ts +6 -2
- package/src/state/api-loader.ts +205 -0
- package/src/state/auth-service.ts +58 -3
- package/src/state/branch-service.ts +3 -3
- package/src/state/company-service.ts +107 -0
- package/src/state/language-service.ts +12 -7
- package/src/state/layout-choice-service.ts +40 -0
- package/src/state/menu-service.ts +2 -2
- package/src/state/preference-sync.ts +11 -3
- package/src/state/signals.ts +36 -4
- package/src/theme/custom-themes.ts +236 -0
- package/src/theme/dark-themes.ts +80 -3
- package/src/theme/logo.ts +19 -4
- package/src/theme/org-themes.json +70 -0
- package/src/theme/theme-service.ts +70 -10
- package/src/utils/app-http.ts +125 -11
- package/src/utils/content-util.ts +49 -0
- package/src/utils/error-util.ts +64 -0
- package/src/utils/language-util.ts +7 -7
- package/src/utils/menu-util.ts +1 -1
- package/src/utils/signal-source.ts +15 -0
- package/src/utils/storage-keys.ts +6 -0
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// app-brand.tsx
|
|
2
2
|
// Logo (light or dark version) and app name. Used by the header, sidebar and auth layout.
|
|
3
|
+
// Company details (config.loadCompanyDetails / setCompanyDetails) win: their logoUrl, appName and companyName (alt).
|
|
3
4
|
|
|
4
5
|
import { useUi } from "../context/use-ui.ts";
|
|
5
6
|
import { PageLink } from "../query/page-link.tsx";
|
|
6
|
-
import {
|
|
7
|
+
import { readAppNameLabel } from "../state/company-service.ts";
|
|
8
|
+
import { companyDetailsSignal, isDarkThemeSignal } from "../state/signals.ts";
|
|
7
9
|
import { readLogoUrl } from "../theme/logo.ts";
|
|
8
10
|
import { joinClassNames } from "../types/class-names.ts";
|
|
9
11
|
|
|
@@ -19,9 +21,11 @@ export function AppBrand(props: AppBrandProps) {
|
|
|
19
21
|
const { isNameHidden = false, nameClassName, className } = props;
|
|
20
22
|
const { config, t } = useUi();
|
|
21
23
|
const isDarkTheme = isDarkThemeSignal.value;
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
24
|
+
const companyDetails = companyDetailsSignal.value;
|
|
25
|
+
const logoUrl = readLogoUrl(config, isDarkTheme, companyDetails);
|
|
26
|
+
const logoAlt = companyDetails?.companyName ?? "";
|
|
27
|
+
const appNameLabel = readAppNameLabel(config, companyDetails);
|
|
28
|
+
const appName = appNameLabel ? t(appNameLabel) : "";
|
|
25
29
|
const homePath = config.homePath ?? "/";
|
|
26
30
|
const linkClassList = ["flex min-w-0 items-center gap-2", className];
|
|
27
31
|
const linkClassName = joinClassNames(linkClassList);
|
|
@@ -30,7 +34,7 @@ export function AppBrand(props: AppBrandProps) {
|
|
|
30
34
|
|
|
31
35
|
return (
|
|
32
36
|
<PageLink page={homePath} className={linkClassName} label={appName}>
|
|
33
|
-
{logoUrl ? <img src={logoUrl} alt=
|
|
37
|
+
{logoUrl ? <img src={logoUrl} alt={logoAlt} className="h-8 w-auto shrink-0" /> : null}
|
|
34
38
|
{isNameHidden ? null : <span className={nameClass}>{appName}</span>}
|
|
35
39
|
</PageLink>
|
|
36
40
|
);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// { path: "/reports", element: <ComingSoon /> }
|
|
5
5
|
|
|
6
6
|
import { useUi } from "../context/use-ui.ts";
|
|
7
|
-
import { isDarkThemeSignal } from "../state/signals.ts";
|
|
7
|
+
import { companyDetailsSignal, isDarkThemeSignal } from "../state/signals.ts";
|
|
8
8
|
import { readLogoUrl } from "../theme/logo.ts";
|
|
9
9
|
import { TypeIcon } from "../types/type.icon.ts";
|
|
10
10
|
|
|
@@ -17,7 +17,8 @@ export function ComingSoon(props: ComingSoonProps) {
|
|
|
17
17
|
const { titleKey = "ui.comingSoon" } = props;
|
|
18
18
|
const { config, t } = useUi();
|
|
19
19
|
const isDarkTheme = isDarkThemeSignal.value;
|
|
20
|
-
const
|
|
20
|
+
const companyDetails = companyDetailsSignal.value;
|
|
21
|
+
const logoUrl = readLogoUrl(config, isDarkTheme, companyDetails);
|
|
21
22
|
const titleText = t(titleKey);
|
|
22
23
|
const bodyText = t("ui.comingSoonText");
|
|
23
24
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// language-switcher.tsx
|
|
2
2
|
// Dropdown that lists the languages (config.languages or the lang reply) and switches the language.
|
|
3
|
-
// Hidden while there is no list.
|
|
3
|
+
// Hidden while there is no list (or only one language).
|
|
4
|
+
// <LanguageSwitcher /> -> 🌐 EN (compact: header)
|
|
5
|
+
// <LanguageSwitcher isFullName /> -> 🌐 English (login / AuthLayout: the user may not know the codes)
|
|
4
6
|
|
|
5
7
|
import { readBaseLanguageCode } from "../i18n/direction.ts";
|
|
6
8
|
import type { MouseEvent, ReactElement } from "react";
|
|
@@ -52,14 +54,31 @@ function buildLanguageItems(languages: LanguageOption[], activeCode: string): Re
|
|
|
52
54
|
return items;
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
export
|
|
57
|
+
export interface LanguageSwitcherProps {
|
|
58
|
+
/** Show the full name of the current language ("English", "Français") instead of the code ("EN"). */
|
|
59
|
+
isFullName?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The button text: the language name (isFullName) or the upper-case base code ("EN"). */
|
|
63
|
+
function readButtonText(languages: LanguageOption[], locale: string, isFullName: boolean): string {
|
|
64
|
+
if (isFullName) {
|
|
65
|
+
for (const language of languages) {
|
|
66
|
+
if (language.code === locale) {
|
|
67
|
+
return language.label;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const baseCode = readBaseLanguageCode(locale);
|
|
72
|
+
return baseCode.toUpperCase();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function LanguageSwitcher(props: LanguageSwitcherProps = {}) {
|
|
56
76
|
const { languages, locale, t } = useUi();
|
|
57
77
|
const languageText = t("ui.language");
|
|
58
78
|
const items = buildLanguageItems(languages, locale);
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (languages.length === 0) {
|
|
79
|
+
const buttonText = readButtonText(languages, locale, props.isFullName === true);
|
|
80
|
+
// No list, or only one language: nothing to switch.
|
|
81
|
+
if (languages.length < 2) {
|
|
63
82
|
return null;
|
|
64
83
|
}
|
|
65
84
|
|
|
@@ -67,7 +86,7 @@ export function LanguageSwitcher() {
|
|
|
67
86
|
<div className="dropdown dropdown-end">
|
|
68
87
|
<div tabIndex={0} role="button" className="btn btn-ghost btn-sm gap-1" aria-label={languageText}>
|
|
69
88
|
<TypeIcon name="Globe" size={16} />
|
|
70
|
-
<span className="text-xs font-semibold">{
|
|
89
|
+
<span className="text-xs font-semibold">{buttonText}</span>
|
|
71
90
|
</div>
|
|
72
91
|
<ul tabIndex={0} className="dropdown-content menu bg-base-100 rounded-box z-40 mt-2 w-48 p-2 shadow-lg">
|
|
73
92
|
{items}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// settings-menu.tsx
|
|
2
2
|
// One settings button that opens a dialog with the language list (from config.languages),
|
|
3
|
-
// the theme list (from config.themes)
|
|
3
|
+
// the theme list (from config.themes), the layout (App / Aside, when config.layout.showLayoutChoice) and the navigation
|
|
4
|
+
// style. The branches (when the app passes them)
|
|
4
5
|
// come first, titled with config.branchLabel.
|
|
5
6
|
// Used by the header when config.layout.headerSettings is "menu"; can also be placed anywhere.
|
|
6
7
|
// isInSidebar: a full-width "Settings" row for the sidebar (icon only on the collapsed rail, with a rail tooltip).
|
|
@@ -10,6 +11,7 @@ import type { MouseEvent, ReactElement } from "react";
|
|
|
10
11
|
import { useUi } from "../context/use-ui.ts";
|
|
11
12
|
import { changeLanguage } from "../i18n/locale-service.ts";
|
|
12
13
|
import type { LanguageOption, NavigationMode, Theme } from "../interfaces/ui-config.ts";
|
|
14
|
+
import { changeLayoutChoice, layoutChoiceSignal, toLayoutChoice } from "../state/layout-choice-service.ts";
|
|
13
15
|
import { changeNavigationMode } from "../state/navigation-service.ts";
|
|
14
16
|
import {
|
|
15
17
|
isAsideLayoutSignal,
|
|
@@ -33,6 +35,8 @@ export interface SettingsMenuProps {
|
|
|
33
35
|
showNavigationMode?: boolean;
|
|
34
36
|
/** Sidebar look: full-width row with the label; icon only on the collapsed rail. Default false. */
|
|
35
37
|
isInSidebar?: boolean;
|
|
38
|
+
/** Show the App layout / Aside layout choice. Default config.layout.showLayoutChoice (false). */
|
|
39
|
+
showLayoutChoice?: boolean;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
export function openSettings(): void {
|
|
@@ -61,6 +65,13 @@ export function handleSettingsNavigationSelect(event: MouseEvent<HTMLButtonEleme
|
|
|
61
65
|
changeNavigationMode(mode);
|
|
62
66
|
}
|
|
63
67
|
|
|
68
|
+
/** Layout buttons carry "app" or "aside" in data-value. Saved as LAYOUT_CHOICE; the app switches its layout. */
|
|
69
|
+
export function handleSettingsLayoutSelect(event: MouseEvent<HTMLButtonElement>): void {
|
|
70
|
+
const value = readDataValue(event);
|
|
71
|
+
const choice = toLayoutChoice(value);
|
|
72
|
+
changeLayoutChoice(choice);
|
|
73
|
+
}
|
|
74
|
+
|
|
64
75
|
/** Builds one choice button. */
|
|
65
76
|
function buildChoice(
|
|
66
77
|
value: string,
|
|
@@ -142,7 +153,7 @@ export function SettingsMenu(props: SettingsMenuProps) {
|
|
|
142
153
|
const hasMenu = navigationSignal.value.length > 0;
|
|
143
154
|
const isNavigationModeAllowed = hasMenu && !isAsideLayout;
|
|
144
155
|
const showNavigationMode = isNavigationModeAllowed && (props.showNavigationMode ?? true);
|
|
145
|
-
const hasLanguages = languages.length >
|
|
156
|
+
const hasLanguages = languages.length > 1;
|
|
146
157
|
const isCollapsed = isSidebarCollapsedSignal.value || isRailEverywhereSignal.value;
|
|
147
158
|
const showLabel = isInSidebar && !isCollapsed;
|
|
148
159
|
const isOpen = isSettingsOpenSignal.value;
|
|
@@ -155,6 +166,12 @@ export function SettingsMenu(props: SettingsMenuProps) {
|
|
|
155
166
|
const darkTitle = t("ui.darkThemes");
|
|
156
167
|
const sidebarText = t("ui.sidebar");
|
|
157
168
|
const megaMenuText = t("ui.megaMenu");
|
|
169
|
+
const showLayoutChoice = props.showLayoutChoice ?? config.layout?.showLayoutChoice ?? false;
|
|
170
|
+
const layoutChoice = layoutChoiceSignal.value;
|
|
171
|
+
const appLayoutText = t("ui.appLayout");
|
|
172
|
+
const asideLayoutText = t("ui.asideLayout");
|
|
173
|
+
const appLayoutChoice = buildChoice("app", appLayoutText, layoutChoice === "app", handleSettingsLayoutSelect);
|
|
174
|
+
const asideLayoutChoice = buildChoice("aside", asideLayoutText, layoutChoice === "aside", handleSettingsLayoutSelect);
|
|
158
175
|
const sidebarChoice = buildChoice("sidebar", sidebarText, navigationMode === "sidebar", handleSettingsNavigationSelect);
|
|
159
176
|
const megaMenuChoice = buildChoice("megamenu", megaMenuText, navigationMode === "megamenu", handleSettingsNavigationSelect);
|
|
160
177
|
|
|
@@ -208,6 +225,15 @@ export function SettingsMenu(props: SettingsMenuProps) {
|
|
|
208
225
|
) : null}
|
|
209
226
|
</div>
|
|
210
227
|
</section>
|
|
228
|
+
{showLayoutChoice ? (
|
|
229
|
+
<section>
|
|
230
|
+
<h4 className="mb-2 text-sm font-semibold opacity-70">{t("ui.layout")}</h4>
|
|
231
|
+
<div className="flex flex-wrap gap-2">
|
|
232
|
+
{appLayoutChoice}
|
|
233
|
+
{asideLayoutChoice}
|
|
234
|
+
</div>
|
|
235
|
+
</section>
|
|
236
|
+
) : null}
|
|
211
237
|
{showNavigationMode ? (
|
|
212
238
|
<section>
|
|
213
239
|
<h4 className="mb-2 text-sm font-semibold opacity-70">{t("ui.navigationMode")}</h4>
|
package/src/context/use-ui.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { changeLanguage } from "../i18n/locale-service.ts";
|
|
|
11
11
|
import { translate } from "../i18n/translator.ts";
|
|
12
12
|
import type { UiContextValue } from "../interfaces/ui-context.ts";
|
|
13
13
|
import { directionSignal, isTranslationReadySignal, languagesSignal, localeSignal, themeSignal } from "../state/signals.ts";
|
|
14
|
-
import { changeTheme, splitThemesByMode } from "../theme/theme-service.ts";
|
|
14
|
+
import { changeTheme, readThemeNames, splitThemesByMode } from "../theme/theme-service.ts";
|
|
15
15
|
import { UiContext } from "./ui-context.ts";
|
|
16
16
|
|
|
17
17
|
const USE_UI_OUTSIDE_PROVIDER = "shared-lib-web: useUi() must be used inside <SharedWebUiProvider config={config}>.";
|
|
@@ -23,7 +23,8 @@ export function useUi(): UiContextValue {
|
|
|
23
23
|
throw new Error(USE_UI_OUTSIDE_PROVIDER);
|
|
24
24
|
}
|
|
25
25
|
const config = contextState.config;
|
|
26
|
-
const
|
|
26
|
+
const themeNames = readThemeNames(config);
|
|
27
|
+
const themeGroups = splitThemesByMode(themeNames);
|
|
27
28
|
const uiValue: UiContextValue = {
|
|
28
29
|
config: config,
|
|
29
30
|
locale: localeSignal.value,
|
|
@@ -31,7 +32,7 @@ export function useUi(): UiContextValue {
|
|
|
31
32
|
direction: directionSignal.value,
|
|
32
33
|
isReady: isTranslationReadySignal.value,
|
|
33
34
|
languages: languagesSignal.value,
|
|
34
|
-
themes:
|
|
35
|
+
themes: themeNames,
|
|
35
36
|
lightThemes: themeGroups.lightThemes,
|
|
36
37
|
darkThemes: themeGroups.darkThemes,
|
|
37
38
|
t: translate,
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// browser-language.ts
|
|
2
|
+
// The languages the user's browser / operating system asks for, and the best match in a language list.
|
|
3
|
+
//
|
|
4
|
+
// navigator.languages = ["fr-CA", "fr", "en-US"] + list en-US, fr-FR, ar-SA -> "fr-FR"
|
|
5
|
+
//
|
|
6
|
+
// Order of the browser codes: navigator.languages, navigator.language, then the Intl locale (system setting).
|
|
7
|
+
// A code matches exactly (case does not matter), else by its base language ("fr-CA" or "fr" -> "fr-FR").
|
|
8
|
+
|
|
9
|
+
import type { LanguageOption } from "../interfaces/ui-config.ts";
|
|
10
|
+
|
|
11
|
+
const CODE_SEPARATOR = "-";
|
|
12
|
+
|
|
13
|
+
/** Adds a code once (codes like "fr_CA" become "fr-CA"). */
|
|
14
|
+
function addCode(codes: string[], code: string | undefined): void {
|
|
15
|
+
if (!code) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const normalizedCode = code.replace("_", CODE_SEPARATOR);
|
|
19
|
+
const isAdded = codes.includes(normalizedCode);
|
|
20
|
+
if (!isAdded) {
|
|
21
|
+
codes.push(normalizedCode);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The system locale from Intl ("" when it is not available). */
|
|
26
|
+
function readIntlLocale(): string {
|
|
27
|
+
if (typeof Intl === "undefined") {
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
const dateFormat = new Intl.DateTimeFormat();
|
|
31
|
+
const options = dateFormat.resolvedOptions();
|
|
32
|
+
return options.locale;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The language codes the browser / system prefers, best first. [] when there is no browser. */
|
|
36
|
+
export function readBrowserLanguageCodes(): string[] {
|
|
37
|
+
const codes: string[] = [];
|
|
38
|
+
if (typeof navigator === "undefined") {
|
|
39
|
+
return codes;
|
|
40
|
+
}
|
|
41
|
+
const preferredCodes = navigator.languages ?? [];
|
|
42
|
+
for (const code of preferredCodes) {
|
|
43
|
+
addCode(codes, code);
|
|
44
|
+
}
|
|
45
|
+
addCode(codes, navigator.language);
|
|
46
|
+
const intlLocale = readIntlLocale();
|
|
47
|
+
addCode(codes, intlLocale);
|
|
48
|
+
return codes;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The base language of a code: "fr-CA" -> "fr". */
|
|
52
|
+
function toBaseLanguage(code: string): string {
|
|
53
|
+
const parts = code.split(CODE_SEPARATOR);
|
|
54
|
+
const baseLanguage = parts[0] ?? "";
|
|
55
|
+
return baseLanguage.toLowerCase();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The list code that equals the code (any case). */
|
|
59
|
+
function findExactCode(code: string, languages: LanguageOption[]): string | undefined {
|
|
60
|
+
const lowerCode = code.toLowerCase();
|
|
61
|
+
for (const language of languages) {
|
|
62
|
+
const lowerListCode = language.code.toLowerCase();
|
|
63
|
+
if (lowerListCode === lowerCode) {
|
|
64
|
+
return language.code;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The first list code with the same base language ("fr" -> "fr-FR"). */
|
|
71
|
+
function findBaseLanguageCode(code: string, languages: LanguageOption[]): string | undefined {
|
|
72
|
+
const baseLanguage = toBaseLanguage(code);
|
|
73
|
+
for (const language of languages) {
|
|
74
|
+
const listBaseLanguage = toBaseLanguage(language.code);
|
|
75
|
+
if (listBaseLanguage === baseLanguage) {
|
|
76
|
+
return language.code;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The best list code for the browser codes: for each browser code (best first) an exact match, else the same base
|
|
84
|
+
* language. undefined when nothing matches.
|
|
85
|
+
*/
|
|
86
|
+
export function matchLanguageCode(browserCodes: string[], languages: LanguageOption[]): string | undefined {
|
|
87
|
+
for (const browserCode of browserCodes) {
|
|
88
|
+
const exactCode = findExactCode(browserCode, languages);
|
|
89
|
+
if (exactCode) {
|
|
90
|
+
return exactCode;
|
|
91
|
+
}
|
|
92
|
+
const baseLanguageCode = findBaseLanguageCode(browserCode, languages);
|
|
93
|
+
if (baseLanguageCode) {
|
|
94
|
+
return baseLanguageCode;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
@@ -45,6 +45,9 @@ export const DEFAULT_UI_MESSAGES: LocaleMessages = {
|
|
|
45
45
|
sidebar: "Sidebar",
|
|
46
46
|
megaMenu: "Mega menu",
|
|
47
47
|
navigationMode: "Navigation",
|
|
48
|
+
layout: "Layout",
|
|
49
|
+
appLayout: "App layout",
|
|
50
|
+
asideLayout: "Aside layout",
|
|
48
51
|
scrollToTop: "Scroll to top",
|
|
49
52
|
branch: "Branch",
|
|
50
53
|
openPreview: "Open preview",
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// locale-groups.ts
|
|
2
|
+
// Locale files split by group: one folder per language, one file per group (the file name is the group).
|
|
3
|
+
//
|
|
4
|
+
// locales/en-US/app.json { "name": "My App" } -> t("app.name")
|
|
5
|
+
// locales/en-US/nav.json { "users": "Users" } -> t("nav.users")
|
|
6
|
+
// locales/en-US/root.json { "REQUIRED": "This field is required" } -> t("REQUIRED") (top-level keys)
|
|
7
|
+
//
|
|
8
|
+
// // config.ts (Vite: import.meta.glob is lazy, only the files of the language in use are downloaded)
|
|
9
|
+
// const localeFiles = import.meta.glob("./locales/*/*.json");
|
|
10
|
+
// loadLocale: loadLocaleGroups.bind(null, localeFiles)
|
|
11
|
+
//
|
|
12
|
+
// Several folders can be merged ({ ...sharedFiles, ...ownFiles }): the same group in two folders is merged,
|
|
13
|
+
// the later file wins per key. Each file is flat, so the "group.key" rule (one sublevel) holds by itself.
|
|
14
|
+
|
|
15
|
+
import type { LocaleMessages, LocaleModule } from "../interfaces/ui-config.ts";
|
|
16
|
+
import { unwrapLocaleModule } from "./locale-service.ts";
|
|
17
|
+
|
|
18
|
+
/** One lazy file loader, as import.meta.glob returns them. */
|
|
19
|
+
export type LocaleFileLoader = () => Promise<unknown>;
|
|
20
|
+
|
|
21
|
+
/** path -> loader, for example the result of import.meta.glob("./locales/*\/*.json"). */
|
|
22
|
+
export type LocaleGroupFiles = Record<string, LocaleFileLoader>;
|
|
23
|
+
|
|
24
|
+
/** The file whose keys stay at the top level (dff-util messages like REQUIRED, or "greeting"). */
|
|
25
|
+
export const ROOT_LOCALE_GROUP = "root";
|
|
26
|
+
|
|
27
|
+
const JSON_EXTENSION = ".json";
|
|
28
|
+
|
|
29
|
+
/** Language code and group of a file path: "./locales/en-US/nav.json" -> { languageCode: "en-US", group: "nav" }. */
|
|
30
|
+
export function readLocaleFilePath(path: string): { languageCode: string; group: string } | undefined {
|
|
31
|
+
const parts = path.split("/");
|
|
32
|
+
const fileName = parts[parts.length - 1] ?? "";
|
|
33
|
+
const languageCode = parts[parts.length - 2] ?? "";
|
|
34
|
+
const isJsonFile = fileName.endsWith(JSON_EXTENSION);
|
|
35
|
+
if (!isJsonFile || languageCode.length === 0) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const groupLength = fileName.length - JSON_EXTENSION.length;
|
|
39
|
+
const group = fileName.slice(0, groupLength);
|
|
40
|
+
return { languageCode: languageCode, group: group };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Loads one group file and takes the messages out of the JSON module. */
|
|
44
|
+
async function loadGroupFile(loadFile: LocaleFileLoader): Promise<LocaleMessages> {
|
|
45
|
+
const fileModule = await loadFile();
|
|
46
|
+
return unwrapLocaleModule(fileModule as LocaleModule);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Adds one group file to the messages (the root file goes to the top level). */
|
|
50
|
+
function addGroupMessages(messages: LocaleMessages, group: string, groupMessages: LocaleMessages): void {
|
|
51
|
+
if (group === ROOT_LOCALE_GROUP) {
|
|
52
|
+
Object.assign(messages, groupMessages);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const existingGroup = messages[group];
|
|
56
|
+
const isExistingGroup = typeof existingGroup === "object" && existingGroup !== null;
|
|
57
|
+
messages[group] = isExistingGroup ? { ...existingGroup, ...groupMessages } : groupMessages;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Loads all group files of one language (in parallel) and returns one messages object, ready for loadLocale.
|
|
62
|
+
* Throws when the language has no files, so the next language is tried (browser language -> en-US).
|
|
63
|
+
*/
|
|
64
|
+
export async function loadLocaleGroups(files: LocaleGroupFiles, languageCode: string): Promise<LocaleMessages> {
|
|
65
|
+
const groups: string[] = [];
|
|
66
|
+
const loadings: Array<Promise<LocaleMessages>> = [];
|
|
67
|
+
const entries = Object.entries(files);
|
|
68
|
+
for (const [path, loadFile] of entries) {
|
|
69
|
+
const fileInfo = readLocaleFilePath(path);
|
|
70
|
+
if (fileInfo?.languageCode !== languageCode) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
groups.push(fileInfo.group);
|
|
74
|
+
const loading = loadGroupFile(loadFile);
|
|
75
|
+
loadings.push(loading);
|
|
76
|
+
}
|
|
77
|
+
if (groups.length === 0) {
|
|
78
|
+
throw new Error(`shared-lib-web: no locale files for ${languageCode}`);
|
|
79
|
+
}
|
|
80
|
+
const groupContents = await Promise.all(loadings);
|
|
81
|
+
const messages: LocaleMessages = {};
|
|
82
|
+
for (const [index, group] of groups.entries()) {
|
|
83
|
+
const groupMessages = groupContents[index] ?? {};
|
|
84
|
+
addGroupMessages(messages, group, groupMessages);
|
|
85
|
+
}
|
|
86
|
+
return messages;
|
|
87
|
+
}
|
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
//
|
|
6
6
|
// The loader is the language's own load() or config.loadLocale(code) (locales/{code}.json).
|
|
7
7
|
// The languages are config.languages or the backend list (languagesSignal). Loaded files are cached.
|
|
8
|
+
//
|
|
9
|
+
// First visit (nothing saved): config.defaultLanguage, else the browser / system language (navigator.languages,
|
|
10
|
+
// matched to the list: "fr-CA" -> "fr-FR"), else "en-US".
|
|
8
11
|
|
|
9
12
|
import type { LanguageOption, LocaleMessages, LocaleModule, UiConfig } from "../interfaces/ui-config.ts";
|
|
10
13
|
import {
|
|
@@ -17,7 +20,11 @@ import {
|
|
|
17
20
|
} from "../state/signals.ts";
|
|
18
21
|
import { AppStorage } from "../utils/app-storage.ts";
|
|
19
22
|
import { LANG } from "../utils/storage-keys.ts";
|
|
23
|
+
import { matchLanguageCode, readBrowserLanguageCodes } from "./browser-language.ts";
|
|
24
|
+
import { isSignalSource } from "../utils/signal-source.ts";
|
|
20
25
|
import { findLanguageOption } from "./direction.ts";
|
|
26
|
+
import { waitForSignalLocale } from "./locale-signal.ts";
|
|
27
|
+
import { findDeepLocaleKeys } from "./translator.ts";
|
|
21
28
|
|
|
22
29
|
const UI_CONFIG_MISSING = "shared-lib-web: config is missing. Wrap your app in <SharedWebUiProvider config={config}>.";
|
|
23
30
|
|
|
@@ -67,9 +74,21 @@ export function runLocaleLoader(languageCode: string): Promise<LocaleModule> | u
|
|
|
67
74
|
if (!loadLocale) {
|
|
68
75
|
return undefined;
|
|
69
76
|
}
|
|
77
|
+
if (isSignalSource(loadLocale)) {
|
|
78
|
+
return waitForSignalLocale(loadLocale, languageCode);
|
|
79
|
+
}
|
|
70
80
|
return loadLocale(languageCode);
|
|
71
81
|
}
|
|
72
82
|
|
|
83
|
+
/** Locale files allow one sublevel ("group.key"); deeper groups are never shown, so say so once per file. */
|
|
84
|
+
function warnAboutDeepKeys(languageCode: string, messages: LocaleMessages): void {
|
|
85
|
+
const deepKeys = findDeepLocaleKeys(messages);
|
|
86
|
+
if (deepKeys.length > 0) {
|
|
87
|
+
const keyList = deepKeys.join(", ");
|
|
88
|
+
console.warn(`shared-lib-web: ${languageCode}.json is deeper than "group.key" (one sublevel); not used: ${keyList}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
73
92
|
/** Loads (or reads from cache) the messages of one language. undefined when it cannot be loaded. */
|
|
74
93
|
export async function loadLocaleMessages(languageCode: string): Promise<LocaleMessages | undefined> {
|
|
75
94
|
const cachedMessages = loadedMessagesCache.get(languageCode);
|
|
@@ -83,6 +102,7 @@ export async function loadLocaleMessages(languageCode: string): Promise<LocaleMe
|
|
|
83
102
|
try {
|
|
84
103
|
const localeModule = await loading;
|
|
85
104
|
const messages = unwrapLocaleModule(localeModule);
|
|
105
|
+
warnAboutDeepKeys(languageCode, messages);
|
|
86
106
|
loadedMessagesCache.set(languageCode, messages);
|
|
87
107
|
return messages;
|
|
88
108
|
} catch {
|
|
@@ -100,36 +120,87 @@ export function readIsLanguageChosen(): boolean {
|
|
|
100
120
|
return isLanguageChosen;
|
|
101
121
|
}
|
|
102
122
|
|
|
103
|
-
/** Language used when
|
|
123
|
+
/** Language used when nothing else is set (no defaultLanguage, no browser match). */
|
|
104
124
|
export const DEFAULT_LANGUAGE = "en-US";
|
|
105
125
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
126
|
+
/**
|
|
127
|
+
* The language for keys missing in the current language, and the last default:
|
|
128
|
+
* config.defaultLanguage, else "en-US".
|
|
129
|
+
*/
|
|
130
|
+
export function readFallbackLanguage(config: UiConfig | null | undefined): string {
|
|
108
131
|
return config?.defaultLanguage || DEFAULT_LANGUAGE;
|
|
109
132
|
}
|
|
110
133
|
|
|
111
|
-
/**
|
|
112
|
-
export function
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
if (isStoredAvailable) {
|
|
117
|
-
return storedCode;
|
|
118
|
-
}
|
|
134
|
+
/** The browser / system language that is in the language list (undefined without a list or a match). */
|
|
135
|
+
export function readBrowserLanguage(): string | undefined {
|
|
136
|
+
const languages = languagesSignal.peek();
|
|
137
|
+
if (languages.length === 0) {
|
|
138
|
+
return undefined;
|
|
119
139
|
}
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
140
|
+
const browserCodes = readBrowserLanguageCodes();
|
|
141
|
+
return matchLanguageCode(browserCodes, languages);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The default language: config.defaultLanguage, else the browser / system language (when it is in the list),
|
|
146
|
+
* else "en-US".
|
|
147
|
+
*/
|
|
148
|
+
export function readDefaultLanguage(config: UiConfig | null | undefined): string {
|
|
149
|
+
if (config?.defaultLanguage) {
|
|
150
|
+
return config.defaultLanguage;
|
|
124
151
|
}
|
|
152
|
+
const browserLanguage = readBrowserLanguage();
|
|
153
|
+
return browserLanguage ?? readFallbackLanguage(config);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Adds a code once. */
|
|
157
|
+
function addCandidate(candidates: string[], code: string | undefined): void {
|
|
158
|
+
if (code && !candidates.includes(code)) {
|
|
159
|
+
candidates.push(code);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The languages to try on start, in order: the saved one, config.defaultLanguage, the browser / system languages
|
|
165
|
+
* (matched to the list; without a list all of them, a missing locale file is skipped), the fallback, the first listed.
|
|
166
|
+
*/
|
|
167
|
+
export function readStartLanguageCandidates(config: UiConfig): string[] {
|
|
168
|
+
const candidates: string[] = [];
|
|
169
|
+
const storedCode = AppStorage.getText(LANG);
|
|
170
|
+
addCandidate(candidates, storedCode);
|
|
171
|
+
addCandidate(candidates, config.defaultLanguage);
|
|
125
172
|
const languages = languagesSignal.peek();
|
|
173
|
+
const browserCodes = readBrowserLanguageCodes();
|
|
174
|
+
if (languages.length > 0) {
|
|
175
|
+
const browserLanguage = matchLanguageCode(browserCodes, languages);
|
|
176
|
+
addCandidate(candidates, browserLanguage);
|
|
177
|
+
} else {
|
|
178
|
+
for (const browserCode of browserCodes) {
|
|
179
|
+
addCandidate(candidates, browserCode);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const fallbackLanguage = readFallbackLanguage(config);
|
|
183
|
+
addCandidate(candidates, fallbackLanguage);
|
|
126
184
|
const firstLanguage = languages[0];
|
|
127
|
-
|
|
185
|
+
addCandidate(candidates, firstLanguage?.code);
|
|
186
|
+
return candidates;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Picks the language to start with: the first start candidate that may be used. */
|
|
190
|
+
export function readInitialLanguageCode(config: UiConfig): string {
|
|
191
|
+
const candidates = readStartLanguageCandidates(config);
|
|
192
|
+
for (const candidate of candidates) {
|
|
193
|
+
const isAvailable = isLanguageAvailable(candidate);
|
|
194
|
+
if (isAvailable) {
|
|
195
|
+
return candidate;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return readFallbackLanguage(config);
|
|
128
199
|
}
|
|
129
200
|
|
|
130
201
|
/** Loads the fallback language messages into fallbackTranslationSignal. */
|
|
131
202
|
export async function loadFallbackMessages(config: UiConfig): Promise<void> {
|
|
132
|
-
const fallbackCode =
|
|
203
|
+
const fallbackCode = readFallbackLanguage(config);
|
|
133
204
|
const fallbackMessages = await loadLocaleMessages(fallbackCode);
|
|
134
205
|
if (fallbackMessages) {
|
|
135
206
|
fallbackTranslationSignal.value = fallbackMessages;
|
|
@@ -168,20 +239,21 @@ export async function changeLanguage(languageCode: string): Promise<void> {
|
|
|
168
239
|
await applyLanguage(languageCode, true);
|
|
169
240
|
}
|
|
170
241
|
|
|
171
|
-
/**
|
|
242
|
+
/**
|
|
243
|
+
* Loads the start language and the fallback language. Called once by TranslationProvider.
|
|
244
|
+
* Tries readStartLanguageCandidates in order; a language whose file cannot be loaded is skipped.
|
|
245
|
+
*/
|
|
172
246
|
export async function initializeLanguage(): Promise<void> {
|
|
173
247
|
const config = readActiveConfig();
|
|
174
248
|
const storedCode = AppStorage.getText(LANG);
|
|
175
249
|
isLanguageChosen = Boolean(storedCode);
|
|
176
250
|
await loadFallbackMessages(config);
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const isDefaultApplied = await applyLanguage(defaultLanguage, false);
|
|
184
|
-
if (!isDefaultApplied) {
|
|
185
|
-
isTranslationReadySignal.value = true;
|
|
251
|
+
const candidates = readStartLanguageCandidates(config);
|
|
252
|
+
for (const candidate of candidates) {
|
|
253
|
+
const isApplied = await applyLanguage(candidate, false);
|
|
254
|
+
if (isApplied) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
186
257
|
}
|
|
258
|
+
isTranslationReadySignal.value = true;
|
|
187
259
|
}
|