@pagefind/component-ui 1.5.0-alpha.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 +66 -0
- package/components/base-element.ts +110 -0
- package/components/index.ts +31 -0
- package/components/instance-manager.ts +91 -0
- package/components/pagefind-config.ts +44 -0
- package/components/pagefind-filter-dropdown.ts +702 -0
- package/components/pagefind-filter-pane.ts +525 -0
- package/components/pagefind-input.ts +224 -0
- package/components/pagefind-keyboard-hints.ts +62 -0
- package/components/pagefind-modal-body.ts +19 -0
- package/components/pagefind-modal-footer.ts +16 -0
- package/components/pagefind-modal-header.ts +59 -0
- package/components/pagefind-modal-trigger.ts +195 -0
- package/components/pagefind-modal.ts +209 -0
- package/components/pagefind-results.ts +586 -0
- package/components/pagefind-searchbox.ts +888 -0
- package/components/pagefind-summary.ts +138 -0
- package/core/announcer.ts +134 -0
- package/core/focus-utils.ts +89 -0
- package/core/instance.ts +714 -0
- package/core/translations.ts +79 -0
- package/css/pagefind-component-ui.css +1448 -0
- package/npm_dist/cjs/component-ui.cjs +6285 -0
- package/npm_dist/cjs/instance.cjs +2849 -0
- package/npm_dist/mjs/component-ui.mjs +6268 -0
- package/npm_dist/mjs/instance.mjs +2826 -0
- package/package.json +48 -0
- package/types-entry.ts +27 -0
- package/types.ts +126 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TranslationStrings,
|
|
3
|
+
TranslationFile,
|
|
4
|
+
TextDirection,
|
|
5
|
+
} from "../types";
|
|
6
|
+
|
|
7
|
+
// @ts-expect-error - glob import handled by esbuild-plugin-import-glob
|
|
8
|
+
import * as translationFiles from "../../translations/*.json";
|
|
9
|
+
import { parse as parseBCP47 } from "bcp-47";
|
|
10
|
+
|
|
11
|
+
const translations: Record<string, TranslationStrings> = {};
|
|
12
|
+
const filenames = translationFiles.filenames as string[];
|
|
13
|
+
const contents = translationFiles.default as TranslationFile[];
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < filenames.length; i++) {
|
|
16
|
+
const match = filenames[i].match(/([^\/]+)\.json$/);
|
|
17
|
+
if (!match) continue;
|
|
18
|
+
const lang = match[1];
|
|
19
|
+
translations[lang] = {
|
|
20
|
+
language: lang,
|
|
21
|
+
direction: contents[i].direction || "ltr",
|
|
22
|
+
...contents[i].strings,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get translations for a given language code.
|
|
28
|
+
* Uses BCP-47 parsing with fallback chain:
|
|
29
|
+
* 1. language-script-region (e.g., zh-Hans-CN)
|
|
30
|
+
* 2. language-region (e.g., en-US)
|
|
31
|
+
* 3. language (e.g., en)
|
|
32
|
+
* 4. English fallback
|
|
33
|
+
*/
|
|
34
|
+
export function getTranslations(langCode?: string): TranslationStrings {
|
|
35
|
+
if (!langCode) {
|
|
36
|
+
return translations["en"];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const parsed = parseBCP47(langCode.toLowerCase());
|
|
40
|
+
|
|
41
|
+
const keys: string[] = [];
|
|
42
|
+
if (parsed.language && parsed.script && parsed.region) {
|
|
43
|
+
keys.push(`${parsed.language}-${parsed.script}-${parsed.region}`);
|
|
44
|
+
}
|
|
45
|
+
if (parsed.language && parsed.region) {
|
|
46
|
+
keys.push(`${parsed.language}-${parsed.region}`);
|
|
47
|
+
}
|
|
48
|
+
if (parsed.language) {
|
|
49
|
+
keys.push(parsed.language);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const key of keys) {
|
|
53
|
+
if (translations[key]) {
|
|
54
|
+
return translations[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return translations["en"];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Interpolate placeholders in a translation string.
|
|
63
|
+
* Placeholders are in the format [PLACEHOLDER_NAME].
|
|
64
|
+
*/
|
|
65
|
+
export function interpolate(
|
|
66
|
+
str: string | undefined,
|
|
67
|
+
replacements: Record<string, string | number> = {},
|
|
68
|
+
): string {
|
|
69
|
+
if (!str) return "";
|
|
70
|
+
|
|
71
|
+
let result = str;
|
|
72
|
+
for (const [placeholder, value] of Object.entries(replacements)) {
|
|
73
|
+
result = result.replace(
|
|
74
|
+
new RegExp(`\\[${placeholder}\\]`, "g"),
|
|
75
|
+
String(value),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|