MCPcopy Create free account
hub / github.com/5e-bits/5e-database / _processLangDir

Function _processLangDir

scripts/dbRefresh.ts:155–207  ·  view source on GitHub ↗
(lang: string, langDir: string, enDir: string)

Source from the content-addressed store, hash-verified

153};
154
155function _processLangDir(lang: string, langDir: string, enDir: string): TranslationDocument[] {
156 const docs: TranslationDocument[] = [];
157 let langFiles: string[];
158 try {
159 langFiles = readdirSync(langDir) as string[];
160 } catch (e) {
161 console.error(`Error reading ${langDir}:`, e);
162 return docs;
163 }
164
165 for (const filename of langFiles) {
166 if (!filename.includes(SRD_PREFIX) || !filename.endsWith('.json')) continue;
167
168 const indexName = getIndexName(filename);
169 if (!indexName) continue;
170
171 let enData: Record<string, unknown>[];
172 try {
173 const raw = JSON.parse(readFileSync(`${enDir}/${filename}`, 'utf8'));
174 if (!Array.isArray(raw)) throw new Error('not an array');
175 enData = raw;
176 } catch (err) {
177 if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
178 console.warn(` No English source at ${enDir}/${filename}. Skipping ${lang}/${filename}.`);
179 } else {
180 console.warn(
181 ` Failed to parse English source ${enDir}/${filename}: ${err}. Skipping ${lang}/${filename}.`
182 );
183 }
184 continue;
185 }
186
187 const enMap = buildIndexMap(enData);
188
189 let transData: Record<string, unknown>[];
190 try {
191 const raw = JSON.parse(readFileSync(`${langDir}/${filename}`, 'utf8'));
192 if (!Array.isArray(raw)) throw new Error('not an array');
193 transData = raw;
194 } catch (err) {
195 console.error(` Error parsing ${langDir}/${filename}:`, err);
196 continue;
197 }
198
199 console.log(
200 ` Processing ${lang} translations for '${indexName}' (${transData.length} entries)...`
201 );
202
203 docs.push(...processTranslationEntries(transData, enMap, indexName, lang, filename));
204 }
205
206 return docs;
207}
208
209async function _refreshLocaleCollection(
210 db: Db,

Callers 1

Calls 3

getIndexNameFunction · 0.90
buildIndexMapFunction · 0.90

Tested by

no test coverage detected