Last active
June 2, 2020 09:39
-
-
Save agc93/87cbf49fb703f49a8bf04d99bfc24698 to your computer and use it in GitHub Desktop.
Extension-specific translation overrides for Vortex: don't do this
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
import * as path from 'path'; | |
import { fs, log } from "vortex-api"; | |
import { IExtensionApi } from "vortex-api/lib/types/api"; | |
type TranslationLanguage = {code: string, content: string}; | |
/** | |
* Loads extension-specific translation files directly into `i18next` | |
* | |
* @remarks | |
* - A note for other extension authors: **DON'T DO THIS**. | |
* - See https://github.com/Nexus-Mods/Vortex/issues/6311 for why this is a bad idea | |
* - I'm leaving this here because I'm wilfully breaking things, but please don't do this. | |
* | |
* @param api The extension API | |
* @param extName The extension name to load translations for. | |
* @param ns The namespace to load translations into. Defaults to @extName | |
*/ | |
async function addTranslations(api: IExtensionApi, extName: string, ns?: string) : Promise<void> { | |
ns = ns ?? extName; | |
var ext = api.getLoadedExtensions().find(e => e.name == extName); | |
if (ext) { | |
var re = new RegExp(/^language_([a-z]{2}\b(-[a-z]{2})?)\.json/); | |
fs.readdirAsync(ext.path) | |
.then((files: string[]) => { | |
return files.filter(f => re.test(f)); | |
}) | |
.map((f: string): TranslationLanguage => { | |
var match = re.exec(f); | |
var lang = match[1]; | |
log('debug', 'loading translation file', {file: f, lang}); | |
return { code: match[1], content: fs.readFileSync(path.join(ext.path, lang), { encoding: 'utf-8'})} | |
}) | |
.forEach((lang: TranslationLanguage) => { | |
api.getI18n().addResources(lang.code, ns, JSON.parse(lang.content)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment