@dloizides/i18n 1.2.0 → 1.3.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
@@ -7,6 +7,21 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ FLIER-2 "Flier Studio follow-ups" item 6 "React-free FM for vanilla apps".
11
+
12
+ ### Added
13
+
14
+ - `@dloizides/i18n/core` subpath: `createFormatMessage(messages)` over a plain nested messages
15
+ object (`FM(key, ...params)` fills `{{p1}}`, `{{p2}}`, ...) and `interpolateDoubleBrace(template,
16
+ params)` for `{{name}}`. Imports no React and no i18next.
17
+
18
+ ### Changed
19
+
20
+ - `react` and `react-native` peer dependencies are now optional, like `i18next` and
21
+ `react-i18next`, so a vanilla app using only `/core` installs none of them.
22
+
23
+ ## [1.2.0] — 2026-10-21
24
+
10
25
  EXTRACT-TS-I18N-1 "Fold duplicated i18n and utils into @dloizides/i18n and @dloizides/utils".
11
26
 
12
27
  ### Added
package/README.md CHANGED
@@ -124,6 +124,25 @@ export default i18n;
124
124
  The init rejection is swallowed (in-memory resources resolve synchronously). Moved here from
125
125
  `@dloizides/frontend-devtools` 1.x, which is now a dev-only package.
126
126
 
127
+ ## Vanilla core: `@dloizides/i18n/core`
128
+
129
+ For apps with no React and no i18next (plain Vite, workers, scripts). The subpath imports nothing
130
+ outside the package, and `react`, `react-native`, `i18next` and `react-i18next` are all optional
131
+ peers, so installing the package for this subpath pulls none of them in.
132
+
133
+ ```ts
134
+ import { createFormatMessage, interpolateDoubleBrace } from '@dloizides/i18n/core';
135
+ import en from './en.json';
136
+
137
+ export const FM = createFormatMessage(en);
138
+ FM('upload.count', '3', '7'); // "{{p1}} of {{p2}}" -> "3 of 7"
139
+ interpolateDoubleBrace('Hello {{name}}', { name: 'Ada' }); // "Hello Ada"
140
+ ```
141
+
142
+ Dotted keys walk the nested object; a missing key (or a key that points at a branch) returns the
143
+ key; an unfilled `{{placeholder}}` is left intact. The root entry's `createFormatMessage(i18n)` is
144
+ unchanged.
145
+
127
146
  ## Catalog conventions
128
147
 
129
148
  Flat, dotted keys, namespaced by screen — `cases.title`, `onboarding.step1.lead`. English is
