mirror of
https://github.com/wassname/talk.git
synced 2026-07-25 13:30:59 +08:00
Fix tests
This commit is contained in:
@@ -5,6 +5,31 @@ import path from "path";
|
||||
// These locale prefixes are always loaded.
|
||||
const commonPrefixes = ["common", "framework"];
|
||||
|
||||
function decorateErrorWhenMissing(bundle: FluentBundle) {
|
||||
const originalHasMessage = bundle.hasMessage;
|
||||
const originalGetMessage = bundle.getMessage;
|
||||
const missing: string[] = [];
|
||||
bundle.hasMessage = (id: string) => {
|
||||
const result = originalHasMessage.apply(bundle, [id]);
|
||||
if (!result) {
|
||||
const msg = `${bundle.locales} translation for key "${id}" not found`;
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(msg);
|
||||
missing.push(id);
|
||||
}
|
||||
// Even if it is missing, we say it is available and later return a descriptive error
|
||||
// string as the translation.
|
||||
return true;
|
||||
};
|
||||
bundle.getMessage = (id: string) => {
|
||||
if (missing.indexOf(id) !== -1) {
|
||||
return `Missing translation "${id}"`;
|
||||
}
|
||||
return originalGetMessage.apply(bundle, [id]);
|
||||
};
|
||||
return bundle;
|
||||
}
|
||||
|
||||
function createFluentBundle(
|
||||
target: string,
|
||||
pathToLocale: string
|
||||
@@ -15,13 +40,11 @@ function createFluentBundle(
|
||||
files.forEach(f => {
|
||||
prefixes.forEach(prefix => {
|
||||
if (f.startsWith(prefix)) {
|
||||
bundle.addMessages(
|
||||
fs.readFileSync(path.resolve(pathToLocale, f)).toString()
|
||||
);
|
||||
bundle.addMessages(require(path.resolve(pathToLocale, f)));
|
||||
}
|
||||
});
|
||||
});
|
||||
return bundle;
|
||||
return decorateErrorWhenMissing(bundle);
|
||||
}
|
||||
|
||||
export default createFluentBundle;
|
||||
|
||||
Reference in New Issue
Block a user