@hyvor/design 0.0.53 → 0.0.55

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.
@@ -2,7 +2,10 @@
2
2
  import { InternationalizationService } from "./i18n.js";
3
3
  export let languages;
4
4
  const i18n = new InternationalizationService(languages);
5
+ const locale = i18n.locale;
5
6
  setContext("i18n", i18n);
6
7
  </script>
7
8
 
8
- <slot />
9
+ {#key $locale}
10
+ <slot />
11
+ {/key}
@@ -2,6 +2,7 @@
2
2
  import { getStringByKey, InternationalizationService } from "./i18n.js";
3
3
  import { IntlMessageFormat } from "intl-messageformat";
4
4
  import { browser } from "$app/environment";
5
+ import { getMessage as getMessageBase } from "./t.js";
5
6
  export let key;
6
7
  export let params = {};
7
8
  let hasComponentParams = false;
@@ -9,12 +10,20 @@ function getParamsForBackend() {
9
10
  let retParams = {};
10
11
  for (let [key2, value] of Object.entries(params)) {
11
12
  let newValue;
12
- if (typeof value === "object" && value !== null && value.hasOwnProperty("component")) {
13
- newValue = (chunks) => {
14
- const children = typeof chunks === "string" ? chunks : chunks.join("");
15
- return children;
16
- };
17
- hasComponentParams = true;
13
+ if (typeof value === "object" && value !== null) {
14
+ if (value.hasOwnProperty("component")) {
15
+ newValue = (chunks) => {
16
+ const children = typeof chunks === "string" ? chunks : chunks.join("");
17
+ return children;
18
+ };
19
+ hasComponentParams = true;
20
+ } else if (value.hasOwnProperty("element")) {
21
+ newValue = (chunks) => {
22
+ const children = typeof chunks === "string" ? chunks : chunks.join("");
23
+ const el = value.element;
24
+ return `<${el}>${children}</${el}>`;
25
+ };
26
+ }
18
27
  } else {
19
28
  newValue = value;
20
29
  }
@@ -53,12 +62,12 @@ const locale = i18n.locale;
53
62
  const strings = i18n.strings;
54
63
  let message = getMessage(getParamsForBackend());
55
64
  function getMessage(processedParams) {
56
- const string = getStringByKey($strings, key);
57
- if (string) {
58
- const formatter = new IntlMessageFormat(string, $locale);
59
- return formatter.format(processedParams);
60
- }
61
- return "";
65
+ return getMessageBase(
66
+ key,
67
+ processedParams,
68
+ $strings,
69
+ $locale
70
+ );
62
71
  }
63
72
  function bindComponents() {
64
73
  for (let [id, binding] of componentBindings) {
@@ -81,7 +90,7 @@ async function renderFrontend() {
81
90
  }
82
91
  let mounted = false;
83
92
  $: {
84
- $locale, $strings, params;
93
+ params;
85
94
  if (browser && mounted) {
86
95
  renderFrontend();
87
96
  }
@@ -4,10 +4,11 @@ import { type PrimitiveType } from 'intl-messageformat';
4
4
  declare const __propDef: {
5
5
  props: {
6
6
  key: string;
7
- params?: Record<string, {
8
- component: ComponentType;
7
+ params?: Record<string, PrimitiveType | {
8
+ element?: string | undefined;
9
+ component?: ComponentType | undefined;
9
10
  props?: Record<string, any> | undefined;
10
- } | PrimitiveType> | undefined;
11
+ }> | undefined;
11
12
  };
12
13
  events: {
13
14
  [evt: string]: CustomEvent<any>;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="svelte" />
2
+ import type { PrimitiveType } from "intl-messageformat";
2
3
  import { type Readable, type Writable } from "svelte/store";
3
4
  export type i18nLoaderType = () => Promise<any>;
4
5
  interface LanguageBase {
@@ -28,6 +29,7 @@ export declare class InternationalizationService {
28
29
  register(language: Language): void;
29
30
  found(code: string): boolean;
30
31
  languageByCode(code: string): LanguageWithLoader | undefined;
32
+ t(key: string, params?: Record<string, PrimitiveType>): string;
31
33
  }
32
34
  export declare function getStringByKey(messages: Record<string, any>, key: string): string | undefined;
33
35
  export {};
@@ -1,5 +1,6 @@
1
1
  import { deepmerge } from "deepmerge-ts";
2
2
  import { writable, derived } from "svelte/store";
3
+ import { t } from "./t.js";
3
4
  export class InternationalizationService {
4
5
  languages = [];
5
6
  locale;
@@ -32,14 +33,15 @@ export class InternationalizationService {
32
33
  this.strings.set(merged);
33
34
  }
34
35
  setLocale(code) {
35
- this.locale.set(code);
36
36
  if (this.stringsCache.has(code)) {
37
37
  this.setStrings(code);
38
+ this.locale.set(code);
38
39
  }
39
40
  else {
40
41
  this.languageByCode(code)?.loader().then(({ default: strings }) => {
41
42
  this.stringsCache.set(code, strings);
42
43
  this.setStrings(code);
44
+ this.locale.set(code);
43
45
  });
44
46
  }
45
47
  }
@@ -57,6 +59,9 @@ export class InternationalizationService {
57
59
  languageByCode(code) {
58
60
  return this.languages.find(l => l.code === code);
59
61
  }
62
+ t(key, params = {}) {
63
+ return t(key, params, this);
64
+ }
60
65
  }
61
66
  export function getStringByKey(messages, key) {
62
67
  const keys = key.split('.');
@@ -0,0 +1,8 @@
1
+ import { type PrimitiveType } from "intl-messageformat";
2
+ import { type InternationalizationService } from "./i18n.js";
3
+ type ParamValue = PrimitiveType | ((chunks: string | string[]) => string);
4
+ type ParamsType = Record<string, ParamValue>;
5
+ export declare function getMessage(key: string, params: ParamsType, $strings: Record<string, any>, $locale: string): string;
6
+ export type TParams = Record<string, PrimitiveType>;
7
+ export declare function t(key: string, params?: Record<string, PrimitiveType>, i18n?: InternationalizationService | null): string;
8
+ export {};
@@ -0,0 +1,18 @@
1
+ import { IntlMessageFormat } from "intl-messageformat";
2
+ import { getContext } from "svelte";
3
+ import { get } from "svelte/store";
4
+ import { getStringByKey } from "./i18n.js";
5
+ export function getMessage(key, params, $strings, $locale) {
6
+ const string = getStringByKey($strings, key);
7
+ if (string) {
8
+ const formatter = new IntlMessageFormat(string, $locale);
9
+ return formatter.format(params);
10
+ }
11
+ return '';
12
+ }
13
+ export function t(key, params = {}, i18n = null) {
14
+ i18n = i18n || getContext('i18n');
15
+ const $locale = get(i18n.locale);
16
+ const $strings = get(i18n.strings);
17
+ return getMessage(key, params, $strings, $locale);
18
+ }
@@ -45,3 +45,4 @@ export { default as InternationalizationProvider } from './Internationalization/
45
45
  export { default as LanguageToggle } from './Internationalization/LanguageToggle.svelte';
46
46
  export { default as T } from './Internationalization/T.svelte';
47
47
  export { type Language as InternationalizationLanguage, InternationalizationService } from './Internationalization/i18n.js';
48
+ export { t } from './Internationalization/t.js';
@@ -47,3 +47,4 @@ export { default as InternationalizationProvider } from './Internationalization/
47
47
  export { default as LanguageToggle } from './Internationalization/LanguageToggle.svelte';
48
48
  export { default as T } from './Internationalization/T.svelte';
49
49
  export { InternationalizationService } from './Internationalization/i18n.js';
50
+ export { t } from './Internationalization/t.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyvor/design",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "scripts": {