This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port more build scripts to ES6, merge i18n management for client and …
…plugins
- Loading branch information
Showing
4 changed files
with
150 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
exports.rootLocalesPath = `${__dirname}/../public/locales`; | ||
exports.relativeLocalesPath = "./public/locales"; | ||
|
||
let fallbackLocale = null; | ||
|
||
exports.loadLocale = (languageCode, relative) => { | ||
if (fallbackLocale == null && languageCode !== "en") fallbackLocale = exports.loadLocale("en", relative); | ||
|
||
const localePath = relative ? exports.relativeLocalesPath : exports.rootLocalesPath; | ||
|
||
const namespaces = {}; | ||
if (relative) namespaces["common"] = JSON.parse(fs.readFileSync(`${exports.rootLocalesPath}/${languageCode}/common.json`, { encoding: "utf8" })); | ||
|
||
let filenames = []; | ||
try { filenames = fs.readdirSync(`${localePath}/${languageCode}`); } catch (err) { /* Ignore */ } | ||
for (const filename of filenames) { | ||
const file = fs.readFileSync(`${localePath}/${languageCode}/${filename}`, { encoding: "utf8" }); | ||
namespaces[filename.slice(0, filename.lastIndexOf("."))] = JSON.parse(file); | ||
} | ||
|
||
if (languageCode !== "en") reportMissingKeys(languageCode, namespaces); | ||
return namespaces; | ||
}; | ||
|
||
exports.makeT = (locale) => { | ||
return function t(path) { | ||
const parts = path.split(":"); | ||
let value = locale[parts[0]]; | ||
if (value == null) return path; | ||
|
||
const keys = parts[1].split("."); | ||
for (const key of keys) { | ||
value = value[key]; | ||
if (value == null) return path; | ||
} | ||
return value; | ||
} | ||
}; | ||
|
||
function reportMissingKeys(languageCode, locale) { | ||
const missingKeys = []; | ||
|
||
function checkRecursively(fallbackRoot, root, key, path) { | ||
if (root[key] == null) { | ||
missingKeys.push(path); | ||
root[key] = fallbackRoot[key]; | ||
} else if (typeof fallbackRoot[key] === "object") { | ||
const childKeys = Object.keys(fallbackRoot[key]); | ||
for (const childKey of childKeys) checkRecursively(fallbackRoot[key], root[key], childKey, `${path}.${childKey}`); | ||
} | ||
} | ||
|
||
const rootKeys = Object.keys(fallbackLocale); | ||
for (const rootKey of rootKeys) checkRecursively(fallbackLocale, locale, rootKey, rootKey); | ||
|
||
if (missingKeys.length > 0) console.log(`Missing keys in ${languageCode} locale: ${missingKeys.join(", ")}`); | ||
} |
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
Oops, something went wrong.