@localness/contract 0.4.2 → 0.4.3
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 +11 -0
- package/dist/locale-file.d.mts +61 -0
- package/dist/locale-file.mjs +1389 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -10,6 +10,17 @@ const meta: DeliveryMeta = deliveryMetaSchema.parse(await res.json())
|
|
|
10
10
|
|
|
11
11
|
`zod` ^4 is a peer dependency.
|
|
12
12
|
|
|
13
|
+
## Locale files
|
|
14
|
+
|
|
15
|
+
`@localness/contract/locale-file` reads and writes one-JSON-per-locale files exactly as Localness git-sync does: flat or nested keys, edits in place (key order, indentation, comments and line endings are kept), new keys appended. The `localness` CLI uses it, so a file written by either one is identical. It does not need `zod`.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { readLocaleFile, writeLocaleFile } from '@localness/contract/locale-file'
|
|
19
|
+
|
|
20
|
+
const { entries } = readLocaleFile(source)
|
|
21
|
+
const next = writeLocaleFile(source, { 'home.title': 'Welcome', 'old.key': null }, 'nested-json')
|
|
22
|
+
```
|
|
23
|
+
|
|
13
24
|
## Versioning
|
|
14
25
|
|
|
15
26
|
Independent of the Localness server version.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One JSON file per locale, as git-sync's `locale-files` layout and the
|
|
3
|
+
* `localness` CLI read and write it. Both go through this module, so a file
|
|
4
|
+
* written by either one is byte-for-byte what the other would write.
|
|
5
|
+
*
|
|
6
|
+
* Package subpath `@localness/contract/locale-file`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOCALE_PLACEHOLDER = "{locale}";
|
|
9
|
+
export declare const DEFAULT_LOCALE_FILE_PATTERN = "{locale}.json";
|
|
10
|
+
/** Nested JSON joins its path with this. */
|
|
11
|
+
export declare const LOCALE_KEY_SEPARATOR = ".";
|
|
12
|
+
/** Key layout a write uses for a new key. Reading accepts both. */
|
|
13
|
+
export declare const LocaleFileFormat: {
|
|
14
|
+
readonly FLAT_JSON: "flat-json";
|
|
15
|
+
readonly NESTED_JSON: "nested-json";
|
|
16
|
+
};
|
|
17
|
+
export type LocaleFileFormat = (typeof LocaleFileFormat)[keyof typeof LocaleFileFormat];
|
|
18
|
+
/**
|
|
19
|
+
* The text to store, or null for "no translation". Only line endings and the
|
|
20
|
+
* ends are touched: inner whitespace is copy.
|
|
21
|
+
*/
|
|
22
|
+
export declare function normalizeLocaleText(value: unknown): string | null;
|
|
23
|
+
/** No prototype, so a key named `__proto__` is stored like any other. */
|
|
24
|
+
export declare function emptyEntries(): Record<string, string>;
|
|
25
|
+
/** Error text for an unusable directory / pattern, or null. */
|
|
26
|
+
export declare function localeFilesScopeError(dir: string, pattern: string): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* `zh_cn` → `zh-CN`, `en` → `en`: the BCP 47 shape app repos name files by.
|
|
29
|
+
* An explicit `localeMap` entry wins.
|
|
30
|
+
*/
|
|
31
|
+
export declare function localeFileName(locale: string, localeMap: Record<string, string> | null): string;
|
|
32
|
+
/** `/`-separated path of a locale's file. `dir` must pass `localeFilesScopeError`. */
|
|
33
|
+
export declare function localeFilePath(dir: string, pattern: string, locale: string, localeMap: Record<string, string> | null): string;
|
|
34
|
+
/**
|
|
35
|
+
* Keys are read the same way Translations import reads them — nested objects
|
|
36
|
+
* joined with `.` — and every key remembers the property path it came from, so
|
|
37
|
+
* a write lands on that exact property even in a file that mixes flat and
|
|
38
|
+
* nested keys.
|
|
39
|
+
*/
|
|
40
|
+
export type LocaleFileContent = {
|
|
41
|
+
entries: Record<string, string>;
|
|
42
|
+
paths: Map<string, string[]>;
|
|
43
|
+
/** Paths holding something other than an object or text (arrays, null). */
|
|
44
|
+
blocked: string[][];
|
|
45
|
+
};
|
|
46
|
+
export declare function readLocaleFile(source: string): LocaleFileContent;
|
|
47
|
+
/**
|
|
48
|
+
* Keys a write cannot place. A key already in the file is always writable, in
|
|
49
|
+
* place. A new nested key is not when its path runs into an existing value
|
|
50
|
+
* (`a` is text, so `a.b` has nowhere to go), would replace an object, has an
|
|
51
|
+
* empty segment, or collides that way with another new key — then neither of
|
|
52
|
+
* the two is written, since there is no right one to pick.
|
|
53
|
+
*/
|
|
54
|
+
export declare function unwritableKeys(file: LocaleFileContent, keys: string[], format: LocaleFileFormat): Set<string>;
|
|
55
|
+
/**
|
|
56
|
+
* Sets each entry in place and leaves everything else byte-for-byte: key
|
|
57
|
+
* order, indentation, comments and line endings. A new key goes to the end of
|
|
58
|
+
* its parent object. A `null` value removes the key. `source` null creates the
|
|
59
|
+
* file. Callers must drop `unwritableKeys` first.
|
|
60
|
+
*/
|
|
61
|
+
export declare function writeLocaleFile(source: string | null, entries: Record<string, string | null>, format: LocaleFileFormat): string;
|