svelte-component-i18n 0.1.0 → 0.1.1
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,16 +2,19 @@ import { Dictionary, type Texts } from './dictionary.ts';
|
|
|
2
2
|
import type { LanguageHook, LanguageStrategy } from './detect.ts';
|
|
3
3
|
type MustStringUnion<T extends string> = string extends T ? never : T;
|
|
4
4
|
export declare class Translator<Languages extends string, Fallback extends Languages> {
|
|
5
|
+
#private;
|
|
5
6
|
readonly supportedLanguages: Languages[];
|
|
6
7
|
readonly fallbackLanguage: Fallback;
|
|
7
|
-
currentLanguage: Languages;
|
|
8
8
|
private readonly locale;
|
|
9
|
+
private readonly languageHooks;
|
|
9
10
|
constructor({ supportedLanguages, fallbackLanguage, languageStrategies, languageHooks, }: {
|
|
10
11
|
supportedLanguages: MustStringUnion<Languages>[];
|
|
11
12
|
fallbackLanguage: Fallback;
|
|
12
13
|
languageStrategies?: LanguageStrategy<Languages>[];
|
|
13
14
|
languageHooks?: LanguageHook<Languages>[];
|
|
14
15
|
});
|
|
16
|
+
get currentLanguage(): Languages;
|
|
17
|
+
set currentLanguage(language: Languages);
|
|
15
18
|
define<T>(texts: Texts<Languages, Fallback, T>): Dictionary<Languages, Fallback, T>;
|
|
16
19
|
}
|
|
17
20
|
export {};
|
|
@@ -2,24 +2,30 @@ import { Dictionary } from "./dictionary.js";
|
|
|
2
2
|
export class Translator {
|
|
3
3
|
supportedLanguages;
|
|
4
4
|
fallbackLanguage;
|
|
5
|
-
currentLanguage;
|
|
5
|
+
#currentLanguage;
|
|
6
6
|
locale;
|
|
7
|
+
languageHooks;
|
|
7
8
|
constructor({ supportedLanguages, fallbackLanguage, languageStrategies = [], languageHooks = [], }) {
|
|
8
9
|
checkSupportedLanguages(supportedLanguages);
|
|
9
10
|
this.supportedLanguages = supportedLanguages;
|
|
10
11
|
this.fallbackLanguage = fallbackLanguage;
|
|
11
12
|
const initialLanguage = determineInitialLanguage(languageStrategies, supportedLanguages, fallbackLanguage);
|
|
12
|
-
this
|
|
13
|
+
this.#currentLanguage = $state(initialLanguage);
|
|
13
14
|
this.locale = $derived({
|
|
14
|
-
language: this
|
|
15
|
+
language: this.#currentLanguage,
|
|
15
16
|
fallback: this.fallbackLanguage,
|
|
16
17
|
pluralRules: new Intl.PluralRules(this.currentLanguage),
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
this.languageHooks = languageHooks;
|
|
20
|
+
}
|
|
21
|
+
get currentLanguage() {
|
|
22
|
+
return this.#currentLanguage;
|
|
23
|
+
}
|
|
24
|
+
set currentLanguage(language) {
|
|
25
|
+
this.#currentLanguage = language;
|
|
26
|
+
for (const hook of this.languageHooks) {
|
|
27
|
+
hook(this.currentLanguage);
|
|
28
|
+
}
|
|
23
29
|
}
|
|
24
30
|
define(texts) {
|
|
25
31
|
return new Dictionary(() => this.locale, texts);
|