-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgetAppTranslations.ts
More file actions
41 lines (33 loc) · 1.16 KB
/
getAppTranslations.ts
File metadata and controls
41 lines (33 loc) · 1.16 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
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { Q } from '@nozbe/watermelondb';
import database from '../database';
import log from './helpers/log';
import protectedFunction from './helpers/protectedFunction';
import sdk from '../services/sdk';
export async function getAppTranslations(language = 'en'): Promise<void> {
try {
const db = database.active;
const result = await sdk.get('apps.translations', { language });
if (!result?.success || !result.translations) {
return;
}
await db.write(async () => {
const collection = db.get('app_translations');
const existing = await collection.query(Q.where('language', result.language)).fetch();
const toDelete = existing.map((r: any) => r.prepareDestroyPermanently());
const toCreate = Object.entries(result.translations).map(([key, value]) =>
collection.prepareCreate(
protectedFunction((r: any) => {
r._raw = sanitizedRaw({ id: `${result.language}_${key}` }, collection.schema);
r.key = key;
r.value = value as string;
r.language = result.language;
})
)
);
await db.batch(...toDelete, ...toCreate);
});
} catch (e) {
log(e);
}
}