UNPKG

1.07 kBPlain TextView Raw
1import fs from "fs"
2import _ from "lodash"
3import { importXlsx } from "./utils"
4import { updateLocalizedStrings } from "./utils"
5
6/**
7 * Export file to XLSX
8 * @param oldDataFile e.g. "localizations.json"
9 * @param xlsxFile path of file to export
10 * @param newDataFile can be the same as oldDataFile (different when testing)
11 */
12 export default function importLocalizationFileFromXlsx(oldDataFile: string, xlsxFile: string, newDataFile: string): void {
13 // Read the xlsx file and get the locales
14 const base64File = fs.readFileSync(xlsxFile, "base64")
15
16 const localizations = JSON.parse(fs.readFileSync(oldDataFile, "utf-8"))
17
18 const updates = importXlsx(localizations.locales, base64File)
19 updateLocalizedStrings(localizations.strings, updates)
20
21 // Sort by used
22 localizations.strings = _.sortBy(localizations.strings, function (l: any) {
23 if (l._unused) {
24 return 1
25 } else {
26 return 0
27 }
28 })
29
30 // Write the whole thing to a JSon file
31 fs.writeFileSync(newDataFile, JSON.stringify(localizations, null, 2), "utf-8")
32 return localizations
33}