wuchale 0.3.2 → 0.4.0

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,5 @@
1
+ type Composite = (number | string | Composite)[];
2
+ export declare function setTranslations(transArray: (string | Composite)[]): void;
3
+ export declare function getCtx(id: number): any;
4
+ export declare function wuchaleTrans(id: number, ...args: string[]): string;
5
+ export {};
@@ -0,0 +1,32 @@
1
+ let translations = $state({});
2
+ export function setTranslations(transArray) {
3
+ translations = transArray;
4
+ }
5
+ export function getCtx(id) {
6
+ const ctx = translations[id];
7
+ if (typeof ctx === 'string') {
8
+ return [ctx];
9
+ }
10
+ if (ctx == null || typeof ctx === 'number') {
11
+ return [`[i18n-404:${id}(${ctx})]`];
12
+ }
13
+ return ctx;
14
+ }
15
+ export function wuchaleTrans(id, ...args) {
16
+ const ctx = getCtx(id);
17
+ let txt = '';
18
+ for (const fragment of ctx) {
19
+ if (typeof fragment === 'string') {
20
+ txt += fragment;
21
+ }
22
+ else if (typeof fragment === 'number') { // index of non-text children
23
+ txt += args[fragment];
24
+ }
25
+ else {
26
+ // shouldn't happen
27
+ console.error('Unknown item in compiled catalog: ', id, fragment);
28
+ }
29
+ }
30
+ return txt;
31
+ }
32
+ //# sourceMappingURL=runtime.svelte.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.svelte.js","sourceRoot":"","sources":["../src/runtime.svelte.ts"],"names":[],"mappings":"AAAA,IAAI,YAAY,GAAG,MAAM,CAAC,EAAE,CAAC,CAAA;AAI7B,MAAM,UAAU,eAAe,CAAC,UAAkC;IAC9D,YAAY,GAAG,UAAU,CAAA;AAC7B,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,EAAU;IAC7B,MAAM,GAAG,GAAG,YAAY,CAAC,EAAE,CAAC,CAAA;IAC5B,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC1B,OAAO,CAAC,GAAG,CAAC,CAAA;IAChB,CAAC;IACD,IAAI,GAAG,IAAI,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QACzC,OAAO,CAAC,aAAa,EAAE,IAAI,GAAG,IAAI,CAAC,CAAA;IACvC,CAAC;IACD,OAAO,GAAG,CAAA;AACd,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,EAAU,EAAE,GAAG,IAAc;IACtD,MAAM,GAAG,GAAG,MAAM,CAAC,EAAE,CAAC,CAAA;IACtB,IAAI,GAAG,GAAG,EAAE,CAAA;IACZ,KAAK,MAAM,QAAQ,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC/B,GAAG,IAAI,QAAQ,CAAA;QACnB,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC,CAAC,6BAA6B;YACpE,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAA;QACzB,CAAC;aAAM,CAAC;YACJ,mBAAmB;YACnB,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAA;QACrE,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAA;AACd,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "wuchale",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "description": "i18n for svelte without turning your codebase upside down",
5
- "main": "index.js",
6
5
  "scripts": {
6
+ "build": "tsc",
7
7
  "test": "node tests"
8
8
  },
9
9
  "keywords": [
@@ -12,20 +12,32 @@
12
12
  "i18n"
13
13
  ],
14
14
  "files": [
15
- "index.js",
16
- "preprocess/",
17
- "runtime.svelte"
15
+ "dist",
16
+ "runtime.svelte"
18
17
  ],
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./runtime.svelte.js": {
23
+ "import": "./dist/runtime.svelte.js",
24
+ "types": "./dist/runtime.svelte.d.ts"
25
+ },
26
+ "./runtime.svelte": "./runtime.svelte"
27
+ },
28
+ "types": "./dist/index.d.ts",
19
29
  "homepage": "https://github.com/K1DV5/wuchale",
20
30
  "bugs": "https://github.com/K1DV5/wuchale/issues",
