@sveltekit-i18n/base 1.0.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 sveltekit-i18n
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.
package/README.md ADDED
@@ -0,0 +1,121 @@
1
+
2
+
3
+ [![npm version](https://badge.fury.io/js/sveltekit-i18n%2Fbase.svg)](https://badge.fury.io/js/sveltekit-i18n%2Fbase) ![](https://github.com/sveltekit-i18n/base/workflows/Tests/badge.svg)
4
+
5
+ # @sveltekit-i18n/base
6
+ This repository contains the base functionality of [sveltekit-i18n](https://github.com/sveltekit-i18n/lib) and provides support for external message parsers.
7
+
8
+ ## Key features
9
+
10
+ ✅ SvelteKit ready\
11
+ ✅ SSR support\
12
+ ✅ Custom data sources – no matter if you are using local files or remote API to get your translations\
13
+ ✅ Module-based – your translations are loaded for visited pages only (and only once!)\
14
+ ✅ Component-scoped translations – you can create multiple instances with custom definitions\
15
+ ✅ Custom modifiers – you can modify the input data the way you really need\
16
+ ✅ TS support\
17
+ ✅ No external dependencies
18
+
19
+ ## Usage
20
+
21
+ Setup `translations.js` in your lib folder...
22
+ ```javascript
23
+ import i18n from '@sveltekit-i18n/base';
24
+ import parser from '@sveltekit-i18n/parser-default'; // use your preferred parser (or create your own)
25
+
26
+ /** @type {import('@sveltekit-i18n/base').Config} */
27
+ export const config = ({
28
+ parser: parser(),
29
+ loaders: [
30
+ {
31
+ locale: 'en',
32
+ key: 'common',
33
+ loader: async () => (
34
+ await import('./en/common.json')
35
+ ).default,
36
+ },
37
+ {
38
+ locale: 'en',
39
+ key: 'home',
40
+ routes: ['/'], // you can use regexes as well!
41
+ loader: async () => (
42
+ await import('./en/home.json')
43
+ ).default,
44
+ },
45
+ {
46
+ locale: 'en',
47
+ key: 'about',
48
+ routes: ['/about'],
49
+ loader: async () => (
50
+ await import('./en/about.json')
51
+ ).default,
52
+ },
53
+ {
54
+ locale: 'cs',
55
+ key: 'common',
56
+ loader: async () => (
57
+ await import('./cs/common.json')
58
+ ).default,
59
+ },
60
+ {
61
+ locale: 'cs',
62
+ key: 'home',
63
+ routes: ['/'],
64
+ loader: async () => (
65
+ await import('./cs/home.json')
66
+ ).default,
67
+ },
68
+ {
69
+ locale: 'cs',
70
+ key: 'about',
71
+ routes: ['/about'],
72
+ loader: async () => (
73
+ await import('./cs/about.json')
74
+ ).default,
75
+ },
76
+ ],
77
+ });
78
+
79
+ export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
80
+ ```
81
+
82
+ ...load your translations in `__layout.svelte`...
83
+
84
+ ```svelte
85
+ <script context="module">
86
+ import { locale, loadTranslations } from '$lib/translations';
87
+
88
+ export const load = async ({ url }) => {
89
+ const { pathname } = url;
90
+
91
+ const defaultLocale = 'en'; // get from cookie, user session, ...
92
+
93
+ const initLocale = locale.get() || defaultLocale; // set default if no locale already set
94
+
95
+ await loadTranslations(initLocale, pathname);
96
+
97
+ return {};
98
+ }
99
+ </script>
100
+ ```
101
+
102
+ ...and include your translations within pages and components.
103
+
104
+ ```svelte
105
+ <script>
106
+ import { t } from '$lib/translations';
107
+
108
+ const pageName = 'This page is Home page!';
109
+ </script>
110
+
111
+ <div>
112
+ <!-- you can use `placeholders` and `modifiers` in your definitions (see docs) -->
113
+ <h2>{$t('common.page', { pageName })}</h2>
114
+ <p>{$t('home.content')}</p>
115
+ </div>
116
+ ```
117
+
118
+ ## More info
119
+ [Docs](https://github.com/sveltekit-i18n/lib/tree/master/docs/README.md)\
120
+ [Examples](https://github.com/sveltekit-i18n/lib/tree/master/examples)\
121
+ [Changelog](https://github.com/sveltekit-i18n/lib/releases)
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@sveltekit-i18n/base",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "Base functionality of sveltekit-i18n library with a support for external message parsers.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./dist/index.cjs",
12
+ "import": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "scripts": {
18
+ "dev": "tsup src/index.ts --watch",
19
+ "test": "jest",
20
+ "build": "tsup src/index.ts --dts --format cjs,esm --minify",
21
+ "prepublishOnly": "rm -rf dist && npm run build",
22
+ "lint": "eslint --fix --ext .ts,.js --ignore-path .gitignore ."
23
+ },
24
+ "files": [
25
+ "lib",
26
+ "types"
27
+ ],
28
+ "pre-commit": [
29
+ "lint"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+ssh://git@github.com/sveltekit-i18n/base.git"
34
+ },
35
+ "keywords": [
36
+ "svelte",
37
+ "sveltekit",
38
+ "i18n",
39
+ "l10n",
40
+ "internationalization",
41
+ "localization",
42
+ "translation"
43
+ ],
44
+ "author": "Jarda Svoboda",
45
+ "license": "MIT",
46
+ "bugs": {
47
+ "url": "https://github.com/sveltekit-i18n/lib/issues"
48
+ },
49
+ "homepage": "https://github.com/sveltekit-i18n/base#readme",
50
+ "peerDependencies": {
51
+ "svelte": "^3.44.3"
52
+ },
53
+ "devDependencies": {
54
+ "@types/jest": "^27.0.3",
55
+ "@typescript-eslint/eslint-plugin": "^5.7.0",
56
+ "@typescript-eslint/parser": "^5.7.0",
57
+ "eslint": "^8.4.1",
58
+ "eslint-config-airbnb-typescript": "^16.1.0",
59
+ "eslint-plugin-import": "^2.25.3",
60
+ "eslint-plugin-svelte3": "^3.2.1",
61
+ "jest": "^27.4.5",
62
+ "pre-commit": "^1.2.2",
63
+ "svelte": "^3.44.3",
64
+ "ts-jest": "^27.1.2",
65
+ "tsup": "^5.11.13",
66
+ "typescript": "^4.5.4"
67
+ }
68
+ }
@@ -0,0 +1,71 @@
1
+ import { Readable, Writable } from 'svelte/store';
2
+
3
+ type LoadingStore = Readable<boolean> & { toPromise: () => Promise<void[]>, get: () => boolean };
4
+
5
+ type Loader = () => Promise<Record<any, any>>;
6
+
7
+ type Route = string | RegExp;
8
+
9
+ type LoaderModule = {
10
+ key: string;
11
+ locale: string;
12
+ routes?: Route[];
13
+ loader: Loader;
14
+ };
15
+
16
+ type TranslationFunction = (key: string, vars?: Record<any, any>) => string;
17
+
18
+ type LocalTranslationFunction = (locale: string, key: string, vars?: Record<any, any>) => string;
19
+
20
+ type ExtendedStore<T, Get = () => T, Store = Readable<T>> = Store & { get: Get };
21
+
22
+ type ConfigTranslations = { [locale: string]: Record<string, any> };
23
+
24
+ type Translations = { [locale: string]: Record<string, string> };
25
+
26
+ type Config = {
27
+ parser: {
28
+ parse: (props: {
29
+ key: string;
30
+ payload: any;
31
+ translations: Translations;
32
+ locale: string;
33
+ fallbackLocale?: string;
34
+ }) => string;
35
+ };
36
+ loaders?: LoaderModule[];
37
+ translations?: ConfigTranslations;
38
+ initLocale?: string;
39
+ fallbackLocale?: string;
40
+ };
41
+
42
+ declare class export_default{
43
+ constructor(config?: Config);
44
+ private loadedKeys;
45
+ private currentRoute;
46
+ private config;
47
+ private isLoading;
48
+ private promises;
49
+ loading: LoadingStore;
50
+ private privateTranslations;
51
+ translations: ExtendedStore<Translations>;
52
+ locales: ExtendedStore<string[]>;
53
+ private internalLocale;
54
+ locale: ExtendedStore<string, () => string, Writable<string>>;
55
+ private loaderTrigger;
56
+ initialized: Readable<boolean>;
57
+ private translation;
58
+ t: ExtendedStore<TranslationFunction, TranslationFunction>;
59
+ l: ExtendedStore<LocalTranslationFunction, LocalTranslationFunction>;
60
+ private getLocale;
61
+ setLocale: (locale?: string | undefined) => Promise<void>;
62
+ setRoute: (route: string) => Promise<void>;
63
+ loadConfig: (config: Config) => Promise<void>;
64
+ addTranslations: (translations?: ConfigTranslations | undefined, keys?: Record<string, string[]> | undefined) => void;
65
+ getTranslationProps: ($locale?: string, $route?: string) => Promise<[ConfigTranslations, Record<string, string[]>] | [
66
+ ]>;
67
+ private translationLoader;
68
+ loadTranslations: (locale: string, route?: string) => Promise<void>;
69
+ }
70
+
71
+ export { Config, export_default as default };