Initial web
This commit is contained in:
29
web/lib/localeServer.ts
Normal file
29
web/lib/localeServer.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user