Formatting and typing fixes. Preserves comments as string[]
This commit is contained in:
Родитель
8863e1cf4c
Коммит
67e106a2f2
29
src/lib.ts
29
src/lib.ts
|
@ -53,7 +53,7 @@ export interface LocalizeInfo {
|
|||
}
|
||||
|
||||
export namespace LocalizeInfo {
|
||||
export function is(value: any): value is JavaScriptMessageBundle {
|
||||
export function is(value: any): value is LocalizeInfo {
|
||||
let candidate = value as LocalizeInfo;
|
||||
return candidate && isDefined(candidate.key) && isDefined(candidate.comment);
|
||||
}
|
||||
|
@ -752,33 +752,32 @@ export function createLocalizedMessages(filename: string, bundle: ResolvedJavaSc
|
|||
}
|
||||
}
|
||||
|
||||
export function bundle2kvp(bundle: JavaScriptMessageBundle): any{
|
||||
let kvpObject = {};
|
||||
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 isLocalizeInfo: boolean = LocalizeInfo.is(bundle.keys[i]);
|
||||
let keyInfo = bundle.keys[i];
|
||||
|
||||
if (isLocalizeInfo) {
|
||||
key = (<LocalizeInfo>bundle.keys[i]).key;
|
||||
comments = (<LocalizeInfo>bundle.keys[i]).comment;
|
||||
if (LocalizeInfo.is(keyInfo)) {
|
||||
key = keyInfo.key;
|
||||
comments = keyInfo.comment;
|
||||
} else {
|
||||
key = <string>bundle.keys[i];
|
||||
key = keyInfo;
|
||||
}
|
||||
|
||||
if(key in kvpObject)
|
||||
{
|
||||
throw new Error("The following key is duplicated: \"" + key + "\". Please use unique keys.");
|
||||
if (key in result) {
|
||||
throw new Error(`The following key is duplicated: "${key}". Please use unique keys.`);
|
||||
}
|
||||
|
||||
kvpObject[key] = bundle.messages[i];
|
||||
result[key] = bundle.messages[i];
|
||||
|
||||
if (isLocalizeInfo) {
|
||||
kvpObject["_"+key+".comment"] = comments.join(' ');
|
||||
if (comments && !dropComments) {
|
||||
result[`_${key}.comments`] = comments;
|
||||
}
|
||||
}
|
||||
|
||||
return kvpObject;
|
||||
return result;
|
||||
}
|
23
src/main.ts
23
src/main.ts
|
@ -10,7 +10,7 @@ import { ThroughStream } from 'through';
|
|||
import { through } from 'event-stream';
|
||||
import File = require('vinyl');
|
||||
|
||||
import { KeyInfo, JavaScriptMessageBundle, processFile, resolveMessageBundle, createLocalizedMessages, bundle2kvp } from './lib';
|
||||
import { KeyInfo, JavaScriptMessageBundle, processFile, resolveMessageBundle, createLocalizedMessages, bundle2keyValuePair } from './lib';
|
||||
|
||||
var util = require('gulp-util');
|
||||
|
||||
|
@ -118,7 +118,7 @@ export function createAdditionalLanguageFiles(languages: string[], i18nBaseDir:
|
|||
});
|
||||
}
|
||||
|
||||
export function createKeyValuePairFile(): ThroughStream {
|
||||
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)) {
|
||||
|
@ -136,21 +136,20 @@ export function createKeyValuePairFile(): ThroughStream {
|
|||
this.emit('data', file);
|
||||
return;
|
||||
}
|
||||
let kvpObject = bundle2kvp(resolvedBundle);
|
||||
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 Valid JSON: ${file.relative}`)
|
||||
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 component file: ${file.relative}`)
|
||||
this.emit('error', `Failed to read JavaScript message bundle file: ${file.relative}`)
|
||||
}
|
||||
this.emit('data', file);
|
||||
if(kvpFile) {
|
||||
if (kvpFile) {
|
||||
this.emit('data', kvpFile);
|
||||
}
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче