@streamscloud/kit 0.1.3 → 0.1.5

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.
@@ -0,0 +1,16 @@
1
+ import { type AppLocaleValue } from './types';
2
+ declare class AppLocaleContainer {
3
+ private _current;
4
+ get current(): AppLocaleValue;
5
+ get availableLocales(): readonly AppLocaleValue[];
6
+ init: (serverLocale?: AppLocaleValue) => void;
7
+ /**
8
+ * Change locale.
9
+ * In the browser: persists to cookie and reloads the page (wuchale translations are compile-time).
10
+ * On the server / in non-browser contexts: updates the reactive state directly.
11
+ */
12
+ change: (newLocale: AppLocaleValue) => void;
13
+ toggle: () => void;
14
+ }
15
+ export declare const AppLocale: AppLocaleContainer;
16
+ export {};
@@ -0,0 +1,36 @@
1
+ import { isBrowser } from '../utils';
2
+ import { LocaleCookie } from './locale-cookie';
3
+ import { APP_LOCALE_DEFAULT, APP_LOCALE_VALUES } from './types';
4
+ class AppLocaleContainer {
5
+ _current = $state(APP_LOCALE_DEFAULT);
6
+ get current() {
7
+ return this._current;
8
+ }
9
+ get availableLocales() {
10
+ return APP_LOCALE_VALUES;
11
+ }
12
+ init = (serverLocale) => {
13
+ this._current = serverLocale ?? LocaleCookie.get() ?? APP_LOCALE_DEFAULT;
14
+ };
15
+ /**
16
+ * Change locale.
17
+ * In the browser: persists to cookie and reloads the page (wuchale translations are compile-time).
18
+ * On the server / in non-browser contexts: updates the reactive state directly.
19
+ */
20
+ change = (newLocale) => {
21
+ if (newLocale === this._current) {
22
+ return;
23
+ }
24
+ if (!isBrowser()) {
25
+ this._current = newLocale;
26
+ return;
27
+ }
28
+ LocaleCookie.set(newLocale);
29
+ window.location.reload();
30
+ };
31
+ toggle = () => {
32
+ const currentIndex = APP_LOCALE_VALUES.indexOf(this._current);
33
+ this.change(APP_LOCALE_VALUES[(currentIndex + 1) % APP_LOCALE_VALUES.length]);
34
+ };
35
+ }
36
+ export const AppLocale = new AppLocaleContainer();
@@ -0,0 +1,4 @@
1
+ export { AppLocale } from './app-locale.svelte';
2
+ export { LocaleCookie } from './locale-cookie';
3
+ export { APP_LOCALE_COOKIE, APP_LOCALE_DEFAULT, APP_LOCALE_VALUES, getLocaleFromAcceptLanguage, isValidLocale } from './types';
4
+ export type { AppLocaleValue, LocalizationSchema } from './types';
@@ -0,0 +1,3 @@
1
+ export { AppLocale } from './app-locale.svelte';
2
+ export { LocaleCookie } from './locale-cookie';
3
+ export { APP_LOCALE_COOKIE, APP_LOCALE_DEFAULT, APP_LOCALE_VALUES, getLocaleFromAcceptLanguage, isValidLocale } from './types';
@@ -0,0 +1,5 @@
1
+ import { type AppLocaleValue } from './types';
2
+ export declare const LocaleCookie: {
3
+ get: () => AppLocaleValue | null;
4
+ set: (value: AppLocaleValue) => void;
5
+ };
@@ -0,0 +1,19 @@
1
+ import { isBrowser } from '../utils';
2
+ import { APP_LOCALE_COOKIE, APP_LOCALE_VALUES } from './types';
3
+ const COOKIE_MAX_AGE = 60 * 60 * 24 * 365; // 1 year
4
+ export const LocaleCookie = {
5
+ get: () => {
6
+ if (!isBrowser()) {
7
+ return null;
8
+ }
9
+ const match = document.cookie.match(new RegExp(`${APP_LOCALE_COOKIE}=([^;]+)`));
10
+ const value = match?.[1];
11
+ return APP_LOCALE_VALUES.find((locale) => locale === value) ?? null;
12
+ },
13
+ set: (value) => {
14
+ if (!isBrowser()) {
15
+ return;
16
+ }
17
+ document.cookie = `${APP_LOCALE_COOKIE}=${value};path=/;max-age=${COOKIE_MAX_AGE};SameSite=Lax`;
18
+ }
19
+ };
@@ -0,0 +1,9 @@
1
+ export declare const APP_LOCALE_COOKIE = "app-locale";
2
+ export declare const APP_LOCALE_VALUES: readonly ["en", "no"];
3
+ export declare const APP_LOCALE_DEFAULT: "en";
4
+ export type AppLocaleValue = (typeof APP_LOCALE_VALUES)[number];
5
+ export type LocalizationSchema = {
6
+ [key: string]: Record<AppLocaleValue, unknown>;
7
+ };
8
+ export declare const isValidLocale: (value: string | undefined) => value is AppLocaleValue;
9
+ export declare const getLocaleFromAcceptLanguage: (header: string | null) => AppLocaleValue | null;
@@ -0,0 +1,25 @@
1
+ export const APP_LOCALE_COOKIE = 'app-locale';
2
+ export const APP_LOCALE_VALUES = ['en', 'no'];
3
+ export const APP_LOCALE_DEFAULT = 'en';
4
+ export const isValidLocale = (value) => {
5
+ return APP_LOCALE_VALUES.some((locale) => locale === value);
6
+ };
7
+ export const getLocaleFromAcceptLanguage = (header) => {
8
+ if (!header) {
9
+ return null;
10
+ }
11
+ // Parse Accept-Language: en-US,en;q=0.9,no;q=0.8
12
+ const languages = header.split(',').map((lang) => lang.split(';')[0].trim());
13
+ for (const lang of languages) {
14
+ const found = APP_LOCALE_VALUES.find((locale) => locale === lang);
15
+ if (found) {
16
+ return found;
17
+ }
18
+ const base = lang.split('-')[0];
19
+ const baseFound = APP_LOCALE_VALUES.find((locale) => locale === base);
20
+ if (baseFound) {
21
+ return baseFound;
22
+ }
23
+ }
24
+ return null;
25
+ };
@@ -2,25 +2,22 @@ import { ToasterHost } from './toaster-host.svelte';
2
2
  export class Toastr {
3
3
  static async error(message, options) {
4
4
  const toast = await ToasterHost.ensure();
5
- toast.error(message, patchOptions(options));
5
+ toast.error(message, options);
6
6
  }
7
7
  static async success(message, options) {
8
8
  const toast = await ToasterHost.ensure();
9
- toast.success(message, patchOptions(options));
9
+ toast.success(message, options);
10
10
  }
11
11
  static async warning(message, options) {
12
12
  const toast = await ToasterHost.ensure();
13
- toast.warning(message, patchOptions(options));
13
+ toast.warning(message, options);
14
14
  }
15
15
  static async info(message, options) {
16
16
  const toast = await ToasterHost.ensure();
17
- toast.info(message, patchOptions(options));
17
+ toast.info(message, options);
18
18
  }
19
19
  static async promise(promise, messages) {
20
20
  const toast = await ToasterHost.ensure();
21
21
  toast.promise(promise, messages);
22
22
  }
23
23
  }
