@etamong-playground/i18n 0.1.2

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) 2026 etamong
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,77 @@
1
+ # @etamong-playground/i18n
2
+
3
+ > **About** — One of several small shared libraries used across a personal "fleet" of small apps (error handling · audit logging · encryption-at-rest · i18n · UI · …). Authored and maintained with [Claude Code](https://www.anthropic.com/claude-code) (Anthropic's agentic CLI). Each README documents the design rationale behind the library.
4
+ >
5
+ > **This is a public repository** — keep internal infrastructure details (hostnames, secret/Vault paths, private URLs, internal issue/MR references) out of code, comments, and commit messages.
6
+
7
+ Shared, framework-agnostic React i18n engine for etamong-playground apps. Plain React
8
+ Context — works in Vite + React and Next.js (App Router, inside a `"use client"`
9
+ boundary). The package owns the **machinery**; each app owns its **dictionaries**.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add @etamong-playground/i18n
15
+ ```
16
+
17
+ Resolving `@etamong-playground/*` from GitHub Packages requires the registry in `.npmrc`:
18
+
19
+ ```
20
+ @etamong-playground:registry=https://npm.pkg.github.com
21
+ //npm.pkg.github.com/:_authToken=<your-github-token>
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```ts
27
+ // app/i18n.ts
28
+ import { createI18n } from "@etamong-playground/i18n";
29
+ import ko from "./locales/ko";
30
+ import en from "./locales/en";
31
+
32
+ export const { I18nProvider, useI18n, LanguageSwitcher } = createI18n({
33
+ locales: ["ko", "en"] as const,
34
+ defaultLocale: "ko",
35
+ dictionaries: { ko, en },
36
+ labels: { ko: "한국어", en: "English" },
37
+ });
38
+
39
+ export type Dict = typeof ko; // dictionaries are checked against this shape
40
+ ```
41
+
42
+ ```tsx
43
+ // main.tsx
44
+ import { I18nProvider } from "./i18n";
45
+ <I18nProvider>{app}</I18nProvider>;
46
+
47
+ // any component
48
+ const { t, locale, setLocale } = useI18n(); // t is fully typed
49
+ <h1>{t.landing.title}</h1>;
50
+ <LanguageSwitcher className="lang" />;
51
+ ```
52
+
53
+ `interpolate("{{n}} clicks", { n: 3 })` fills `{{name}}` placeholders.
54
+
55
+ ### Locale resolution order
56
+
57
+ 1. Stored choice (`localStorage["locale"]`, key configurable)
58
+ 2. `userLocale` prop (e.g. from auth/profile) — exact or base match
59
+ 3. `navigator.language` base match
60
+ 4. `defaultLocale`
61
+
62
+ An explicit stored choice always wins; `userLocale` is adopted only when nothing
63
+ is stored yet.
64
+
65
+ ## Release
66
+
67
+ Push a `vX.Y.Z` tag — CI publishes to GitHub Packages (`@etamong-playground/i18n`).
68
+
69
+ The tag must match `version` in `package.json` or the publish job fails.
70
+
71
+ ## Acknowledgements
72
+
73
+ Built for [React](https://react.dev) (peer dependency, MIT).
74
+
75
+ ## License
76
+
77
+ MIT — see [LICENSE](LICENSE).
package/dist/index.cjs ADDED
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+
6
+ // src/createI18n.tsx
7
+ function safeGet(key) {
8
+ try {
9
+ return typeof localStorage !== "undefined" ? localStorage.getItem(key) : null;
10
+ } catch {
11
+ return null;
12
+ }
13
+ }
14
+ function safeSet(key, value) {
15
+ try {
16
+ localStorage.setItem(key, value);
17
+ } catch {
18
+ }
19
+ }
20
+ function createI18n(config) {
21
+ const storageKey = config.storageKey ?? "locale";
22
+ const labels = config.labels ?? Object.fromEntries(config.locales.map((l) => [l, String(l)]));
23
+ const isLocale = (v) => typeof v === "string" && config.locales.includes(v);
24
+ function resolve(userLocale) {
25
+ const stored = safeGet(storageKey);
26
+ if (isLocale(stored)) return stored;
27
+ if (userLocale) {
28
+ if (isLocale(userLocale)) return userLocale;
29
+ const base = userLocale.split("-")[0];
30
+ if (isLocale(base)) return base;
31
+ }
32
+ if (typeof navigator !== "undefined" && navigator.language) {
33
+ const nav = navigator.language.split("-")[0];
34
+ if (isLocale(nav)) return nav;
35
+ }
36
+ return config.defaultLocale;
37
+ }
38
+ const Ctx = react.createContext({
39
+ locale: config.defaultLocale,
40
+ setLocale: () => {
41
+ },
42
+ t: config.dictionaries[config.defaultLocale],
43
+ locales: config.locales,
44
+ labels
45
+ });
46
+ function I18nProvider({ children, userLocale }) {
47
+ const [locale, setLocaleState] = react.useState(() => resolve(userLocale));
48
+ const setLocale = react.useCallback((l) => {
49
+ setLocaleState(l);
50
+ safeSet(storageKey, l);
51
+ }, []);
52
+ react.useEffect(() => {
53
+ try {
54
+ document.documentElement.lang = locale;
55
+ } catch {
56
+ }
57
+ }, [locale]);
58
+ const [seenUserLocale, setSeenUserLocale] = react.useState(userLocale);
59
+ if (userLocale !== seenUserLocale) {
60
+ setSeenUserLocale(userLocale);
61
+ if (userLocale && !safeGet(storageKey)) {
62
+ setLocaleState(resolve(userLocale));
63
+ }
64
+ }
65
+ const value = {
66
+ locale,
67
+ setLocale,
68
+ t: config.dictionaries[locale],
69
+ locales: config.locales,
70
+ labels
71
+ };
72
+ return /* @__PURE__ */ jsxRuntime.jsx(Ctx.Provider, { value, children });
73
+ }
74
+ function useI18n() {
75
+ return react.useContext(Ctx);
76
+ }
77
+ function LanguageSwitcher({ className, "aria-label": ariaLabel }) {
78
+ const { locale, setLocale, locales } = useI18n();
79
+ return /* @__PURE__ */ jsxRuntime.jsx(
80
+ "select",
81
+ {
82
+ className,
83
+ value: locale,
84
+ "aria-label": ariaLabel ?? "Language",
85
+ onChange: (e) => setLocale(e.target.value),
86
+ children: locales.map((l) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: l, children: labels[l] ?? l }, l))
87
+ }
88
+ );
89
+ }
90
+ return { I18nProvider, useI18n, LanguageSwitcher };
91
+ }
92
+
93
+ // src/interpolate.ts
94
+ function interpolate(template, vars) {
95
+ return template.replace(
96
+ /\{\{(\w+)\}\}/g,
97
+ (_, key) => key in vars ? String(vars[key]) : `{{${key}}}`
98
+ );
99
+ }
100
+
101
+ exports.createI18n = createI18n;
102
+ exports.interpolate = interpolate;
@@ -0,0 +1,61 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface I18nConfig<L extends string, Dict> {
5
+ /** Supported locale codes, e.g. `["ko", "en", "ja"]`. */
6
+ locales: readonly L[];
7
+ /** Locale used when nothing else resolves. */
8
+ defaultLocale: L;
9
+ /** One dictionary per locale; all share the same shape (the `Dict` type). */
10
+ dictionaries: Record<L, Dict>;
11
+ /** Display names for the language switcher, e.g. `{ ko: "한국어", en: "English", ja: "日本語" }`. */
12
+ labels?: Record<L, string>;
13
+ /** localStorage key for the persisted choice. Default `"locale"`. */
14
+ storageKey?: string;
15
+ }
16
+ interface I18n<L extends string, Dict> {
17
+ locale: L;
18
+ setLocale: (locale: L) => void;
19
+ /** The active locale's dictionary. */
20
+ t: Dict;
21
+ locales: readonly L[];
22
+ labels: Record<L, string>;
23
+ }
24
+ interface I18nProviderProps {
25
+ children: ReactNode;
26
+ /** Upstream preference (e.g. from auth/profile). Adopted only when no stored choice exists. */
27
+ userLocale?: string;
28
+ }
29
+ interface LanguageSwitcherProps {
30
+ className?: string;
31
+ "aria-label"?: string;
32
+ }
33
+ /**
34
+ * Build a typed i18n instance bound to a fixed set of locales and dictionaries.
35
+ * Framework-agnostic (plain React Context) — works in Vite + React and Next.js
36
+ * (App Router: render `<I18nProvider>` inside a `"use client"` boundary).
37
+ *
38
+ * const { I18nProvider, useI18n, LanguageSwitcher } = createI18n({
39
+ * locales: ["ko", "en", "ja"] as const,
40
+ * defaultLocale: "ko",
41
+ * dictionaries: { ko, en, ja },
42
+ * labels: { ko: "한국어", en: "English", ja: "日本語" },
43
+ * });
44
+ *
45
+ * `useI18n().t` is fully typed as the dictionary shape.
46
+ */
47
+ declare function createI18n<L extends string, Dict>(config: I18nConfig<L, Dict>): {
48
+ I18nProvider: ({ children, userLocale }: I18nProviderProps) => react.JSX.Element;
49
+ useI18n: () => I18n<L, Dict>;
50
+ LanguageSwitcher: ({ className, "aria-label": ariaLabel }: LanguageSwitcherProps) => react.JSX.Element;
51
+ };
52
+
53
+ /**
54
+ * Replace `{{name}}` placeholders in a template with values from `vars`.
55
+ * Unknown placeholders are left untouched so missing data is visible, not silent.
56
+ *
57
+ * interpolate("{{n}} clicks", { n: 3 }) // "3 clicks"
58
+ */
59
+ declare function interpolate(template: string, vars: Record<string, string | number>): string;
60
+
61
+ export { type I18n, type I18nConfig, type I18nProviderProps, type LanguageSwitcherProps, createI18n, interpolate };
@@ -0,0 +1,61 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface I18nConfig<L extends string, Dict> {
5
+ /** Supported locale codes, e.g. `["ko", "en", "ja"]`. */
6
+ locales: readonly L[];
7
+ /** Locale used when nothing else resolves. */
8
+ defaultLocale: L;
9
+ /** One dictionary per locale; all share the same shape (the `Dict` type). */
10
+ dictionaries: Record<L, Dict>;
11
+ /** Display names for the language switcher, e.g. `{ ko: "한국어", en: "English", ja: "日本語" }`. */
12
+ labels?: Record<L, string>;
13
+ /** localStorage key for the persisted choice. Default `"locale"`. */
14
+ storageKey?: string;
15
+ }
16
+ interface I18n<L extends string, Dict> {
17
+ locale: L;
18
+ setLocale: (locale: L) => void;
19
+ /** The active locale's dictionary. */
20
+ t: Dict;
21
+ locales: readonly L[];
22
+ labels: Record<L, string>;
23
+ }
24
+ interface I18nProviderProps {
25
+ children: ReactNode;
26
+ /** Upstream preference (e.g. from auth/profile). Adopted only when no stored choice exists. */
27
+ userLocale?: string;
28
+ }
29
+ interface LanguageSwitcherProps {
30
+ className?: string;
31
+ "aria-label"?: string;
32
+ }
33
+ /**
34
+ * Build a typed i18n instance bound to a fixed set of locales and dictionaries.
35
+ * Framework-agnostic (plain React Context) — works in Vite + React and Next.js
36
+ * (App Router: render `<I18nProvider>` inside a `"use client"` boundary).
37
+ *
38
+ * const { I18nProvider, useI18n, LanguageSwitcher } = createI18n({
39
+ * locales: ["ko", "en", "ja"] as const,
40
+ * defaultLocale: "ko",
41
+ * dictionaries: { ko, en, ja },
42
+ * labels: { ko: "한국어", en: "English", ja: "日本語" },
43
+ * });
44
+ *
45
+ * `useI18n().t` is fully typed as the dictionary shape.
46
+ */
47
+ declare function createI18n<L extends string, Dict>(config: I18nConfig<L, Dict>): {
48
+ I18nProvider: ({ children, userLocale }: I18nProviderProps) => react.JSX.Element;
49
+ useI18n: () => I18n<L, Dict>;
50
+ LanguageSwitcher: ({ className, "aria-label": ariaLabel }: LanguageSwitcherProps) => react.JSX.Element;
51
+ };
52
+
53
+ /**
54
+ * Replace `{{name}}` placeholders in a template with values from `vars`.
55
+ * Unknown placeholders are left untouched so missing data is visible, not silent.
56
+ *
57
+ * interpolate("{{n}} clicks", { n: 3 }) // "3 clicks"
58
+ */
59
+ declare function interpolate(template: string, vars: Record<string, string | number>): string;
60
+
61
+ export { type I18n, type I18nConfig, type I18nProviderProps, type LanguageSwitcherProps, createI18n, interpolate };
package/dist/index.js ADDED
@@ -0,0 +1,99 @@
1
+ import { createContext, useState, useCallback, useEffect, useContext } from 'react';
2
+ import { jsx } from 'react/jsx-runtime';
3
+
4
+ // src/createI18n.tsx
5
+ function safeGet(key) {
6
+ try {
7
+ return typeof localStorage !== "undefined" ? localStorage.getItem(key) : null;
8
+ } catch {
9
+ return null;
10
+ }
11
+ }
12
+ function safeSet(key, value) {
13
+ try {
14
+ localStorage.setItem(key, value);
15
+ } catch {
16
+ }
17
+ }
18
+ function createI18n(config) {
19
+ const storageKey = config.storageKey ?? "locale";
20
+ const labels = config.labels ?? Object.fromEntries(config.locales.map((l) => [l, String(l)]));
21
+ const isLocale = (v) => typeof v === "string" && config.locales.includes(v);
22
+ function resolve(userLocale) {
23
+ const stored = safeGet(storageKey);
24
+ if (isLocale(stored)) return stored;
25
+ if (userLocale) {
26
+ if (isLocale(userLocale)) return userLocale;
27
+ const base = userLocale.split("-")[0];
28
+ if (isLocale(base)) return base;
29
+ }
30
+ if (typeof navigator !== "undefined" && navigator.language) {
31
+ const nav = navigator.language.split("-")[0];
32
+ if (isLocale(nav)) return nav;
33
+ }
34
+ return config.defaultLocale;
35
+ }
36
+ const Ctx = createContext({
37
+ locale: config.defaultLocale,
38
+ setLocale: () => {
39
+ },
40
+ t: config.dictionaries[config.defaultLocale],
41
+ locales: config.locales,
42
+ labels
43
+ });
44
+ function I18nProvider({ children, userLocale }) {
45
+ const [locale, setLocaleState] = useState(() => resolve(userLocale));
46
+ const setLocale = useCallback((l) => {
47
+ setLocaleState(l);
48
+ safeSet(storageKey, l);
49
+ }, []);
50
+ useEffect(() => {
51
+ try {
52
+ document.documentElement.lang = locale;
53
+ } catch {
54
+ }
55
+ }, [locale]);
56
+ const [seenUserLocale, setSeenUserLocale] = useState(userLocale);
57
+ if (userLocale !== seenUserLocale) {
58
+ setSeenUserLocale(userLocale);
59
+ if (userLocale && !safeGet(storageKey)) {
60
+ setLocaleState(resolve(userLocale));
61
+ }
62
+ }
63
+ const value = {
64
+ locale,
65
+ setLocale,
66
+ t: config.dictionaries[locale],
67
+ locales: config.locales,
68
+ labels
69
+ };
70
+ return /* @__PURE__ */ jsx(Ctx.Provider, { value, children });
71
+ }
72
+ function useI18n() {
73
+ return useContext(Ctx);
74
+ }
75
+ function LanguageSwitcher({ className, "aria-label": ariaLabel }) {
76
+ const { locale, setLocale, locales } = useI18n();
77
+ return /* @__PURE__ */ jsx(
78
+ "select",
79
+ {
80
+ className,
81
+ value: locale,
82
+ "aria-label": ariaLabel ?? "Language",
83
+ onChange: (e) => setLocale(e.target.value),
84
+ children: locales.map((l) => /* @__PURE__ */ jsx("option", { value: l, children: labels[l] ?? l }, l))
85
+ }
86
+ );
87
+ }
88
+ return { I18nProvider, useI18n, LanguageSwitcher };
89
+ }
90
+
91
+ // src/interpolate.ts
92
+ function interpolate(template, vars) {
93
+ return template.replace(
94
+ /\{\{(\w+)\}\}/g,
95
+ (_, key) => key in vars ? String(vars[key]) : `{{${key}}}`
96
+ );
97
+ }
98
+
99
+ export { createI18n, interpolate };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@etamong-playground/i18n",
3
+ "version": "0.1.2",
4
+ "description": "Shared framework-agnostic React i18n engine (typed createI18n factory, provider, hook, interpolate, language switcher) for etamong-lab apps.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsup",
21
+ "dev": "tsup --watch",
22
+ "typecheck": "tsc --noEmit"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "peerDependencies": {
28
+ "react": ">=18"
29
+ },
30
+ "devDependencies": {
31
+ "@types/react": "^18.3.12",
32
+ "react": "^18.3.1",
33
+ "tsup": "^8.3.5",
34
+ "typescript": "^5.6.3"
35
+ },
36
+ "license": "MIT"
37
+ }