* Discovers all non-English locale directories under jsonDbDir, loads their * translation JSON files, validates them against the English source, and * upserts into `{collectionPrefix}translations`.
( db: Db, jsonDbDir: string, collectionPrefix: string )
| 243 | * upserts into `{collectionPrefix}translations`. |
| 244 | */ |
| 245 | async function uploadTranslationsFromFolder( |
| 246 | db: Db, |
| 247 | jsonDbDir: string, |
| 248 | collectionPrefix: string |
| 249 | ): Promise<void> { |
| 250 | const translationCollectionName = `${collectionPrefix}translations`; |
| 251 | const translationCollection = db.collection(translationCollectionName); |
| 252 | |
| 253 | try { |
| 254 | await translationCollection.drop(); |
| 255 | console.log(` Dropped existing collection '${translationCollectionName}'.`); |
| 256 | } catch (err) { |
| 257 | if (!(err instanceof MongoServerError && err.codeName === 'NamespaceNotFound')) { |
| 258 | throw err; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | const enDir = `${jsonDbDir}/en`; |
| 263 | if (!existsSync(enDir)) { |
| 264 | console.warn( |
| 265 | ` No English source directory at ${enDir}. Skipping translations for ${jsonDbDir}.` |
| 266 | ); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | let langDirs: string[]; |
| 271 | try { |
| 272 | langDirs = readdirSync(jsonDbDir, { withFileTypes: true }) |
| 273 | .filter( |
| 274 | (e) => |
| 275 | e.isDirectory() && |
| 276 | LOCALE_PATTERN.test(e.name) && |
| 277 | e.name !== 'en' && |
| 278 | !TRANSLATION_SKIP_DIRS.has(e.name) |
| 279 | ) |
| 280 | .map((e) => e.name); |
| 281 | } catch (e) { |
| 282 | console.error(`Error reading ${jsonDbDir}:`, e); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (langDirs.length === 0) { |
| 287 | console.log(` No translation directories found in ${jsonDbDir}.`); |
| 288 | await _refreshLocaleCollection(db, collectionPrefix, []); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | console.log(` Found translation languages: ${langDirs.join(', ')}`); |
| 293 | const translationDocs: TranslationDocument[] = []; |
| 294 | |
| 295 | for (const lang of langDirs) { |
| 296 | const docs = _processLangDir(lang, `${jsonDbDir}/${lang}`, enDir); |
| 297 | translationDocs.push(...docs); |
| 298 | } |
| 299 | |
| 300 | if (translationDocs.length > 0) { |
| 301 | await translationCollection.createIndex( |
| 302 | { source_collection: 1, source_index: 1, lang: 1 }, |
no test coverage detected