24
- const patchOptions = (options) => {
25
- return { ...options, duration: 100000 };
26
- };
@@ -1,8 +1,8 @@
1
- import { type Locale } from '../../locale';
1
+ import { type AppLocaleValue } from '../locale';
2
2
  type CompactOptions = {
3
3
  decimals?: number;
4
4
  trimZeros?: boolean;
5
- locale?: Locale;
5
+ locale?: AppLocaleValue;
6
6
  };
7
7
  export declare const compactNumber: (value: number, options?: CompactOptions) => string;
8
8
  export {};
@@ -1,10 +1,10 @@
1
- import { getLocale } from '../../locale';
1
+ import { AppLocale } from '../locale';
2
2
  const THOUSAND = 1_000;
3
3
  const MILLION = 1_000_000;
4
4
  const BILLION = 1_000_000_000;
5
5
  const TRILLION = 1_000_000_000_000;
6
6
  export const compactNumber = (value, options = {}) => {
7
- const { decimals = 1, trimZeros = true, locale = getLocale() } = options;
7
+ const { decimals = 1, trimZeros = true, locale = AppLocale.current } = options;
8
8
  const abs = Math.abs(value);
9
9
  const formatter = makeNumberFormatter(locale, decimals, trimZeros);
10
10
  if (abs >= TRILLION) {
@@ -1,25 +1,25 @@
1
- import { getLocale } from '../../../locale';
1
+ import { AppLocale } from '../../locale';
2
2
  export class ValidationLocalization {
3
3
  get required() {
4
- return loc.required[getLocale()];
4
+ return loc.required[AppLocale.current];
5
5
  }
6
6
  get email() {
7
- return loc.email[getLocale()];
7
+ return loc.email[AppLocale.current];
8
8
  }
9
9
  get minLength() {
10
- return loc.minLength[getLocale()];
10
+ return loc.minLength[AppLocale.current];
11
11
  }
12
12
  get maxLength() {
13
- return loc.maxLength[getLocale()];
13
+ return loc.maxLength[AppLocale.current];
14
14
  }
15
15
  get min() {
16
- return loc.min[getLocale()];
16
+ return loc.min[AppLocale.current];
17
17
  }
18
18
  get max() {
19
- return loc.max[getLocale()];
19
+ return loc.max[AppLocale.current];
20
20
  }
21
21
  get badFormat() {
22
- return loc.badFormat[getLocale()];
22
+ return loc.badFormat[AppLocale.current];
23
23
  }
24
24
  }
25
25
  const loc = {
@@ -1,10 +1,10 @@
1
- import { getLocale } from '../../locale';
1
+ import { AppLocale } from '../../core/locale';
2
2
  export class LineClampLocalization {
3
3
  get showLess() {
4
- return loc.showLess[getLocale()];
4
+ return loc.showLess[AppLocale.current];
5
5
  }
6
6
  get showMore() {
7
- return loc.showMore[getLocale()];
7
+ return loc.showMore[AppLocale.current];
8
8
  }
9
9
  }
10
10
  const loc = {
@@ -1,7 +1,7 @@
1
- import { getLocale } from '../../../locale';
1
+ import { AppLocale } from '../../../core/locale';
2
2
  export class CarouselLocalization {
3
3
  get nOfM() {
4
- return loc.nOfM[getLocale()];
4
+ return loc.nOfM[AppLocale.current];
5
5
  }
6
6
  }
7
7
  const loc = {
@@ -1,7 +1,7 @@
1
- import { getLocale } from '../../locale';
1
+ import { AppLocale } from '../../core/locale';
2
2
  export class SwipeIndicatorLocalization {
3
3
  get swipe() {
4
- return loc.swipe[getLocale()];
4
+ return loc.swipe[AppLocale.current];
5
5
  }
6
6
  }
7
7
  const loc = {
@@ -1,28 +1,28 @@
1
- import { getLocale } from '../../locale';
1
+ import { AppLocale } from '../../core/locale';
2
2
  export class TimeAgoLocalization {
3
3
  get locale() {
4
- return loc.locale[getLocale()];
4
+ return loc.locale[AppLocale.current];
5
5
  }
6
6
  get aMinuteAgo() {
7
- return loc.aMinuteAgo[getLocale()];
7
+ return loc.aMinuteAgo[AppLocale.current];
8
8
  }
9
9
  get anHourAgo() {
10
- return loc.anHourAgo[getLocale()];
10
+ return loc.anHourAgo[AppLocale.current];
11
11
  }
12
12
  get justNow() {
13
- return loc.justNow[getLocale()];
13
+ return loc.justNow[AppLocale.current];
14
14
  }
15
15
  get at() {
16
- return loc.at[getLocale()];
16
+ return loc.at[AppLocale.current];
17
17
  }
18
18
  get hoursAgo() {
19
- return loc.hoursAgo[getLocale()];
19
+ return loc.hoursAgo[AppLocale.current];
20
20
  }
21
21
  get minutesAgo() {
22
- return loc.minutesAgo[getLocale()];
22
+ return loc.minutesAgo[AppLocale.current];
23
23
  }
24
24
  get yesterday() {
25
- return loc.yesterday[getLocale()];
25
+ return loc.yesterday[AppLocale.current];
26
26
  }
27
27
  }
28
28
  const loc = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/kit",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "author": "StreamsCloud",
5
5
  "repository": {
6
6
  "type": "git",
@@ -52,6 +52,10 @@
52
52
  "types": "./dist/core/i18n/index.d.ts",
53
53
  "svelte": "./dist/core/i18n/index.js"
54
54
  },
55
+ "./core/locale": {
56
+ "types": "./dist/core/locale/index.d.ts",
57
+ "svelte": "./dist/core/locale/index.js"
58
+ },
55
59
  "./core/media": {
56
60
  "types": "./dist/core/media/index.d.ts",
57
61
  "svelte": "./dist/core/media/index.js"
@@ -80,10 +84,6 @@
80
84
  "types": "./dist/core/validation/index.d.ts",
81
85
  "svelte": "./dist/core/validation/index.js"
82
86
  },
83
- "./locale": {
84
- "types": "./dist/locale/index.d.ts",
85
- "svelte": "./dist/locale/index.js"
86
- },
87
87
  "./ui/button": {
88
88
  "types": "./dist/ui/button/index.d.ts",
89
89
  "svelte": "./dist/ui/button/index.js"
@@ -1 +0,0 @@
1
- export { getLocale, type Locale, type LocalizationSchema, setLocale } from './locale.svelte';
@@ -1 +0,0 @@
1
- export { getLocale, setLocale } from './locale.svelte';
@@ -1,6 +0,0 @@
1
- export type Locale = 'en' | 'no';
2
- export type LocalizationSchema = {
3
- [key: string]: Record<Locale, unknown>;
4
- };
5
- export declare const getLocale: () => Locale;
6
- export declare const setLocale: (value: Locale) => void;
@@ -1,5 +0,0 @@
1
- let locale = $state('en');
2
- export const getLocale = () => locale;
3
- export const setLocale = (value) => {
4
- locale = value;
5
- };