@dragcraft/i18n 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT hackycy <https://github.com/hackycy>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ import { InjectionKey, Ref } from "vue";
2
+ //#region src/index.d.ts
3
+ interface MessageTree {
4
+ [key: string]: string | MessageTree;
5
+ }
6
+ type LocaleMessages = Record<string, MessageTree>;
7
+ type FlatMessages = Record<string, string>;
8
+ interface I18nInstance {
9
+ locale: Ref<string>;
10
+ t: (key: string, fallback?: string) => string;
11
+ setLocale: (locale: string) => void;
12
+ mergeMessages: (locale: string, msgs: MessageTree) => void;
13
+ }
14
+ declare const I18N_KEY: InjectionKey<I18nInstance>;
15
+ declare function createI18n(defaultLocale: string, defaultMessages?: LocaleMessages): I18nInstance;
16
+ declare function useI18n(): I18nInstance;
17
+ //#endregion
18
+ export { FlatMessages, I18N_KEY, I18nInstance, LocaleMessages, MessageTree, createI18n, useI18n };
package/dist/index.mjs ADDED
@@ -0,0 +1,47 @@
1
+ import { inject, ref } from "vue";
2
+ //#region src/index.ts
3
+ const I18N_KEY = Symbol("dc-i18n");
4
+ function flatten(tree, prefix = "") {
5
+ const result = {};
6
+ for (const [key, value] of Object.entries(tree)) {
7
+ const path = prefix ? `${prefix}.${key}` : key;
8
+ if (typeof value === "string") result[path] = value;
9
+ else Object.assign(result, flatten(value, path));
10
+ }
11
+ return result;
12
+ }
13
+ function createI18n(defaultLocale, defaultMessages = {}) {
14
+ const locale = ref(defaultLocale);
15
+ const flatStore = {};
16
+ for (const [currentLocale, tree] of Object.entries(defaultMessages)) flatStore[currentLocale] = flatten(tree);
17
+ function t(key, fallback) {
18
+ return flatStore[locale.value]?.[key] ?? fallback ?? key;
19
+ }
20
+ function setLocale(nextLocale) {
21
+ locale.value = nextLocale;
22
+ }
23
+ function mergeMessages(targetLocale, tree) {
24
+ flatStore[targetLocale] = {
25
+ ...flatStore[targetLocale],
26
+ ...flatten(tree)
27
+ };
28
+ }
29
+ return {
30
+ locale,
31
+ t,
32
+ setLocale,
33
+ mergeMessages
34
+ };
35
+ }
36
+ function useI18n() {
37
+ const i18n = inject(I18N_KEY);
38
+ if (i18n) return i18n;
39
+ return {
40
+ locale: ref("zh-CN"),
41
+ t: (key, fallback) => fallback ?? key,
42
+ setLocale: () => {},
43
+ mergeMessages: () => {}
44
+ };
45
+ }
46
+ //#endregion
47
+ export { I18N_KEY, createI18n, useI18n };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@dragcraft/i18n",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "Reactive internationalization context for @dragcraft UI packages",
6
+ "author": "hackycy <hackycy@outlook.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/hackycy/dragcraft#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/hackycy/dragcraft.git"
12
+ },
13
+ "bugs": "https://github.com/hackycy/dragcraft/issues",
14
+ "keywords": [],
15
+ "sideEffects": false,
16
+ "exports": {
17
+ ".": "./dist/index.mjs",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "main": "./dist/index.mjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.mts",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "peerDependencies": {
27
+ "vue": ">=3.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "vitest": "^4.0.7",
31
+ "vue": "^3.5.27"
32
+ },
33
+ "scripts": {
34
+ "build": "tsdown",
35
+ "dev": "tsdown --watch",
36
+ "lint": "eslint",
37
+ "release": "bumpp",
38
+ "test": "vitest",
39
+ "typecheck": "tsc"
40
+ }
41
+ }