-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n-config.ts
More file actions
62 lines (56 loc) · 1.59 KB
/
i18n-config.ts
File metadata and controls
62 lines (56 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import localesJson from "./lib/locales.json"; // Edge-safe static locales
interface ICtfLocale {
code: string;
name: string;
default: boolean;
fallbackCode: null;
sys: {
id: string;
type: string;
version: number;
};
}
export const getI18nConfig = async () => {
let locales: ICtfLocale[] = [];
try {
// Try to parse and validate locales.json
locales = localesJson as unknown as ICtfLocale[];
if (!Array.isArray(locales) || locales.length === 0) {
throw new Error("Locales array is empty or invalid");
}
// Basic validation: ensure each has a code
for (const loc of locales) {
if (!loc.code || typeof loc.code !== "string") {
throw new Error("Invalid locale structure");
}
}
} catch (error) {
console.warn(
"Warning: lib/locales.json is missing, empty, or invalid. Falling back to default locale 'en-US'. Re-run 'npm run build' to regenerate.",
error
);
// Fallback to a safe default
locales = [
{
code: "en-US",
name: "English (United States)",
default: true,
fallbackCode: null,
sys: {
id: "fallback",
type: "Locale",
version: 1,
},
},
];
}
const localeCodes = locales.map((locale: ICtfLocale) => locale.code);
return {
defaultLocale: localeCodes.includes("en-US") ? "en-US" : localeCodes[0],
locales: localeCodes,
} as const;
};
export type Locale = Awaited<
ReturnType<typeof getI18nConfig>
>["locales"][number];
// Note: middleware runs on Edge Runtime; avoid importing the Contentful SDK here.