@warkypublic/svelix 0.1.15 → 0.1.17

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.
@@ -1,5 +1,5 @@
1
1
  import { type Readable } from "svelte/store";
2
- import type { GlobalStateStoreType } from "./GlobalStateStore.types";
2
+ import type { GlobalStateStoreType, SupportedLanguage } from "./GlobalStateStore.types";
3
3
  type PartialUpdater = Partial<GlobalStateStoreType> | ((state: GlobalStateStoreType) => Partial<GlobalStateStoreType>);
4
4
  type GlobalStateStoreApi = {
5
5
  getState: () => GlobalStateStoreType;
@@ -7,6 +7,7 @@ type GlobalStateStoreApi = {
7
7
  subscribe: (run: (value: GlobalStateStoreType) => void) => () => void;
8
8
  };
9
9
  declare const GlobalStateStore: GlobalStateStoreApi;
10
+ declare const languageStore: Readable<SupportedLanguage>;
10
11
  declare function useGlobalStateStore(): Readable<GlobalStateStoreType>;
11
12
  declare function useGlobalStateStore<T>(selector: (state: GlobalStateStoreType) => T): Readable<T>;
12
13
  declare const setApiURL: (url: string) => void;
@@ -15,5 +16,6 @@ declare const getAuthToken: () => string;
15
16
  declare const isLoggedIn: () => boolean;
16
17
  declare const setAuthToken: (token: string) => void;
17
18
  declare const GetGlobalState: () => GlobalStateStoreType;
19
+ declare const setLanguage: (lang: SupportedLanguage) => void;
18
20
  export type { GlobalStateStoreApi };
19
- export { getApiURL, getAuthToken, GetGlobalState, GlobalStateStore, isLoggedIn, setApiURL, setAuthToken, useGlobalStateStore, };
21
+ export { getApiURL, getAuthToken, GetGlobalState, GlobalStateStore, isLoggedIn, languageStore, setApiURL, setAuthToken, setLanguage, useGlobalStateStore, };
@@ -0,0 +1,35 @@
1
+ import { type Readable } from 'svelte/store';
2
+ import type { SupportedLanguage } from './GlobalStateStore.types';
3
+ type TranslationMap = Record<SupportedLanguage, string> & Record<string, string>;
4
+ type TranslationDictionary = Record<string, TranslationMap>;
5
+ /** Subscribes to an external language store and keeps the internal language in sync. Call once at app initialisation. */
6
+ declare const initTranslation: (langStore: Readable<SupportedLanguage>) => void;
7
+ /** Merges entries into the global dictionary. Global translations persist for the lifetime of the app. */
8
+ declare const loadTranslations: (dict: TranslationDictionary) => void;
9
+ /** Clears all global translations. */
10
+ declare const clearTranslations: () => void;
11
+ /** Replaces the page dictionary entirely. Call on page navigation to swap in translations for the new page. */
12
+ declare const loadPageTranslations: (dict: TranslationDictionary) => void;
13
+ /** Merges entries into the global dictionary. Useful for incrementally adding global translations without a full reload. */
14
+ declare const appendTranslations: (dict: TranslationDictionary) => void;
15
+ /** Merges entries into the current page dictionary without replacing it. Useful when a page loads translations in multiple steps. */
16
+ declare const appendPageTranslations: (dict: TranslationDictionary) => void;
17
+ /** Clears all page translations. */
18
+ declare const clearPageTranslations: () => void;
19
+ /**
20
+ * Resolves a translation synchronously using the current language.
21
+ * Accepts a dictionary key or an inline `TranslationMap`.
22
+ * Page translations take priority over global translations.
23
+ * Falls back to `en`, then to the key itself if no match is found.
24
+ */
25
+ declare function _T(key: string): string;
26
+ declare function _T(map: TranslationMap): string;
27
+ /**
28
+ * Reactive Svelte store that exposes a translate function.
29
+ * Re-derives whenever the language or either dictionary changes.
30
+ * Accepts a dictionary key or an inline `TranslationMap`.
31
+ * Page translations take priority over global translations.
32
+ */
33
+ declare const t: Readable<(keyOrMap: string | TranslationMap) => string>;
34
+ export type { TranslationDictionary, TranslationMap };
35
+ export { _T, appendPageTranslations, appendTranslations, clearPageTranslations, clearTranslations, initTranslation, loadPageTranslations, loadTranslations, t };
@@ -0,0 +1,52 @@
1
+ import { derived, get, writable } from 'svelte/store';
2
+ const _langStore = writable('en');
3
+ const _globalDictStore = writable({});
4
+ const _pageDictStore = writable({});
5
+ /** Subscribes to an external language store and keeps the internal language in sync. Call once at app initialisation. */
6
+ const initTranslation = (langStore) => {
7
+ langStore.subscribe((lang) => _langStore.set(lang));
8
+ };
9
+ /** Merges entries into the global dictionary. Global translations persist for the lifetime of the app. */
10
+ const loadTranslations = (dict) => {
11
+ _globalDictStore.update((current) => ({ ...current, ...dict }));
12
+ };
13
+ /** Clears all global translations. */
14
+ const clearTranslations = () => {
15
+ _globalDictStore.set({});
16
+ };
17
+ /** Replaces the page dictionary entirely. Call on page navigation to swap in translations for the new page. */
18
+ const loadPageTranslations = (dict) => {
19
+ _pageDictStore.set(dict);
20
+ };
21
+ /** Merges entries into the global dictionary. Useful for incrementally adding global translations without a full reload. */
22
+ const appendTranslations = (dict) => {
23
+ _globalDictStore.update((current) => ({ ...current, ...dict }));
24
+ };
25
+ /** Merges entries into the current page dictionary without replacing it. Useful when a page loads translations in multiple steps. */
26
+ const appendPageTranslations = (dict) => {
27
+ _pageDictStore.update((current) => ({ ...current, ...dict }));
28
+ };
29
+ /** Clears all page translations. */
30
+ const clearPageTranslations = () => {
31
+ _pageDictStore.set({});
32
+ };
33
+ const _resolve = (lang, globalDict, pageDict, keyOrMap) => {
34
+ if (typeof keyOrMap === 'string') {
35
+ const map = pageDict[keyOrMap] ?? globalDict[keyOrMap];
36
+ if (!map)
37
+ return keyOrMap;
38
+ return map[lang] ?? map['en'] ?? keyOrMap;
39
+ }
40
+ return keyOrMap[lang] ?? keyOrMap['en'];
41
+ };
42
+ function _T(keyOrMap) {
43
+ return _resolve(get(_langStore), get(_globalDictStore), get(_pageDictStore), keyOrMap);
44
+ }
45
+ /**
46
+ * Reactive Svelte store that exposes a translate function.
47
+ * Re-derives whenever the language or either dictionary changes.
48
+ * Accepts a dictionary key or an inline `TranslationMap`.
49
+ * Page translations take priority over global translations.
50
+ */
51
+ const t = derived([_langStore, _globalDictStore, _pageDictStore], ([lang, globalDict, pageDict]) => (keyOrMap) => _resolve(lang, globalDict, pageDict, keyOrMap));
52
+ export { _T, appendPageTranslations, appendTranslations, clearPageTranslations, clearTranslations, initTranslation, loadPageTranslations, loadTranslations, t };
@@ -1,4 +1,5 @@
1
1
  import { derived, get, writable } from "svelte/store";
2
+ import { initTranslation } from "./GlobalStateStore.i18n";
2
3
  import { loadStorage, saveStorage } from "./GlobalStateStore.utils";
3
4
  const createInitialState = () => ({
4
5
  initialized: false,
@@ -32,6 +33,7 @@ const createInitialState = () => ({
32
33
  },
33
34
  user: {
34
35
  guid: "",
36
+ language: "en",
35
37
  username: "",
36
38
  },
37
39
  });
@@ -303,6 +305,9 @@ const createGlobalStateStore = () => {
303
305
  topBar: { ...state.layout.topBar, ...updates },
304
306
  },
305
307
  })),
