@rcooler/i18n-core 0.0.0-stage → 0.1.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) 2026 Roman Derevianko
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 CHANGED
@@ -1,3 +1,17 @@
1
- # Temporary Holding Version
1
+ # @rcooler/i18n-core
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
+ Typed `uk` and `en` dictionaries, placeholders, plural forms and error text by code
4
+ (`createTranslator`, `fill`, `toLocale`, `errorCode`). No runtime dependencies. React apps use it
5
+ through [`@rcooler/react-i18n`](https://www.npmjs.com/package/@rcooler/react-i18n).
6
+
7
+ ```sh
8
+ npm install @rcooler/i18n-core
9
+ ```
10
+
11
+ ESM with type declarations. Part of [web-kit](https://github.com/RCooLeR/web-kit): the
12
+ [README](https://github.com/RCooLeR/web-kit#readme) there describes every package, and the
13
+ [changelog](https://github.com/RCooLeR/web-kit/blob/main/CHANGELOG.md) lists the changes.
14
+
15
+ ## Licence
16
+
17
+ [MIT](LICENSE)
@@ -0,0 +1,53 @@
1
+ /** The supported languages, Ukrainian first: it is the default. */
2
+ export declare const LOCALES: readonly ["uk", "en"];
3
+ export type Locale = (typeof LOCALES)[number];
4
+ /**
5
+ * The supported locale a language tag belongs to: "uk-UA" and "UK" give "uk". Anything else,
6
+ * including a missing tag, gives `fallback`.
7
+ */
8
+ export declare function toLocale(value: string | undefined, fallback?: Locale): Locale;
9
+ /** Values for the `{name}` placeholders of a text. */
10
+ export type Values = Readonly<Record<string, string | number>>;
11
+ /**
12
+ * Fills `{name}` placeholders. A placeholder without a value is left visible: a missing value is
13
+ * a bug, and a visible one gets fixed.
14
+ */
15
+ export declare function fill(template: string, values?: Values): string;
16
+ /** All texts of one language. */
17
+ export type Dictionary<Key extends string> = Readonly<Record<Key, string>>;
18
+ /** The texts of every supported language, with the same keys in each. */
19
+ export type Dictionaries<Key extends string> = Readonly<Record<Locale, Dictionary<Key>>>;
20
+ /** The key every dictionary needs: the text for a failure nobody wrote a text for. */
21
+ export declare const UNKNOWN_ERROR_KEY = "error.unknown";
22
+ /** The bases that have plural forms: every key ending in ".other", without that ending. */
23
+ export type PluralBase<Key extends string> = Key extends `${infer Base}.other` ? Base : never;
24
+ /**
25
+ * What a part of the UI needs to speak one language. The members are plain functions, so they can
26
+ * be taken apart: `const { t, errorText } = translator`.
27
+ */
28
+ export interface Translator<Key extends string> {
29
+ readonly locale: Locale;
30
+ /** The text for a key, with its placeholders filled. */
31
+ readonly t: (key: Key, values?: Values) => string;
32
+ /** The text for a key built from data (an event type a server sent), or undefined when there is none. */
33
+ readonly lookup: (key: string) => string | undefined;
34
+ /**
35
+ * The text for a failed call, chosen by the error's `code` through the key `error.<code>`. An
36
+ * error without a code, or with one that has no text, gives `error.unknown`. The error's own
37
+ * message is never shown: it may come from a server and is not written for users.
38
+ */
39
+ readonly errorText: (error: unknown) => string;
40
+ /**
41
+ * The text for a count in the language's plural form, from the key `<base>.<category>` with the
42
+ * category of Intl.PluralRules (uk: one, few, many, other; en: one, other). A missing form falls
43
+ * back to `<base>.other`. `{count}` is filled with the count unless `values` sets it.
44
+ */
45
+ readonly plural: (base: PluralBase<Key>, count: number, values?: Values) => string;
46
+ }
47
+ /** The stable code of a failed call, when the error carries one (as `ApiError` of @rcooler/http does). */
48
+ export declare function errorCode(error: unknown): string | undefined;
49
+ /**
50
+ * Returns the translator of one language over the app's dictionaries. The dictionaries must
51
+ * contain `error.unknown`; leaving it out is a compile error.
52
+ */
53
+ export declare function createTranslator<Key extends string>(dictionaries: Dictionaries<Key> & Dictionaries<typeof UNKNOWN_ERROR_KEY>, locale: Locale): Translator<Key>;
package/dist/index.js ADDED
@@ -0,0 +1,56 @@
1
+ // @rcooler/i18n-core: texts in Ukrainian and English, with keys the compiler checks. No UI framework: a React provider or a Lit controller wraps `createTranslator`.
2
+ //
3
+ // The app owns its dictionaries. English is the contract: `type MessageKey = keyof typeof en`, and
4
+ // declaring `uk` as `Dictionary<MessageKey>` makes a missing or misspelled key a compile error.
5
+ // Two key conventions are fixed here because the helpers build keys from data:
6
+ // `error.<code>` (with `error.unknown` as the fallback) and `<base>.<plural category>`. Since
7
+ // English is the contract, it also lists the plural forms only Ukrainian selects (few, many).
8
+ /** The supported languages, Ukrainian first: it is the default. */
9
+ export const LOCALES = ["uk", "en"];
10
+ /**
11
+ * The supported locale a language tag belongs to: "uk-UA" and "UK" give "uk". Anything else,
12
+ * including a missing tag, gives `fallback`.
13
+ */
14
+ export function toLocale(value, fallback = "uk") {
15
+ return LOCALES.find((locale) => value?.toLowerCase().startsWith(locale)) ?? fallback;
16
+ }
17
+ /**
18
+ * Fills `{name}` placeholders. A placeholder without a value is left visible: a missing value is
19
+ * a bug, and a visible one gets fixed.
20
+ */
21
+ export function fill(template, values = {}) {
22
+ return template.replace(/\{(\w+)\}/g, (placeholder, name) => name in values ? String(values[name]) : placeholder);
23
+ }
24
+ /** The key every dictionary needs: the text for a failure nobody wrote a text for. */
25
+ export const UNKNOWN_ERROR_KEY = "error.unknown";
26
+ /** The stable code of a failed call, when the error carries one (as `ApiError` of @rcooler/http does). */
27
+ export function errorCode(error) {
28
+ if (typeof error !== "object" || error === null || !("code" in error))
29
+ return undefined;
30
+ return typeof error.code === "string" ? error.code : undefined;
31
+ }
32
+ /**
33
+ * Returns the translator of one language over the app's dictionaries. The dictionaries must
34
+ * contain `error.unknown`; leaving it out is a compile error.
35
+ */
36
+ export function createTranslator(dictionaries, locale) {
37
+ const dictionary = dictionaries[locale];
38
+ const lookup = (key) => (Object.hasOwn(dictionary, key) ? dictionary[key] : undefined);
39
+ const unknownError = dictionaries[locale][UNKNOWN_ERROR_KEY];
40
+ const pluralRules = new Intl.PluralRules(locale);
41
+ return {
42
+ locale,
43
+ // A key the types allowed but the dictionary lacks can only come from a cast; showing the
44
+ // key itself makes that visible.
45
+ t: (key, values) => fill(lookup(key) ?? key, values),
46
+ lookup,
47
+ errorText: (error) => {
48
+ const code = errorCode(error);
49
+ return (code === undefined ? undefined : lookup(`error.${code}`)) ?? unknownError;
50
+ },
51
+ plural: (base, count, values) => {
52
+ const template = lookup(`${base}.${pluralRules.select(count)}`) ?? lookup(`${base}.other`) ?? base;
53
+ return fill(template, { count, ...values });
54
+ },
55
+ };
56
+ }
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@rcooler/i18n-core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Typed uk and en dictionaries, placeholders, plural forms and error texts by code; no UI framework.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "@rcooler/source": "./src/index.ts",
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "engines": {
19
+ "node": ">=24"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/RCooLeR/web-kit.git",
24
+ "directory": "packages/i18n-core"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "registry": "https://registry.npmjs.org"
29
+ },
30
+ "scripts": {
31
+ "build": "node ../../node_modules/@typescript/native/bin/tsc -p tsconfig.build.json",
32
+ "typecheck": "node ../../node_modules/@typescript/native/bin/tsc --noEmit -p tsconfig.json"
33
+ }
34
+ }