svelte-component-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/README.md +58 -0
- package/dist/dictionary.d.ts +31 -0
- package/dist/dictionary.js +32 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/locale.d.ts +5 -0
- package/dist/locale.js +1 -0
- package/dist/translator.svelte.d.ts +9 -0
- package/dist/translator.svelte.js +28 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Svelte library
|
|
2
|
+
|
|
3
|
+
Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
|
|
4
|
+
|
|
5
|
+
Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
|
|
6
|
+
|
|
7
|
+
## Creating a project
|
|
8
|
+
|
|
9
|
+
If you're seeing this, you've probably already done this step. Congrats!
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
# create a new project in the current directory
|
|
13
|
+
npx sv create
|
|
14
|
+
|
|
15
|
+
# create a new project in my-app
|
|
16
|
+
npx sv create my-app
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Developing
|
|
20
|
+
|
|
21
|
+
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm run dev
|
|
25
|
+
|
|
26
|
+
# or start the server and open the app in a new browser tab
|
|
27
|
+
npm run dev -- --open
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
|
|
31
|
+
|
|
32
|
+
## Building
|
|
33
|
+
|
|
34
|
+
To build your library:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm pack
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
To create a production version of your showcase app:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
npm run build
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can preview the production build with `npm run preview`.
|
|
47
|
+
|
|
48
|
+
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
|
49
|
+
|
|
50
|
+
## Publishing
|
|
51
|
+
|
|
52
|
+
Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
|
|
53
|
+
|
|
54
|
+
To publish your library to [npm](https://www.npmjs.com):
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
npm publish
|
|
58
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Locale } from './locale.ts';
|
|
2
|
+
type Localized<Languages extends string, Fallback extends Languages, T> = {
|
|
3
|
+
[K in Languages]?: T;
|
|
4
|
+
} & {
|
|
5
|
+
[K in Fallback]-?: T;
|
|
6
|
+
};
|
|
7
|
+
type Quantifier = Intl.LDMLPluralRule;
|
|
8
|
+
type Pluralized<T> = {
|
|
9
|
+
[K in Quantifier]?: T;
|
|
10
|
+
} & {
|
|
11
|
+
[K in 'one']-?: T;
|
|
12
|
+
};
|
|
13
|
+
type FunctionLike = (...args: any[]) => unknown;
|
|
14
|
+
type MaybeParameterized<T, V> = {
|
|
15
|
+
[K in keyof T as T[K] extends FunctionLike ? K : never]: (...args: MaybeParameters<T, K>) => V;
|
|
16
|
+
} & {
|
|
17
|
+
[K in keyof T as T[K] extends FunctionLike ? never : K]: V;
|
|
18
|
+
};
|
|
19
|
+
type MaybeParameters<T, K extends keyof T> = T[K] extends FunctionLike ? Parameters<T[K]> : [];
|
|
20
|
+
export type Texts<Languages extends string, Fallback extends Languages, T> = T & MaybeParameterized<T, Localized<Languages, Fallback, Pluralized<string> | string>>;
|
|
21
|
+
export declare class Dictionary<Languages extends string, Fallback extends Languages, T> {
|
|
22
|
+
#private;
|
|
23
|
+
private readonly locale;
|
|
24
|
+
private readonly texts;
|
|
25
|
+
constructor(locale: () => Locale<Languages, Fallback>, texts: Texts<Languages, Fallback, T>);
|
|
26
|
+
readonly translate: <K extends keyof T>(key: K, ...params: MaybeParameters<T, K>) => string;
|
|
27
|
+
readonly t: <K extends keyof T>(key: K, ...params: MaybeParameters<T, K>) => string;
|
|
28
|
+
readonly pluralize: <K extends keyof T>(key: K, n: number, ...params: MaybeParameters<T, K>) => string;
|
|
29
|
+
readonly p: <K extends keyof T>(key: K, n: number, ...params: MaybeParameters<T, K>) => string;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class Dictionary {
|
|
2
|
+
locale;
|
|
3
|
+
texts;
|
|
4
|
+
constructor(locale, texts) {
|
|
5
|
+
this.locale = locale;
|
|
6
|
+
this.texts = texts;
|
|
7
|
+
}
|
|
8
|
+
#translate(key, ...params) {
|
|
9
|
+
return this.pluralize(key, 1, ...params);
|
|
10
|
+
}
|
|
11
|
+
#pluralize(key, n, ...params) {
|
|
12
|
+
const { language, fallback, pluralRules } = this.locale();
|
|
13
|
+
const maybeParameterized = this.texts[key];
|
|
14
|
+
const localized = typeof maybeParameterized === 'function' ? maybeParameterized(...params) : maybeParameterized;
|
|
15
|
+
const maybePluralized = has(localized, language) ? localized[language] : localized[fallback];
|
|
16
|
+
if (isPluralized(maybePluralized)) {
|
|
17
|
+
const quantifier = pluralRules.select(n);
|
|
18
|
+
return has(maybePluralized, quantifier) ? maybePluralized[quantifier] : maybePluralized.one;
|
|
19
|
+
}
|
|
20
|
+
return maybePluralized;
|
|
21
|
+
}
|
|
22
|
+
translate = this.#translate.bind(this);
|
|
23
|
+
t = this.translate;
|
|
24
|
+
pluralize = this.#pluralize.bind(this);
|
|
25
|
+
p = this.pluralize;
|
|
26
|
+
}
|
|
27
|
+
function has(value, key) {
|
|
28
|
+
return value[key] !== undefined && value[key] !== null;
|
|
29
|
+
}
|
|
30
|
+
function isPluralized(maybePluralized) {
|
|
31
|
+
return typeof maybePluralized === 'object' && 'one' in maybePluralized;
|
|
32
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/locale.d.ts
ADDED
package/dist/locale.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dictionary, type Texts } from './dictionary.ts';
|
|
2
|
+
export declare class Translator<Languages extends string, Fallback extends Languages> {
|
|
3
|
+
readonly languages: Languages[];
|
|
4
|
+
readonly fallback: Fallback;
|
|
5
|
+
currentLanguage: Languages;
|
|
6
|
+
private readonly locale;
|
|
7
|
+
constructor(languages: Languages[], fallback: Fallback);
|
|
8
|
+
define<T>(texts: Texts<Languages, Fallback, T>): Dictionary<Languages, Fallback, T>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dictionary } from "./dictionary.js";
|
|
2
|
+
export class Translator {
|
|
3
|
+
languages;
|
|
4
|
+
fallback;
|
|
5
|
+
currentLanguage;
|
|
6
|
+
locale;
|
|
7
|
+
constructor(languages, fallback) {
|
|
8
|
+
this.languages = languages;
|
|
9
|
+
this.fallback = fallback;
|
|
10
|
+
checkSupportedLanguages(languages);
|
|
11
|
+
this.currentLanguage = $state(fallback);
|
|
12
|
+
this.locale = $derived({
|
|
13
|
+
language: this.currentLanguage,
|
|
14
|
+
fallback: this.fallback,
|
|
15
|
+
pluralRules: new Intl.PluralRules(this.currentLanguage),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
define(texts) {
|
|
19
|
+
return new Dictionary(() => this.locale, texts);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function checkSupportedLanguages(languages) {
|
|
23
|
+
const supportedLanguages = Intl.PluralRules.supportedLocalesOf(languages);
|
|
24
|
+
const unsupportedLanguages = languages.filter((l) => !supportedLanguages.includes(l));
|
|
25
|
+
if (unsupportedLanguages.length > 0) {
|
|
26
|
+
throw new Error(`Unsupported languages: ${unsupportedLanguages}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "svelte-component-i18n",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"dev": "vite dev",
|
|
6
|
+
"build": "vite build && npm run prepack",
|
|
7
|
+
"preview": "vite preview",
|
|
8
|
+
"prepare": "svelte-kit sync || echo ''",
|
|
9
|
+
"prepack": "svelte-kit sync && svelte-package && publint",
|
|
10
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
11
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
12
|
+
"format": "prettier --write .",
|
|
13
|
+
"lint": "prettier --check . && eslint .",
|
|
14
|
+
"test:unit": "vitest",
|
|
15
|
+
"test": "npm run test:unit -- --run"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"!dist/**/*.test.*",
|
|
20
|
+
"!dist/**/*.spec.*"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": [
|
|
23
|
+
"**/*.css"
|
|
24
|
+
],
|
|
25
|
+
"svelte": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"svelte": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"svelte": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@eslint/compat": "^1.4.0",
|
|
39
|
+
"@eslint/js": "^9.39.1",
|
|
40
|
+
"@sveltejs/adapter-auto": "^7.0.0",
|
|
41
|
+
"@sveltejs/kit": "^2.49.1",
|
|
42
|
+
"@sveltejs/package": "^2.5.7",
|
|
43
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.1",
|
|
44
|
+
"@types/node": "^22",
|
|
45
|
+
"eslint": "^9.39.1",
|
|
46
|
+
"eslint-config-prettier": "^10.1.8",
|
|
47
|
+
"eslint-plugin-svelte": "^3.13.1",
|
|
48
|
+
"globals": "^16.5.0",
|
|
49
|
+
"prettier": "^3.7.4",
|
|
50
|
+
"prettier-plugin-svelte": "^3.4.0",
|
|
51
|
+
"publint": "^0.3.15",
|
|
52
|
+
"svelte": "^5.45.6",
|
|
53
|
+
"svelte-check": "^4.3.4",
|
|
54
|
+
"typescript": "^5.9.3",
|
|
55
|
+
"typescript-eslint": "^8.48.1",
|
|
56
|
+
"vite": "^7.2.6",
|
|
57
|
+
"vitest": "^4.0.15"
|
|
58
|
+
},
|
|
59
|
+
"keywords": [
|
|
60
|
+
"svelte"
|
|
61
|
+
]
|
|
62
|
+
}
|