@quietui/squeak 1.0.0 → 3.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.0
4
+
5
+ - [breaking] Improved translation file format so args are named instead of order-based
6
+
7
+ ## 1.1.0
8
+
9
+ - Added support for SSR environments
10
+
3
11
  ## 1.0.0
4
12
 
5
13
  - Initial fork
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Squeak is a tiny, zero-dependency library that provides a [Reactive Controller](https://lit.dev/docs/composition/controllers/) for localizing terms, dates, and numbers, and currency across one or more custom elements in a component library. It _does not_ aim to replicate a full-blown localization tool. For that, you should use something like [i18next](https://www.i18next.com/).
4
4
 
5
- Reactive Controllers are supported by Lit out of the box, but they're designed to be generic so other libraries can elect to support them either natively or through an adapter. If you're favorite custom element authoring library doesn't support Reactive Controllers yet, consider asking the maintainers to add support for them!
5
+ Reactive Controllers are supported by Lit out of the box, but they're designed to be generic so other libraries can elect to support them either natively or through an adapter. If your favorite custom element authoring library doesn't support Reactive Controllers yet, consider asking the maintainers to add support for them!
6
6
 
7
7
  ## Overview
8
8
 
@@ -175,9 +175,9 @@ async function changeLanguage(lang) {
175
175
  You can use Squeak with any library that supports [Lit's Reactive Controller pattern](https://lit.dev/docs/composition/controllers/). In Lit, a localized custom element will look something like this.
176
176
 
177
177
  ```ts
178
- import { LitElement } from 'lit';
179
- import { customElement } from 'lit/decorators.js';
180
- import { Localize } from '@quietui/squeak/dist/lit.js';
178
+ import { LitElement, html } from 'lit';
179
+ import { customElement, property } from 'lit/decorators.js';
180
+ import { Localize } from '@quietui/squeak/dist/index.js';
181
181
 
182
182
  @customElement('my-element')
183
183
  export class MyElement extends LitElement {
package/cspell.json ADDED
@@ -0,0 +1,18 @@
1
+ // cSpell Settings
2
+ {
3
+ // Version of the setting file. Always 0.2
4
+ "version": "0.2",
5
+ // language - current active spelling language
6
+ "language": "en,lorem",
7
+ // words - list of words to be always considered correct
8
+ "words": [
9
+ "claviska",
10
+ "Español",
11
+ "LaViska",
12
+ "quietui"
13
+ ],
14
+ // flagWords - list of words to be always considered incorrect
15
+ // This is useful for offensive words and common spelling errors.
16
+ // For example "hte" should be "the"
17
+ "flagWords": []
18
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ReactiveController, ReactiveControllerHost } from 'lit';
2
- export type FunctionParams<T> = T extends (...args: infer U) => string ? U : [];
2
+ export type FunctionParams<T> = T extends (params: infer P) => string ? [P] : [];
3
3
  export interface Translation {
4
4
  $code: string;
5
5
  $name: string;
package/dist/index.js CHANGED
@@ -1,13 +1,18 @@
1
1
  const connectedElements = new Set();
2
- const documentElementObserver = new MutationObserver(update);
3
2
  const translations = new Map();
4
- let documentDirection = document.documentElement.dir || 'ltr';
5
- let documentLanguage = document.documentElement.lang || navigator.language;
3
+ const isSSR = typeof (document === null || document === void 0 ? void 0 : document.documentElement) === 'undefined';
4
+ let documentDirection = 'ltr';
5
+ let documentLanguage = 'en';
6
6
  let defaultTranslation;
7
- documentElementObserver.observe(document.documentElement, {
8
- attributes: true,
9
- attributeFilter: ['dir', 'lang']
10
- });
7
+ if (!isSSR) {
8
+ const documentElementObserver = new MutationObserver(update);
9
+ documentDirection = document.documentElement.dir || 'ltr';
10
+ documentLanguage = document.documentElement.lang || navigator.language;
11
+ documentElementObserver.observe(document.documentElement, {
12
+ attributes: true,
13
+ attributeFilter: ['dir', 'lang']
14
+ });
15
+ }
11
16
  export function registerDefaultTranslation(translation) {
12
17
  defaultTranslation = translation;
13
18
  update();
@@ -25,6 +30,8 @@ export function registerTranslation(...translation) {
25
30
  update();
26
31
  }
27
32
  export function update() {
33
+ if (isSSR)
34
+ return;
28
35
  documentDirection = document.documentElement.dir || 'ltr';
29
36
  documentLanguage = document.documentElement.lang || navigator.language;
30
37
  [...connectedElements.keys()].map((el) => {
@@ -87,7 +94,13 @@ export class Localize {
87
94
  return String(key);
88
95
  }
89
96
  if (typeof term === 'function') {
90
- return term(...args);
97
+ try {
98
+ return term(args[0]);
99
+ }
100
+ catch (_a) {
101
+ console.warn(`Translation term "${String(key)}" threw an error. Make sure all required parameters are provided.`);
102
+ return String(key);
103
+ }
91
104
  }
92
105
  return term;
93
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quietui/squeak",
3
- "version": "1.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "A tiny, zero-dependency library that provides a Reactive Controller for localizing terms, dates, and numbers, and currency across one or more custom elements in a component library.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -9,9 +9,10 @@
9
9
  "scripts": {
10
10
  "start": "tsc -w",
11
11
  "build": "tsc",
12
- "clean": "node ./scripts/clean.js",
12
+ "clean": "rimraf dist",
13
13
  "prebuild": "npm run clean",
14
- "prepublishOnly": "npm run build"
14
+ "prepublishOnly": "npm run build",
15
+ "check-updates": "npx npm-check-updates --interactive --format group"
15
16
  },
16
17
  "repository": {
17
18
  "type": "git",
@@ -35,6 +36,7 @@
35
36
  "del": "^7.1.0",
36
37
  "lit": "^3.1.2",
37
38
  "prettier": "^3.2.5",
39
+ "rimraf": "^6.0.1",
38
40
  "typescript": "^5.3.3"
39
41
  }
40
42
  }
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { LitElement, ReactiveController, ReactiveControllerHost } from 'lit';
2
2
 
3
- export type FunctionParams<T> = T extends (...args: infer U) => string ? U : [];
3
+ export type FunctionParams<T> = T extends (params: infer P) => string ? [P] : [];
4
4
 
5
5
  export interface Translation {
6
6
  $code: string; // e.g. en, en-GB
@@ -14,17 +14,24 @@ export interface ExistsOptions {
14
14
  }
15
15
 
16
16
  const connectedElements = new Set<HTMLElement>();
17
- const documentElementObserver = new MutationObserver(update);
18
17
  const translations: Map<string, Translation> = new Map();
19
- let documentDirection = document.documentElement.dir || 'ltr';
20
- let documentLanguage = document.documentElement.lang || navigator.language;
18
+ const isSSR = typeof document?.documentElement === 'undefined';
19
+ let documentDirection = 'ltr'; // SSR default
20
+ let documentLanguage = 'en'; // SSR default
21
21
  let defaultTranslation: Translation;
22
22
 
23
- // Watch for changes on <html lang>
24
- documentElementObserver.observe(document.documentElement, {
25
- attributes: true,
26
- attributeFilter: ['dir', 'lang']
27
- });
23
+ // Don't run this block in an SSR environment
24
+ if (!isSSR) {
25
+ const documentElementObserver = new MutationObserver(update);
26
+ documentDirection = document.documentElement.dir || 'ltr';
27
+ documentLanguage = document.documentElement.lang || navigator.language;
28
+
29
+ // Watch for changes on <html lang>
30
+ documentElementObserver.observe(document.documentElement, {
31
+ attributes: true,
32
+ attributeFilter: ['dir', 'lang']
33
+ });
34
+ }
28
35
 
29
36
  /** Registers the default (fallback) translation. */
30
37
  export function registerDefaultTranslation(translation: Translation) {
@@ -50,6 +57,8 @@ export function registerTranslation(...translation: Translation[]) {
50
57
 
51
58
  /** Updates all localized elements that are currently connected */
52
59
  export function update() {
60
+ if (isSSR) return;
61
+
53
62
  documentDirection = document.documentElement.dir || 'ltr';
54
63
  documentLanguage = document.documentElement.lang || navigator.language;
55
64
 
@@ -161,7 +170,12 @@ export class Localize<UserTranslation extends Translation> implements ReactiveCo
161
170
  }
162
171
 
163
172
  if (typeof term === 'function') {
164
- return term(...args) as string;
173
+ try {
174
+ return term(args[0]) as string;
175
+ } catch {
176
+ console.warn(`Translation term "${String(key)}" threw an error. Make sure all required parameters are provided.`);
177
+ return String(key);
178
+ }
165
179
  }
166
180
 
167
181
  return term;
package/scripts/clean.js DELETED
@@ -1,3 +0,0 @@
1
- import { deleteSync } from 'del';
2
-
3
- deleteSync('./dist');