308
+ setLanguage: (lang) => setGlobalState((state) => ({
309
+ user: { ...state.user, language: lang },
310
+ })),
306
311
  setUser: (updates) => setGlobalState((state) => ({
307
312
  user: { ...state.user, ...updates },
308
313
  })),
@@ -352,6 +357,8 @@ const GlobalStateStore = createGlobalStateStore();
352
357
  const GlobalStateStoreReadable = {
353
358
  subscribe: GlobalStateStore.subscribe,
354
359
  };
360
+ const languageStore = derived(GlobalStateStoreReadable, (state) => state.user.language ?? "en");
361
+ initTranslation(languageStore);
355
362
  function useGlobalStateStore(selector) {
356
363
  if (!selector) {
357
364
  return GlobalStateStoreReadable;
@@ -376,4 +383,7 @@ const setAuthToken = (token) => {
376
383
  const GetGlobalState = () => {
377
384
  return GlobalStateStore.getState();
378
385
  };
379
- export { getApiURL, getAuthToken, GetGlobalState, GlobalStateStore, isLoggedIn, setApiURL, setAuthToken, useGlobalStateStore, };
386
+ const setLanguage = (lang) => {
387
+ GlobalStateStore.getState().setLanguage(lang);
388
+ };
389
+ export { getApiURL, getAuthToken, GetGlobalState, GlobalStateStore, isLoggedIn, languageStore, setApiURL, setAuthToken, setLanguage, useGlobalStateStore, };
@@ -41,6 +41,7 @@ interface GlobalStateActions {
41
41
  setRightBar: (updates: Partial<BarState>) => void;
42
42
  setSession: (updates: Partial<SessionState>) => void;
43
43
  setTopBar: (updates: Partial<BarState>) => void;
44
+ setLanguage: (lang: SupportedLanguage) => void;
44
45
  setUser: (updates: Partial<UserState>) => void;
45
46
  }
46
47
  interface GlobalStateStoreType extends GlobalState, GlobalStateActions {
@@ -107,6 +108,7 @@ interface SessionState {
107
108
  meta?: Record<string, any>;
108
109
  parameters?: Record<string, any>;
109
110
  }
111
+ type SupportedLanguage = 'af' | 'en';
110
112
  interface ThemeSettings {
111
113
  darkMode?: boolean;
112
114
  name?: string;
@@ -117,6 +119,7 @@ interface UserState {
117
119
  fullNames?: string;
118
120
  guid?: string;
119
121
  isAdmin?: boolean;
122
+ language?: SupportedLanguage;
120
123
  noticeMsg?: string;
121
124
  parameters?: Record<string, any>;
122
125
  rid?: number;
@@ -124,4 +127,4 @@ interface UserState {
124
127
  username: string;
125
128
  }
126
129
  type PersistedGlobalState = Pick<GlobalState, 'layout' | 'navigation' | 'owner' | 'program' | 'session' | 'user'>;
127
- export type { BarState, GlobalState, GlobalStateActions, GlobalStateStoreType, LayoutState, MenuItem, NavigationState, OwnerState, PageInfo, PersistedGlobalState, ProgramState, SessionState, ThemeSettings, UserState };
130
+ export type { BarState, GlobalState, GlobalStateActions, GlobalStateStoreType, LayoutState, MenuItem, NavigationState, OwnerState, PageInfo, PersistedGlobalState, ProgramState, SessionState, SupportedLanguage, ThemeSettings, UserState };
@@ -1,4 +1,6 @@
1
1
  export * from "./GlobalStateStore";
2
+ export { _T, appendPageTranslations, appendTranslations, clearPageTranslations, clearTranslations, loadPageTranslations, loadTranslations, t } from "./GlobalStateStore.i18n";
3
+ export type { TranslationDictionary, TranslationMap } from "./GlobalStateStore.i18n";
2
4
  export type * from "./GlobalStateStore.types";
3
5
  export { default as GlobalStateStoreProvider } from "./GlobalStateStoreProvider.svelte";
4
6
  export { useGlobalStateStoreContext } from "./GlobalStateStoreContext";
@@ -1,3 +1,4 @@
1
1
  export * from "./GlobalStateStore";
2
+ export { _T, appendPageTranslations, appendTranslations, clearPageTranslations, clearTranslations, loadPageTranslations, loadTranslations, t } from "./GlobalStateStore.i18n";
2
3
  export { default as GlobalStateStoreProvider } from "./GlobalStateStoreProvider.svelte";
3
4
  export { useGlobalStateStoreContext } from "./GlobalStateStoreContext";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warkypublic/svelix",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "Svelte 5 component library with Skeleton UI and Tailwind CSS",
5
5
  "license": "Apache-2.0",
6
6
  "exports": {