This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f989a64
commit da8a4a3
Showing
3 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require("fs"); | ||
const util = require("util"); | ||
const path = require("path"); | ||
|
||
const rdir = util.promisify(fs.readdir); | ||
const read = util.promisify(fs.readFile); | ||
const write = util.promisify(fs.writeFile); | ||
|
||
const encoding = 'utf8'; | ||
|
||
const fixLang = async (base, lang, placeholders) => { | ||
const file = path.join(__dirname, base, lang, 'messages.json'); | ||
const source = await read(file, { encoding }); | ||
const messages = JSON.parse(source); | ||
for(const k in placeholders) { | ||
messages[k].placeholders = placeholders[k]; | ||
} | ||
|
||
return write(file, JSON.stringify(messages, null, 4)); | ||
}; | ||
|
||
const main = async (base, defaultLang = "en") => { | ||
let files = await rdir(path.join(__dirname, base)); | ||
const placeholders = {}; | ||
if(!files.includes(defaultLang)) { | ||
throw new Error(`Missing default language ${defaultLang}`); | ||
} | ||
|
||
const source = await read(path.join(__dirname, base, defaultLang, 'messages.json'), { encoding }); | ||
const messages = JSON.parse(source); | ||
for(const k in messages) { | ||
if("placeholders" in messages[k]) { | ||
placeholders[k] = messages[k].placeholders; | ||
} | ||
} | ||
|
||
files = files.filter((f) => f !== defaultLang); | ||
|
||
return Promise.all(files.map((f) => fixLang(base, f, placeholders))) | ||
}; | ||
|
||
main('../webextension/_locales'); |