@zjazj/i18n 0.0.0-stage → 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 CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # @zjazj/i18n
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Locale, message catalogs and formatting for platform apps. A thin layer over i18next.
4
+
5
+ ```ts
6
+ import { formatDateTime, registerMessages, t } from '@zjazj/i18n'
7
+ import { useT } from '@zjazj/i18n/react'
8
+ ```
9
+
10
+ Locales are `en-US` and `zh-CN`. English is the source language. Dates are formatted in the tenant's time zone.
11
+
12
+ Part of [platform-cli](https://github.com/zjazj/platform-cli). All `@zjazj/*` packages share one version. Install them at the same version.
@@ -0,0 +1,106 @@
1
+ // src/index.ts
2
+ import i18next from "i18next";
3
+ var LOCALES = ["en-US", "zh-CN"];
4
+ var DEFAULT_LOCALE = "en-US";
5
+ var LOCALE_STORAGE_KEY = "platform.locale";
6
+ function isLocale(value) {
7
+ return typeof value === "string" && LOCALES.includes(value);
8
+ }
9
+ function readStoredLocale() {
10
+ try {
11
+ const stored = globalThis.localStorage?.getItem(LOCALE_STORAGE_KEY);
12
+ if (isLocale(stored)) return stored;
13
+ } catch {
14
+ }
15
+ return DEFAULT_LOCALE;
16
+ }
17
+ var i18n = i18next.createInstance();
18
+ void i18n.init({
19
+ lng: readStoredLocale(),
20
+ fallbackLng: DEFAULT_LOCALE,
21
+ supportedLngs: [...LOCALES],
22
+ resources: {},
23
+ defaultNS: "app",
24
+ interpolation: { escapeValue: false },
25
+ initAsync: false
26
+ });
27
+ function registerMessages(namespace, catalog) {
28
+ for (const locale of LOCALES) {
29
+ const messages = catalog[locale];
30
+ if (messages) i18n.addResourceBundle(locale, namespace, messages, true, true);
31
+ }
32
+ }
33
+ function t(key, options) {
34
+ return i18n.t(key, options ?? {});
35
+ }
36
+ function getLocale() {
37
+ return isLocale(i18n.language) ? i18n.language : DEFAULT_LOCALE;
38
+ }
39
+ function setLocale(locale) {
40
+ if (!isLocale(locale)) throw new Error(`Unsupported locale: ${String(locale)}`);
41
+ try {
42
+ globalThis.localStorage?.setItem(LOCALE_STORAGE_KEY, locale);
43
+ } catch {
44
+ }
45
+ void i18n.changeLanguage(locale);
46
+ }
47
+ function onLocaleChange(listener) {
48
+ const handler = () => listener(getLocale());
49
+ i18n.on("languageChanged", handler);
50
+ return () => i18n.off("languageChanged", handler);
51
+ }
52
+ if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
53
+ window.addEventListener("storage", (event) => {
54
+ if (event.key === LOCALE_STORAGE_KEY && isLocale(event.newValue)) {
55
+ void i18n.changeLanguage(event.newValue);
56
+ }
57
+ });
58
+ }
59
+ var timeZone;
60
+ function setTimeZone(zone) {
61
+ timeZone = zone || void 0;
62
+ }
63
+ function getTimeZone() {
64
+ return timeZone;
65
+ }
66
+ function toDate(value) {
67
+ const date = value instanceof Date ? value : new Date(value);
68
+ if (Number.isNaN(date.getTime())) throw new RangeError(`Invalid date: ${String(value)}`);
69
+ return date;
70
+ }
71
+ function formatDate(value, options) {
72
+ return new Intl.DateTimeFormat(getLocale(), {
73
+ dateStyle: "medium",
74
+ timeZone,
75
+ ...options
76
+ }).format(toDate(value));
77
+ }
78
+ function formatDateTime(value, options) {
79
+ return new Intl.DateTimeFormat(getLocale(), {
80
+ dateStyle: "medium",
81
+ timeStyle: "short",
82
+ timeZone,
83
+ ...options
84
+ }).format(toDate(value));
85
+ }
86
+ function formatNumber(value, options) {
87
+ return new Intl.NumberFormat(getLocale(), options).format(value);
88
+ }
89
+
90
+ export {
91
+ LOCALES,
92
+ DEFAULT_LOCALE,
93
+ LOCALE_STORAGE_KEY,
94
+ isLocale,
95
+ i18n,
96
+ registerMessages,
97
+ t,
98
+ getLocale,
99
+ setLocale,
100
+ onLocaleChange,
101
+ setTimeZone,
102
+ getTimeZone,
103
+ formatDate,
104
+ formatDateTime,
105
+ formatNumber
106
+ };
@@ -0,0 +1,43 @@
1
+ import { i18n as i18n$1, TOptions } from 'i18next';
2
+ export { TOptions } from 'i18next';
3
+
4
+ /** Locales the platform ships. `en-US` is the source language. */
5
+ declare const LOCALES: readonly ["en-US", "zh-CN"];
6
+ type Locale = (typeof LOCALES)[number];
7
+ declare const DEFAULT_LOCALE: Locale;
8
+ /** The one stored value that decides the locale. Nothing else is consulted. */
9
+ declare const LOCALE_STORAGE_KEY = "platform.locale";
10
+ type Messages = {
11
+ [key: string]: string | Messages;
12
+ };
13
+ /** Messages for one namespace. English is required, other locales are optional. */
14
+ type Catalog = {
15
+ 'en-US': Messages;
16
+ } & Partial<Record<Locale, Messages>>;
17
+ declare function isLocale(value: unknown): value is Locale;
18
+ /** The shared i18next instance. Use it directly only for features this module does not wrap. */
19
+ declare const i18n: i18n$1;
20
+ /**
21
+ * Add the messages of one namespace. Packages and apps each register their own namespace,
22
+ * for example `registerMessages('app', { 'en-US': {...}, 'zh-CN': {...} })`.
23
+ */
24
+ declare function registerMessages(namespace: string, catalog: Catalog): void;
25
+ /**
26
+ * Translate a key. Use `namespace:key.path` for a namespace other than `app`.
27
+ * A missing translation falls back to English, then to the key itself.
28
+ */
29
+ declare function t(key: string, options?: TOptions): string;
30
+ declare function getLocale(): Locale;
31
+ /** Switch the locale, remember it, and notify subscribers in this and other tabs. */
32
+ declare function setLocale(locale: Locale): void;
33
+ /** Subscribe to locale changes. Returns the unsubscribe function. */
34
+ declare function onLocaleChange(listener: (locale: Locale) => void): () => void;
35
+ /** Set the IANA time zone used by the date formatters, for example `Asia/Shanghai`. */
36
+ declare function setTimeZone(zone: string | undefined): void;
37
+ declare function getTimeZone(): string | undefined;
38
+ type DateInput = Date | string | number;
39
+ declare function formatDate(value: DateInput, options?: Intl.DateTimeFormatOptions): string;
40
+ declare function formatDateTime(value: DateInput, options?: Intl.DateTimeFormatOptions): string;
41
+ declare function formatNumber(value: number, options?: Intl.NumberFormatOptions): string;
42
+
43
+ export { type Catalog, DEFAULT_LOCALE, LOCALES, LOCALE_STORAGE_KEY, type Locale, type Messages, formatDate, formatDateTime, formatNumber, getLocale, getTimeZone, i18n, isLocale, onLocaleChange, registerMessages, setLocale, setTimeZone, t };
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ import {
2
+ DEFAULT_LOCALE,
3
+ LOCALES,
4
+ LOCALE_STORAGE_KEY,
5
+ formatDate,
6
+ formatDateTime,
7
+ formatNumber,
8
+ getLocale,
9
+ getTimeZone,
10
+ i18n,
11
+ isLocale,
12
+ onLocaleChange,
13
+ registerMessages,
14
+ setLocale,
15
+ setTimeZone,
16
+ t
17
+ } from "./chunk-ARSIJEVN.js";
18
+ export {
19
+ DEFAULT_LOCALE,
20
+ LOCALES,
21
+ LOCALE_STORAGE_KEY,
22
+ formatDate,
23
+ formatDateTime,
24
+ formatNumber,
25
+ getLocale,
26
+ getTimeZone,
27
+ i18n,
28
+ isLocale,
29
+ onLocaleChange,
30
+ registerMessages,
31
+ setLocale,
32
+ setTimeZone,
33
+ t
34
+ };
@@ -0,0 +1,13 @@
1
+ import { Locale } from './index.js';
2
+ import { TOptions } from 'i18next';
3
+
4
+ /** The current locale and a setter. Re-renders when the locale changes. */
5
+ declare function useLocale(): [Locale, (locale: Locale) => void];
6
+ type Translate = (key: string, options?: TOptions) => string;
7
+ /**
8
+ * A translate function bound to a namespace. Keys without a `namespace:` prefix resolve in
9
+ * that namespace. Re-renders when the locale changes or new messages are registered.
10
+ */
11
+ declare function useT(namespace?: string): Translate;
12
+
13
+ export { type Translate, useLocale, useT };
package/dist/react.js ADDED
@@ -0,0 +1,39 @@
1
+ import {
2
+ getLocale,
3
+ i18n,
4
+ setLocale,
5
+ t
6
+ } from "./chunk-ARSIJEVN.js";
7
+
8
+ // src/react.ts
9
+ import { useCallback, useSyncExternalStore } from "react";
10
+ var version = 0;
11
+ var listeners = /* @__PURE__ */ new Set();
12
+ function bump() {
13
+ version += 1;
14
+ for (const listener of listeners) listener();
15
+ }
16
+ i18n.on("languageChanged", bump);
17
+ i18n.store?.on("added", bump);
18
+ function subscribe(listener) {
19
+ listeners.add(listener);
20
+ return () => {
21
+ listeners.delete(listener);
22
+ };
23
+ }
24
+ var getVersion = () => version;
25
+ function useLocale() {
26
+ useSyncExternalStore(subscribe, getVersion, getVersion);
27
+ return [getLocale(), setLocale];
28
+ }
29
+ function useT(namespace = "app") {
30
+ const current = useSyncExternalStore(subscribe, getVersion, getVersion);
31
+ return useCallback(
32
+ (key, options) => t(key.includes(":") ? key : `${namespace}:${key}`, options),
33
+ [namespace, current]
34
+ );
35
+ }
36
+ export {
37
+ useLocale,
38
+ useT
39
+ };
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@zjazj/i18n",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
4
+ "description": "Locale, message catalogs and formatting for platform apps.",
5
+ "type": "module",
6
+ "license": "UNLICENSED",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/zjazj/platform-cli.git",
10
+ "directory": "packages/i18n"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "./react": {
18
+ "types": "./dist/react.d.ts",
19
+ "default": "./dist/react.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": false,
26
+ "dependencies": {
27
+ "i18next": "^27.1.1"
28
+ },
29
+ "peerDependencies": {
30
+ "react": "^19.0.0"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "react": {
34
+ "optional": true
35
+ }
36
+ },
37
+ "devDependencies": {
38
+ "@types/react": "^19.2.0",
39
+ "react": "^19.2.0"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public",
43
+ "registry": "https://registry.npmjs.org/"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup",
47
+ "test": "vitest run",
48
+ "typecheck": "tsc --noEmit -p tsconfig.json"
49
+ }
6
50
  }