@@ -0,0 +1,14 @@
1
+ /** A plain nested messages object, e.g. the contents of `en.json`. */
2
+ interface MessageTree {
3
+ [key: string]: string | MessageTree;
4
+ }
5
+ /** Values that can fill a `{{name}}` placeholder. */
6
+ type TemplateParams = Readonly<Record<string, string | number>>;
7
+ /** Positional message formatter: `FM(key, p1, p2, ...)` fills `{{p1}}`, `{{p2}}`, ... in order. */
8
+ type FormatMessage = (key: string, ...params: string[]) => string;
9
+ /** Fills `{{name}}` placeholders from `params`; an unknown name is left intact. */
10
+ declare function interpolateDoubleBrace(template: string, params: TemplateParams): string;
11
+ /** Builds a React-free, i18next-free `FM()` over a plain messages object; a missing key returns the key. */
12
+ declare function createFormatMessage(messages: MessageTree): FormatMessage;
13
+
14
+ export { type FormatMessage, type MessageTree, type TemplateParams, createFormatMessage, interpolateDoubleBrace };
package/dist/core.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /** A plain nested messages object, e.g. the contents of `en.json`. */
2
+ interface MessageTree {
3
+ [key: string]: string | MessageTree;
4
+ }
5
+ /** Values that can fill a `{{name}}` placeholder. */
6
+ type TemplateParams = Readonly<Record<string, string | number>>;
7
+ /** Positional message formatter: `FM(key, p1, p2, ...)` fills `{{p1}}`, `{{p2}}`, ... in order. */
8
+ type FormatMessage = (key: string, ...params: string[]) => string;
9
+ /** Fills `{{name}}` placeholders from `params`; an unknown name is left intact. */
10
+ declare function interpolateDoubleBrace(template: string, params: TemplateParams): string;
11
+ /** Builds a React-free, i18next-free `FM()` over a plain messages object; a missing key returns the key. */
12
+ declare function createFormatMessage(messages: MessageTree): FormatMessage;
13
+
14
+ export { type FormatMessage, type MessageTree, type TemplateParams, createFormatMessage, interpolateDoubleBrace };
package/dist/core.js ADDED
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ // src/core/formatMessage.ts
4
+ var DOUBLE_BRACE_PLACEHOLDER = /\{\{(\w+)\}\}/g;
5
+ var POSITIONAL_PREFIX = "p";
6
+ function interpolateDoubleBrace(template, params) {
7
+ return template.replace(
8
+ DOUBLE_BRACE_PLACEHOLDER,
9
+ (match, name) => Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match
10
+ );
11
+ }
12
+ function lookupMessage(messages, key) {
13
+ const found = key.split(".").reduce(
14
+ (node, part) => typeof node === "object" ? node[part] : void 0,
15
+ messages
16
+ );
17
+ return typeof found === "string" ? found : void 0;
18
+ }
19
+ function toPositionalParams(params) {
20
+ return Object.fromEntries(params.map((value, index) => [`${POSITIONAL_PREFIX}${String(index + 1)}`, value]));
21
+ }
22
+ function createFormatMessage(messages) {
23
+ return (key, ...params) => {
24
+ const message = lookupMessage(messages, key);
25
+ if (message === void 0) return key;
26
+ return interpolateDoubleBrace(message, toPositionalParams(params));
27
+ };
28
+ }
29
+
30
+ exports.createFormatMessage = createFormatMessage;
31
+ exports.interpolateDoubleBrace = interpolateDoubleBrace;
32
+ //# sourceMappingURL=core.js.map
33
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/formatMessage.ts"],"names":[],"mappings":";;;AAWA,IAAM,wBAAA,GAA2B,gBAAA;AACjC,IAAM,iBAAA,GAAoB,GAAA;AAGnB,SAAS,sBAAA,CAAuB,UAAkB,MAAA,EAAgC;AACvF,EAAA,OAAO,QAAA,CAAS,OAAA;AAAA,IAAQ,wBAAA;AAAA,IAA0B,CAAC,KAAA,EAAO,IAAA,KACxD,MAAA,CAAO,UAAU,cAAA,CAAe,IAAA,CAAK,MAAA,EAAQ,IAAI,CAAA,GAAI,MAAA,CAAO,MAAA,CAAO,IAAI,CAAC,CAAA,GAAI;AAAA,GAC9E;AACF;AAEA,SAAS,aAAA,CAAc,UAAuB,GAAA,EAAiC;AAC7E,EAAA,MAAM,KAAA,GAAQ,GAAA,CACX,KAAA,CAAM,GAAG,CAAA,CACT,MAAA;AAAA,IACC,CAAC,MAAM,IAAA,KAAU,OAAO,SAAS,QAAA,GAAW,IAAA,CAAK,IAAI,CAAA,GAAI,MAAA;AAAA,IACzD;AAAA,GACF;AACF,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,GAAW,KAAA,GAAQ,MAAA;AAC7C;AAEA,SAAS,mBAAmB,MAAA,EAAmD;AAC7E,EAAA,OAAO,OAAO,WAAA,CAAY,MAAA,CAAO,IAAI,CAAC,KAAA,EAAO,UAAU,CAAC,CAAA,EAAG,iBAAiB,CAAA,EAAG,OAAO,KAAA,GAAQ,CAAC,CAAC,CAAA,CAAA,EAAI,KAAK,CAAC,CAAC,CAAA;AAC7G;AAGO,SAAS,oBAAoB,QAAA,EAAsC;AACxE,EAAA,OAAO,CAAC,QAAQ,MAAA,KAAW;AACzB,IAAA,MAAM,OAAA,GAAU,aAAA,CAAc,QAAA,EAAU,GAAG,CAAA;AAC3C,IAAA,IAAI,OAAA,KAAY,QAAW,OAAO,GAAA;AAClC,IAAA,OAAO,sBAAA,CAAuB,OAAA,EAAS,kBAAA,CAAmB,MAAM,CAAC,CAAA;AAAA,EACnE,CAAA;AACF","file":"core.js","sourcesContent":["/** A plain nested messages object, e.g. the contents of `en.json`. */\nexport interface MessageTree {\n [key: string]: string | MessageTree;\n}\n\n/** Values that can fill a `{{name}}` placeholder. */\nexport type TemplateParams = Readonly<Record<string, string | number>>;\n\n/** Positional message formatter: `FM(key, p1, p2, ...)` fills `{{p1}}`, `{{p2}}`, ... in order. */\nexport type FormatMessage = (key: string, ...params: string[]) => string;\n\nconst DOUBLE_BRACE_PLACEHOLDER = /\\{\\{(\\w+)\\}\\}/g;\nconst POSITIONAL_PREFIX = 'p';\n\n/** Fills `{{name}}` placeholders from `params`; an unknown name is left intact. */\nexport function interpolateDoubleBrace(template: string, params: TemplateParams): string {\n return template.replace(DOUBLE_BRACE_PLACEHOLDER, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\nfunction lookupMessage(messages: MessageTree, key: string): string | undefined {\n const found = key\n .split('.')\n .reduce<string | MessageTree | undefined>(\n (node, part) => (typeof node === 'object' ? node[part] : undefined),\n messages,\n );\n return typeof found === 'string' ? found : undefined;\n}\n\nfunction toPositionalParams(params: readonly string[]): Record<string, string> {\n return Object.fromEntries(params.map((value, index) => [`${POSITIONAL_PREFIX}${String(index + 1)}`, value]));\n}\n\n/** Builds a React-free, i18next-free `FM()` over a plain messages object; a missing key returns the key. */\nexport function createFormatMessage(messages: MessageTree): FormatMessage {\n return (key, ...params) => {\n const message = lookupMessage(messages, key);\n if (message === undefined) return key;\n return interpolateDoubleBrace(message, toPositionalParams(params));\n };\n}\n"]}
package/dist/core.mjs ADDED
@@ -0,0 +1,30 @@
1
+ // src/core/formatMessage.ts
2
+ var DOUBLE_BRACE_PLACEHOLDER = /\{\{(\w+)\}\}/g;
3
+ var POSITIONAL_PREFIX = "p";
4
+ function interpolateDoubleBrace(template, params) {
5
+ return template.replace(
6
+ DOUBLE_BRACE_PLACEHOLDER,
7
+ (match, name) => Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match
8
+ );
9
+ }
10
+ function lookupMessage(messages, key) {
11
+ const found = key.split(".").reduce(
12
+ (node, part) => typeof node === "object" ? node[part] : void 0,
13
+ messages
14
+ );
15
+ return typeof found === "string" ? found : void 0;
16
+ }
17
+ function toPositionalParams(params) {
18
+ return Object.fromEntries(params.map((value, index) => [`${POSITIONAL_PREFIX}${String(index + 1)}`, value]));
19
+ }
20
+ function createFormatMessage(messages) {
21
+ return (key, ...params) => {
22
+ const message = lookupMessage(messages, key);
23
+ if (message === void 0) return key;
24
+ return interpolateDoubleBrace(message, toPositionalParams(params));
25
+ };
26
+ }
27
+
28
+ export { createFormatMessage, interpolateDoubleBrace };
29
+ //# sourceMappingURL=core.mjs.map
30
+ //# source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B,MAAM,CAAC,CAAA;AAAA,EACnE,CAAA;AACF","file":"core.mjs","sourcesContent":["/** A plain nested messages object, e.g. the contents of `en.json`. */\nexport interface MessageTree {\n [key: string]: string | MessageTree;\n}\n\n/** Values that can fill a `{{name}}` placeholder. */\nexport type TemplateParams = Readonly<Record<string, string | number>>;\n\n/** Positional message formatter: `FM(key, p1, p2, ...)` fills `{{p1}}`, `{{p2}}`, ... in order. */\nexport type FormatMessage = (key: string, ...params: string[]) => string;\n\nconst DOUBLE_BRACE_PLACEHOLDER = /\\{\\{(\\w+)\\}\\}/g;\nconst POSITIONAL_PREFIX = 'p';\n\n/** Fills `{{name}}` placeholders from `params`; an unknown name is left intact. */\nexport function interpolateDoubleBrace(template: string, params: TemplateParams): string {\n return template.replace(DOUBLE_BRACE_PLACEHOLDER, (match, name: string) =>\n Object.prototype.hasOwnProperty.call(params, name) ? String(params[name]) : match,\n );\n}\n\nfunction lookupMessage(messages: MessageTree, key: string): string | undefined {\n const found = key\n .split('.')\n .reduce<string | MessageTree | undefined>(\n (node, part) => (typeof node === 'object' ? node[part] : undefined),\n messages,\n );\n return typeof found === 'string' ? found : undefined;\n}\n\nfunction toPositionalParams(params: readonly string[]): Record<string, string> {\n return Object.fromEntries(params.map((value, index) => [`${POSITIONAL_PREFIX}${String(index + 1)}`, value]));\n}\n\n/** Builds a React-free, i18next-free `FM()` over a plain messages object; a missing key returns the key. */\nexport function createFormatMessage(messages: MessageTree): FormatMessage {\n return (key, ...params) => {\n const message = lookupMessage(messages, key);\n if (message === undefined) return key;\n return interpolateDoubleBrace(message, toPositionalParams(params));\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dloizides/i18n",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Tiny, dependency-free i18n runtime for React / React Native (RN-web) apps — locale detection, persisted choice, per-key English fallback, positional AND named interpolation, and a themable LanguageSwitcher. Its translate function matches the @dloizides UI-kit's `t` contract, so dropping it into UiProvider localizes every shared component for free. Part of the dloizides.com shared kit.",
5
5
  "keywords": [
6
6
  "react",
@@ -49,6 +49,35 @@
49
49
  "types": "./dist/index.d.ts",
50
50
  "default": "./dist/index.js"
51
51
  }
52
+ },
53
+ "./core": {
54
+ "react-native": {
55
+ "types": "./dist/core.d.ts",
56
+ "default": "./dist/core.js"
57
+ },
58
+ "browser": {
59
+ "types": "./dist/core.d.ts",
60
+ "default": "./dist/core.js"
61
+ },
62
+ "require": {
63
+ "types": "./dist/core.d.ts",
64
+ "default": "./dist/core.js"
65
+ },
66
+ "import": {
67
+ "types": "./dist/core.d.mts",
68
+ "default": "./dist/core.mjs"
69
+ },
70
+ "default": {
71
+ "types": "./dist/core.d.ts",
72
+ "default": "./dist/core.js"
73
+ }
74
+ }
75
+ },
76
+ "typesVersions": {
77
+ "*": {
78
+ "core": [
79
+ "./dist/core.d.ts"
80
+ ]
52
81
  }
53
82
  },
54
83
  "files": [
@@ -111,6 +140,12 @@
111
140
  "typescript": "^5.4.0"
112
141
  },
113
142
  "peerDependenciesMeta": {
143
+ "react": {
144
+ "optional": true
145
+ },
146
+ "react-native": {
147
+ "optional": true
148
+ },
114
149
  "i18next": {
115
150
  "optional": true
116
151
  },