Merge branch 'bjacobs3-master'

This commit is contained in:
Dirk Baeumer 2016-05-20 10:26:27 +02:00
Родитель 4661511ffb 67e106a2f2
Коммит 5b7b01ca75
2 изменённых файлов: 76 добавлений и 1 удалений

Просмотреть файл

@ -52,6 +52,13 @@ export interface LocalizeInfo {
comment: string[];
}
export namespace LocalizeInfo {
export function is(value: any): value is LocalizeInfo {
let candidate = value as LocalizeInfo;
return candidate && isDefined(candidate.key) && isDefined(candidate.comment);
}
}
export type KeyInfo = string | LocalizeInfo;
export interface JavaScriptMessageBundle {
@ -743,4 +750,34 @@ export function createLocalizedMessages(filename: string, bundle: ResolvedJavaSc
} else {
return { messages: PackageJsonMessageBundle.asTranslatedMessages(bundle, messages, problems), problems };
}
}
export function bundle2keyValuePair(bundle: JavaScriptMessageBundle, dropComments: boolean = false): any {
let result = Object.create(null);
for(var i=0; i < bundle.messages.length; ++i) {
let key: string;
let comments: string[];
let message: string = bundle.messages[i];
let keyInfo = bundle.keys[i];
if (LocalizeInfo.is(keyInfo)) {
key = keyInfo.key;
comments = keyInfo.comment;
} else {
key = keyInfo;
}
if (key in result) {
throw new Error(`The following key is duplicated: "${key}". Please use unique keys.`);
}
result[key] = bundle.messages[i];
if (comments && !dropComments) {
result[`_${key}.comments`] = comments;
}
}
return result;
}

Просмотреть файл

@ -10,7 +10,7 @@ import { ThroughStream } from 'through';
import { through } from 'event-stream';
import File = require('vinyl');
import { KeyInfo, JavaScriptMessageBundle, processFile, resolveMessageBundle, createLocalizedMessages } from './lib';
import { KeyInfo, JavaScriptMessageBundle, processFile, resolveMessageBundle, createLocalizedMessages, bundle2keyValuePair } from './lib';
var util = require('gulp-util');
@ -23,6 +23,7 @@ interface FileWithSourceMap extends File {
}
const NLS_JSON = '.nls.json';
const I18N_JSON = '.i18n.json';
export function rewriteLocalizeCalls(): ThroughStream {
return through(
@ -115,4 +116,41 @@ export function createAdditionalLanguageFiles(languages: string[], i18nBaseDir:
}
this.emit('data', file);
});
}
export function createKeyValuePairFile(dropComments: boolean = false): ThroughStream {
return through(function(file: File) {
let basename = path.basename(file.relative);
if (basename.length < NLS_JSON.length || NLS_JSON !== basename.substr(basename.length - NLS_JSON.length)) {
this.emit('data', file);
return;
}
let json;
let kvpFile;
let filename = file.relative.substr(0, file.relative.length - NLS_JSON.length);
if (file.isBuffer()) {
json = JSON.parse(file.contents.toString('utf8'));
if (JavaScriptMessageBundle.is(json)) {
let resolvedBundle = json as JavaScriptMessageBundle;
if (resolvedBundle.messages.length !== resolvedBundle.keys.length) {
this.emit('data', file);
return;
}
let kvpObject = bundle2keyValuePair(resolvedBundle, dropComments);
kvpFile = new File({
base: file.base,
path: path.join(file.base, filename) + I18N_JSON,
contents: new Buffer(JSON.stringify(kvpObject, null, '\t'), 'utf8')
});
} else {
this.emit('error', `Not a valid JavaScript message bundle: ${file.relative}`)
}
} else {
this.emit('error', `Failed to read JavaScript message bundle file: ${file.relative}`)
}
this.emit('data', file);
if (kvpFile) {
this.emit('data', kvpFile);
}
});
}