feat(compiler-cli): add a locale option to ng-xi18n

Fixes #12303
Closes #14537
This commit is contained in:
Marc Laval
2017-02-16 17:03:18 +01:00
committed by Igor Minar
parent e99d721612
commit 234f05996c
17 changed files with 47 additions and 29 deletions

View File

@ -86,7 +86,8 @@ export class Extractor {
});
}
static create(host: ExtractorHost): {extractor: Extractor, staticReflector: StaticReflector} {
static create(host: ExtractorHost, locale: string|null):
{extractor: Extractor, staticReflector: StaticReflector} {
const htmlParser = new I18NHtmlParser(new HtmlParser());
const urlResolver = createOfflineCompileUrlResolver();
@ -112,7 +113,7 @@ export class Extractor {
symbolCache, staticReflector);
// TODO(vicb): implicit tags & attributes
const messageBundle = new MessageBundle(htmlParser, [], {});
const messageBundle = new MessageBundle(htmlParser, [], {}, locale);
const extractor = new Extractor(host, staticSymbolResolver, messageBundle, resolver);
return {extractor, staticReflector};

View File

@ -23,7 +23,7 @@ export class MessageBundle {
constructor(
private _htmlParser: HtmlParser, private _implicitTags: string[],
private _implicitAttrs: {[k: string]: string[]}) {}
private _implicitAttrs: {[k: string]: string[]}, private _locale: string|null = null) {}
updateFromTemplate(html: string, url: string, interpolationConfig: InterpolationConfig):
ParseError[] {
@ -67,7 +67,7 @@ export class MessageBundle {
return new i18n.Message(nodes, {}, {}, src.meaning, src.description, id);
});
return serializer.write(msgList);
return serializer.write(msgList, this._locale);
}
}
@ -92,4 +92,4 @@ class MapPlaceholderNames extends i18n.CloneVisitor {
visitIcuPlaceholder(ph: i18n.IcuPlaceholder, mapper: PlaceholderMapper): i18n.IcuPlaceholder {
return new i18n.IcuPlaceholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
}
}
}

View File

@ -12,7 +12,7 @@ export abstract class Serializer {
// - The `placeholders` and `placeholderToMessage` properties are irrelevant in the input messages
// - The `id` contains the message id that the serializer is expected to use
// - Placeholder names are already map to public names using the provided mapper
abstract write(messages: i18n.Message[]): string;
abstract write(messages: i18n.Message[], locale: string|null): string;
abstract load(content: string, url: string):
{locale: string | null, i18nNodesByMsgId: {[msgId: string]: i18n.Node[]}};

View File

@ -18,7 +18,7 @@ import * as xml from './xml_helper';
const _VERSION = '1.2';
const _XMLNS = 'urn:oasis:names:tc:xliff:document:1.2';
// TODO(vicb): make this a param (s/_/-/)
const _SOURCE_LANG = 'en';
const _DEFAULT_SOURCE_LANG = 'en';
const _PLACEHOLDER_TAG = 'x';
const _FILE_TAG = 'file';
@ -29,7 +29,7 @@ const _UNIT_TAG = 'trans-unit';
// http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
// http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
export class Xliff extends Serializer {
write(messages: i18n.Message[]): string {
write(messages: i18n.Message[], locale: string|null): string {
const visitor = new _WriteVisitor();
const transUnits: xml.Node[] = [];
@ -59,7 +59,11 @@ export class Xliff extends Serializer {
const body = new xml.Tag('body', {}, [...transUnits, new xml.CR(4)]);
const file = new xml.Tag(
'file', {'source-language': _SOURCE_LANG, datatype: 'plaintext', original: 'ng2.template'},
'file', {
'source-language': locale || _DEFAULT_SOURCE_LANG,
datatype: 'plaintext',
original: 'ng2.template',
},
[new xml.CR(4), body, new xml.CR(2)]);
const xliff = new xml.Tag(
'xliff', {version: _VERSION, xmlns: _XMLNS}, [new xml.CR(2), file, new xml.CR()]);
@ -283,4 +287,4 @@ function getCtypeForTag(tag: string): string {
default:
return `x-${tag}`;
}
}
}

View File

@ -38,7 +38,7 @@ const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
<!ELEMENT ex (#PCDATA)>`;
export class Xmb extends Serializer {
write(messages: i18n.Message[]): string {
write(messages: i18n.Message[], locale: string|null): string {
const exampleVisitor = new ExampleVisitor();
const visitor = new _Visitor();
let rootNode = new xml.Tag(_MESSAGES_TAG);
@ -161,4 +161,4 @@ class ExampleVisitor implements xml.IVisitor {
// XMB/XTB placeholders can only contain A-Z, 0-9 and _
export function toPublicName(internalName: string): string {
return internalName.toUpperCase().replace(/[^A-Z0-9_]/g, '_');
}
}

View File

@ -19,7 +19,7 @@ const _TRANSLATION_TAG = 'translation';
const _PLACEHOLDER_TAG = 'ph';
export class Xtb extends Serializer {
write(messages: i18n.Message[]): string { throw new Error('Unsupported'); }
write(messages: i18n.Message[], locale: string|null): string { throw new Error('Unsupported'); }
load(content: string, url: string):
{locale: string, i18nNodesByMsgId: {[msgId: string]: i18n.Node[]}} {