@gitpagedocs/tools 1.1.67 → 1.1.68
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 +2 -1
- package/package.json +2 -1
- package/src/i18n/index.ts +28 -0
- package/src/i18n/language-bundles.ts +204 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -15,11 +15,12 @@ The **shared business-logic core** of the [Git Page Docs](../README.md) monorepo
|
|
|
15
15
|
| `errors/` | `AppError` + typed subclasses |
|
|
16
16
|
| `filesystem/` | `FileService` (root-bounded list/read/write/search) |
|
|
17
17
|
| `documentation/` | `DocumentationService`, `patchManagedRegion`, `DocUpdater` |
|
|
18
|
+
| `i18n/` | Language bundles (`gitpagedocs/langs.json` + `langs/<lang>.json`): `loadLanguageBundles`, `applyLanguageBundles` (fold into legacy `site.langmenu` / `translations`), `splitLanguageBundles` (generator side); browser-safe |
|
|
18
19
|
| `ports/` | Type-only contracts (logger, cache, crypto, security, config, ai) |
|
|
19
20
|
|
|
20
21
|
## Entry points (`exports`)
|
|
21
22
|
|
|
22
|
-
`.` (full barrel) · `./ai` · `./errors` · `./ports` · `./crypto/web` · `./security/web` · `./cache/web` — the `web` subpaths are browser-safe (no `node:` imports) for the Next.js bundle.
|
|
23
|
+
`.` (full barrel) · `./ai` · `./errors` · `./i18n` · `./ports` · `./crypto/web` · `./security/web` · `./cache/web` — the `web` subpaths and `./i18n` are browser-safe (no `node:` imports) for the Next.js bundle.
|
|
23
24
|
|
|
24
25
|
## Usage
|
|
25
26
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gitpagedocs/tools",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.68",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shared business-logic core for Git Page Docs (consumed by frontend, cli and mcp). Ships TypeScript source; consume via tsx or a TS-aware bundler.",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
".": "./src/index.ts",
|
|
10
10
|
"./ai": "./src/ai/index.ts",
|
|
11
11
|
"./errors": "./src/errors/index.ts",
|
|
12
|
+
"./i18n": "./src/i18n/index.ts",
|
|
12
13
|
"./ports": "./src/ports/index.ts",
|
|
13
14
|
"./crypto/web": "./src/crypto/web.ts",
|
|
14
15
|
"./security/web": "./src/security/web.ts",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_LANGS_DIR,
|
|
3
|
+
DEFAULT_LANGS_MANIFEST_PATH,
|
|
4
|
+
LANGS_DIRNAME,
|
|
5
|
+
LANGS_MANIFEST_FILENAME,
|
|
6
|
+
applyLanguageBundles,
|
|
7
|
+
buildLanguageManifest,
|
|
8
|
+
getLanguageBundlePath,
|
|
9
|
+
isLanguageCode,
|
|
10
|
+
loadLanguageBundles,
|
|
11
|
+
parseLanguageBundle,
|
|
12
|
+
parseLanguageManifest,
|
|
13
|
+
splitLanguageBundles,
|
|
14
|
+
} from "./language-bundles";
|
|
15
|
+
export type {
|
|
16
|
+
InlineLangMenu,
|
|
17
|
+
InlineTranslationSection,
|
|
18
|
+
InlineTranslations,
|
|
19
|
+
JsonReader,
|
|
20
|
+
LanguageBundle,
|
|
21
|
+
LanguageBundleMap,
|
|
22
|
+
LanguageManifest,
|
|
23
|
+
LanguageStrings,
|
|
24
|
+
LoadLanguageBundlesOptions,
|
|
25
|
+
LocalizableConfig,
|
|
26
|
+
LocalizableSite,
|
|
27
|
+
TranslationSections,
|
|
28
|
+
} from "./language-bundles";
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language bundles — the UI strings of a gitpagedocs site, one JSON file per
|
|
3
|
+
* language, listed by a manifest:
|
|
4
|
+
*
|
|
5
|
+
* gitpagedocs/langs.json { "languages": ["en", "pt", "es"] }
|
|
6
|
+
* gitpagedocs/langs/<lang>.json { "langmenu": {...}, "translations": {...} }
|
|
7
|
+
*
|
|
8
|
+
* Historically these strings lived inline in `config.json` (`site.langmenu`
|
|
9
|
+
* keyed by language and `translations.<section>.<key>` keyed by language).
|
|
10
|
+
* `applyLanguageBundles` folds the bundles back into that legacy shape, so
|
|
11
|
+
* every consumer keeps reading `site.langmenu` / `translations` unchanged and
|
|
12
|
+
* old inline configs keep working. Bundles win over inline strings.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const LANGS_MANIFEST_FILENAME = "langs.json";
|
|
16
|
+
export const LANGS_DIRNAME = "langs";
|
|
17
|
+
export const DEFAULT_LANGS_MANIFEST_PATH = `gitpagedocs/${LANGS_MANIFEST_FILENAME}`;
|
|
18
|
+
export const DEFAULT_LANGS_DIR = `gitpagedocs/${LANGS_DIRNAME}`;
|
|
19
|
+
|
|
20
|
+
/** Language codes become file names, so they are kept to a safe alphabet. */
|
|
21
|
+
const LANGUAGE_CODE_PATTERN = /^[A-Za-z0-9]{2,8}(?:[-_][A-Za-z0-9]{1,8})?$/;
|
|
22
|
+
|
|
23
|
+
export type LanguageStrings = Record<string, string>;
|
|
24
|
+
export type TranslationSections = Record<string, LanguageStrings>;
|
|
25
|
+
|
|
26
|
+
export interface LanguageManifest {
|
|
27
|
+
languages: string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface LanguageBundle {
|
|
31
|
+
langmenu?: LanguageStrings;
|
|
32
|
+
translations?: TranslationSections;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type LanguageBundleMap = Record<string, LanguageBundle>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Legacy inline shapes: `langmenu[lang][key]` and `translations[section][key][lang]`.
|
|
39
|
+
* Sections and keys may be absent, matching the optional fields of typed configs.
|
|
40
|
+
*/
|
|
41
|
+
export type InlineLangMenu = Record<string, LanguageStrings>;
|
|
42
|
+
export type InlineTranslationSection = { [key: string]: LanguageStrings | undefined };
|
|
43
|
+
export type InlineTranslations = { [section: string]: InlineTranslationSection | undefined };
|
|
44
|
+
|
|
45
|
+
export interface LocalizableSite {
|
|
46
|
+
langmenu?: InlineLangMenu;
|
|
47
|
+
supportedLanguages?: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface LocalizableConfig {
|
|
51
|
+
site?: LocalizableSite;
|
|
52
|
+
translations?: InlineTranslations;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Reads a repo-relative JSON file; resolves null when it is missing or invalid. */
|
|
56
|
+
export type JsonReader = (relativePath: string) => Promise<unknown>;
|
|
57
|
+
|
|
58
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
59
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isStringRecord(value: unknown): value is LanguageStrings {
|
|
63
|
+
return isPlainObject(value) && Object.values(value).every((entry) => typeof entry === "string");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isLanguageCode(value: unknown): value is string {
|
|
67
|
+
return typeof value === "string" && LANGUAGE_CODE_PATTERN.test(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function getLanguageBundlePath(language: string, langsDir: string = DEFAULT_LANGS_DIR): string {
|
|
71
|
+
return `${langsDir}/${language}.json`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseLanguageManifest(raw: unknown): LanguageManifest | null {
|
|
75
|
+
if (!isPlainObject(raw) || !Array.isArray(raw.languages)) return null;
|
|
76
|
+
const languages = Array.from(new Set(raw.languages.filter(isLanguageCode)));
|
|
77
|
+
return languages.length > 0 ? { languages } : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseTranslationSections(raw: unknown): TranslationSections | undefined {
|
|
81
|
+
if (!isPlainObject(raw)) return undefined;
|
|
82
|
+
const sections: TranslationSections = {};
|
|
83
|
+
for (const [section, strings] of Object.entries(raw)) {
|
|
84
|
+
if (isStringRecord(strings)) sections[section] = strings;
|
|
85
|
+
}
|
|
86
|
+
return sections;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function parseLanguageBundle(raw: unknown): LanguageBundle | null {
|
|
90
|
+
if (!isPlainObject(raw)) return null;
|
|
91
|
+
const bundle: LanguageBundle = {};
|
|
92
|
+
if (isStringRecord(raw.langmenu)) bundle.langmenu = raw.langmenu;
|
|
93
|
+
const translations = parseTranslationSections(raw.translations);
|
|
94
|
+
if (translations) bundle.translations = translations;
|
|
95
|
+
return bundle;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface LoadLanguageBundlesOptions {
|
|
99
|
+
manifestPath?: string;
|
|
100
|
+
langsDir?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Loads the manifest and every bundle it lists. Resolves null when there is
|
|
105
|
+
* no manifest (legacy inline config); bundles that are missing or malformed
|
|
106
|
+
* are skipped so one broken file never takes the other languages down.
|
|
107
|
+
*/
|
|
108
|
+
export async function loadLanguageBundles(
|
|
109
|
+
readJson: JsonReader,
|
|
110
|
+
options: LoadLanguageBundlesOptions = {},
|
|
111
|
+
): Promise<LanguageBundleMap | null> {
|
|
112
|
+
const manifest = parseLanguageManifest(await readJson(options.manifestPath ?? DEFAULT_LANGS_MANIFEST_PATH));
|
|
113
|
+
if (!manifest) return null;
|
|
114
|
+
|
|
115
|
+
const entries = await Promise.all(
|
|
116
|
+
manifest.languages.map(async (language) => {
|
|
117
|
+
const bundle = parseLanguageBundle(await readJson(getLanguageBundlePath(language, options.langsDir)));
|
|
118
|
+
return [language, bundle] as const;
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const bundles: LanguageBundleMap = {};
|
|
123
|
+
for (const [language, bundle] of entries) {
|
|
124
|
+
if (bundle) bundles[language] = bundle;
|
|
125
|
+
}
|
|
126
|
+
return Object.keys(bundles).length > 0 ? bundles : null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function mergeLangMenu(inline: InlineLangMenu | undefined, bundles: LanguageBundleMap): InlineLangMenu {
|
|
130
|
+
const merged: InlineLangMenu = { ...inline };
|
|
131
|
+
for (const [language, bundle] of Object.entries(bundles)) {
|
|
132
|
+
merged[language] = { ...inline?.[language], ...bundle.langmenu };
|
|
133
|
+
}
|
|
134
|
+
return merged;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function cloneTranslations(inline: InlineTranslations | undefined): Record<string, InlineTranslationSection> {
|
|
138
|
+
const cloned: Record<string, InlineTranslationSection> = {};
|
|
139
|
+
for (const [section, keys] of Object.entries(inline ?? {})) {
|
|
140
|
+
if (!keys) continue;
|
|
141
|
+
cloned[section] = {};
|
|
142
|
+
for (const [key, byLanguage] of Object.entries(keys)) {
|
|
143
|
+
if (byLanguage) cloned[section][key] = { ...byLanguage };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return cloned;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function mergeTranslations(inline: InlineTranslations | undefined, bundles: LanguageBundleMap): InlineTranslations {
|
|
150
|
+
const merged = cloneTranslations(inline);
|
|
151
|
+
for (const [language, bundle] of Object.entries(bundles)) {
|
|
152
|
+
for (const [section, strings] of Object.entries(bundle.translations ?? {})) {
|
|
153
|
+
const target = (merged[section] ??= {});
|
|
154
|
+
for (const [key, text] of Object.entries(strings)) {
|
|
155
|
+
target[key] = { ...target[key], [language]: text };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return merged;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Folds bundles into the legacy inline shape. The manifest order defines
|
|
164
|
+
* `site.supportedLanguages`; inline strings survive only for keys a bundle
|
|
165
|
+
* does not provide.
|
|
166
|
+
*/
|
|
167
|
+
export function applyLanguageBundles<T extends LocalizableConfig>(config: T, bundles: LanguageBundleMap | null): T {
|
|
168
|
+
if (!bundles || Object.keys(bundles).length === 0) return config;
|
|
169
|
+
return {
|
|
170
|
+
...config,
|
|
171
|
+
site: {
|
|
172
|
+
...config.site,
|
|
173
|
+
langmenu: mergeLangMenu(config.site?.langmenu, bundles),
|
|
174
|
+
supportedLanguages: Object.keys(bundles),
|
|
175
|
+
},
|
|
176
|
+
translations: mergeTranslations(config.translations, bundles),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The generator-side inverse of `applyLanguageBundles`: one bundle per language. */
|
|
181
|
+
export function splitLanguageBundles(
|
|
182
|
+
languages: string[],
|
|
183
|
+
langmenu: InlineLangMenu,
|
|
184
|
+
translations: InlineTranslations,
|
|
185
|
+
): LanguageBundleMap {
|
|
186
|
+
const bundles: LanguageBundleMap = {};
|
|
187
|
+
for (const language of languages) {
|
|
188
|
+
const sections: TranslationSections = {};
|
|
189
|
+
for (const [section, keys] of Object.entries(translations)) {
|
|
190
|
+
const strings: LanguageStrings = {};
|
|
191
|
+
for (const [key, byLanguage] of Object.entries(keys ?? {})) {
|
|
192
|
+
const text = byLanguage?.[language];
|
|
193
|
+
if (typeof text === "string") strings[key] = text;
|
|
194
|
+
}
|
|
195
|
+
if (Object.keys(strings).length > 0) sections[section] = strings;
|
|
196
|
+
}
|
|
197
|
+
bundles[language] = { langmenu: { ...langmenu[language] }, translations: sections };
|
|
198
|
+
}
|
|
199
|
+
return bundles;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function buildLanguageManifest(languages: string[]): LanguageManifest {
|
|
203
|
+
return { languages: [...languages] };
|
|
204
|
+
}
|