35 lines
940 B
TypeScript
35 lines
940 B
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { parseCsv } from './localization';
|
|
import type { LocaleMap } from './localization';
|
|
|
|
declare global {
|
|
var __serverLocaleCache: LocaleMap | undefined;
|
|
}
|
|
|
|
/**
|
|
* Loads and merges EN + DE locale CSVs from the public directory.
|
|
* Cached for the lifetime of the server process.
|
|
* Server-only — never import from client components.
|
|
*/
|
|
export function getServerLocaleMap(): LocaleMap {
|
|
if (globalThis.__serverLocaleCache) return globalThis.__serverLocaleCache;
|
|
|
|
const pub = path.join(process.cwd(), 'public');
|
|
|
|
function load(filename: string): LocaleMap {
|
|
try {
|
|
return parseCsv(fs.readFileSync(path.join(pub, filename), 'utf8'));
|
|
} catch {
|
|
return new Map();
|
|
}
|
|
}
|
|
|
|
const merged: LocaleMap = new Map([
|
|
...load('factorio_english_items.csv'),
|
|
...load('factorio_german_items.csv'),
|
|
]);
|
|
globalThis.__serverLocaleCache = merged;
|
|
return merged;
|
|
}
|