in src/node/main.ts [380:451]
export function loadMessageBundle(file?: string): LocalizeFunc {
if (!file) {
// No file. We are in dev mode. Return the default
// localize function.
return localize;
}
// Remove extension since we load json files.
let ext = path.extname(file);
if (ext) {
file = file.substr(0, file.length - ext.length);
}
if (options.messageFormat === MessageFormat.both || options.messageFormat === MessageFormat.bundle) {
let headerFile = tryFindMetaDataHeaderFile(file);
if (headerFile) {
let bundlePath = path.dirname(headerFile);
let bundle: LanguageBundle = resolvedBundles[bundlePath];
if (bundle === undefined) {
try {
let header: MetadataHeader = JSON.parse(fs.readFileSync(headerFile, 'utf8'));
try {
let nlsBundle = loadNlsBundle(header, bundlePath);
bundle = cacheBundle(bundlePath, nlsBundle ? { header, nlsBundle } : null);
} catch (err) {
console.error('Failed to load nls bundle', err);
bundle = cacheBundle(bundlePath, null);
}
} catch (err) {
console.error('Failed to read header file', err);
bundle = cacheBundle(bundlePath, null);
}
}
if (bundle) {
let module = file.substr(bundlePath.length + 1).replace(/\\/g, '/');
let messages = bundle.nlsBundle[module];
if (messages === undefined) {
console.error(`Messages for file ${file} not found. See console for details.`);
return function (): string {
return 'Messages not found.';
};
}
return createScopedLocalizeFunction(messages);
}
}
}
if (options.messageFormat === MessageFormat.both || options.messageFormat === MessageFormat.file) {
// Try to load a single file bundle
try {
let json: SingleFileJsonFormat = readJsonFileSync(resolveLanguage(file));
if (Array.isArray(json)) {
return createScopedLocalizeFunction(json);
} else {
if (isDefined(json.messages) && isDefined(json.keys)) {
return createScopedLocalizeFunction(json.messages);
} else {
console.error(`String bundle '${file}' uses an unsupported format.`);
return function () {
return 'File bundle has unsupported format. See console for details';
};
}
}
} catch (err) {
if (err.code !== 'ENOENT') {
console.error('Failed to load single file bundle', err);
}
}
}
console.error(`Failed to load message bundle for file ${file}`);
return function (): string {
return 'Failed to load message bundle. See console for details.';
};
}