21
31
  "author": "K1DV5",
22
32
  "license": "MIT",
23
33
  "dependencies": {
34
+ "svelte": "^5.28.1",
24
35
  "magic-string": "^0.30.17",
25
36
  "pofile": "^1.1.4"
26
37
  },
27
38
  "devDependencies": {
28
- "svelte": "^5.28.1"
39
+ "@types/node": "^24.0.3",
40
+ "typescript": "^5.8.3"
29
41
  },
30
42
  "type": "module"
31
43
  }
package/runtime.svelte CHANGED
@@ -1,51 +1,5 @@
1
- <script module>
2
-
3
- let translations = $state({})
4
-
5
- /**
6
- * @param {object} transArray
7
- */
8
- export function setTranslations(transArray) {
9
- translations = transArray
10
- }
11
-
12
- /**
13
- * @param {number} id
14
- */
15
- function getCtx(id) {
16
- const ctx = translations[id]
17
- if (typeof ctx === 'string') {
18
- return [ctx]
19
- }
20
- if (ctx == null || typeof ctx === 'number') {
21
- return [`[i18n-404:${id}(${ctx})]`]
22
- }
23
- return ctx
24
- }
25
-
26
- /**
27
- * @param {number} id
28
- * @param {string[]} args
29
- */
30
- export function wuchaleTrans(id, ...args) {
31
- const ctx = getCtx(id)
32
- let txt = ''
33
- for (const fragment of ctx) {
34
- if (typeof fragment === 'string') {
35
- txt += fragment
36
- } else if (typeof fragment === 'number') { // index of non-text children
37
- txt += args[fragment]
38
- } else {
39
- // shouldn't happen
40
- console.error('Unknown item in compiled catalog: ', id, fragment)
41
- }
42
- }
43
- return txt
44
- }
45
-
46
- </script>
47
-
48
1
  <script>
2
+ import {getCtx} from './dist/runtime.svelte.js'
49
3
  const {id = null, ctx, tags, args} = $props()
50
4
  const finalCtx = $derived(id != null ? getCtx(id) : ctx)
51
5
  </script>
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- import plugin from "./preprocess/index.js"
2
-
3
- export const wuchale = plugin
@@ -1,52 +0,0 @@
1
- // $$ cd .. && npm run test
2
- // $$ node %f
3
-
4
- import { parse } from "svelte/compiler"
5
-
6
- /**
7
- * @param {{ type: string; data: any; name: string | any[]; children: any; expression: { value: any; }; }} ast
8
- */
9
- function walkCompileNodes(ast) {
10
- const parts = []
11
- if (ast.type === 'Text') {
12
- parts.push(ast.data)
13
- } else if (ast.type === 'InlineComponent') {
14
- const nodeIndex = Number(ast.name.slice(1))
15
- const subParts = [nodeIndex]
16
- for (const child of ast.children) {
17
- subParts.push(...walkCompileNodes(child))
18
- }
19
- parts.push(subParts)
20
- } else if (ast.type === 'MustacheTag') {
21
- parts.push(ast.expression.value)
22
- } else if (ast.type === 'Fragment') {
23
- for (const child of ast.children) {
24
- parts.push(...walkCompileNodes(child))
25
- }
26
- } else {
27
- console.log(ast)
28
- }
29
- return parts
30
- }
31
-
32
- /**
33
- * @param {string} text
34
- * @param {any} fallback
35
- */
36
- export default function compileTranslation(text, fallback) {
37
- if (!text) {
38
- return fallback
39
- }
40
- if (!text.includes('<') && !text.includes('{')) {
41
- return text
42
- }
43
- // <0></0> to <X0></X0> to please svelte parser
44
- text = text.replace(/(<|(<\/))(\d+)/g, '$1X$3')
45
- try {
46
- const ast = parse(text).html
47
- return walkCompileNodes(ast)
48
- } catch(err) {
49
- console.error(err)
50
- return fallback
51
- }
52
- }