fix(core): misc i18n fixes

This commit is contained in:
Victor Berchet 2016-11-01 18:02:29 -07:00
parent 146af1fed9
commit ed5e98d0df
4 changed files with 21 additions and 11 deletions

View File

@ -12,7 +12,6 @@ import * as html from '../ml_parser/ast';
import {getHtmlTagDefinition} from '../ml_parser/html_tags'; import {getHtmlTagDefinition} from '../ml_parser/html_tags';
import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {InterpolationConfig} from '../ml_parser/interpolation_config';
import {ParseSourceSpan} from '../parse_util'; import {ParseSourceSpan} from '../parse_util';
import {digest} from './digest';
import * as i18n from './i18n_ast'; import * as i18n from './i18n_ast';
import {PlaceholderRegistry} from './serializers/placeholder'; import {PlaceholderRegistry} from './serializers/placeholder';
@ -34,8 +33,8 @@ class _I18nVisitor implements html.Visitor {
private _isIcu: boolean; private _isIcu: boolean;
private _icuDepth: number; private _icuDepth: number;
private _placeholderRegistry: PlaceholderRegistry; private _placeholderRegistry: PlaceholderRegistry;
private _placeholderToContent: {[name: string]: string}; private _placeholderToContent: {[phName: string]: string};
private _placeholderToMessage: {[name: string]: i18n.Message}; private _placeholderToMessage: {[phName: string]: i18n.Message};
constructor( constructor(
private _expressionParser: ExpressionParser, private _expressionParser: ExpressionParser,

View File

@ -35,12 +35,17 @@ export class Xliff implements Serializer {
write(messages: i18n.Message[]): string { write(messages: i18n.Message[]): string {
const visitor = new _WriteVisitor(); const visitor = new _WriteVisitor();
const visited: {[id: string]: boolean} = {};
const transUnits: xml.Node[] = []; const transUnits: xml.Node[] = [];
messages.forEach(message => { messages.forEach(message => {
const id = this.digest(message);
const transUnit = new xml.Tag(_UNIT_TAG, {id: this.digest(message), datatype: 'html'}); // deduplicate messages
if (visited[id]) return;
visited[id] = true;
const transUnit = new xml.Tag(_UNIT_TAG, {id, datatype: 'html'});
transUnit.children.push( transUnit.children.push(
new xml.CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), new xml.CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)),
new xml.CR(8), new xml.Tag(_TARGET_TAG)); new xml.CR(8), new xml.Tag(_TARGET_TAG));

View File

@ -42,10 +42,17 @@ const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
export class Xmb implements Serializer { export class Xmb implements Serializer {
write(messages: i18n.Message[]): string { write(messages: i18n.Message[]): string {
const visitor = new _Visitor(); const visitor = new _Visitor();
const rootNode = new xml.Tag(_MESSAGES_TAG); const visited: {[id: string]: boolean} = {};
let rootNode = new xml.Tag(_MESSAGES_TAG);
messages.forEach(message => { messages.forEach(message => {
const attrs: {[k: string]: string} = {id: this.digest(message)}; const id = this.digest(message);
// deduplicate messages
if (visited[id]) return;
visited[id] = true;
const attrs: {[k: string]: string} = {id};
if (message.description) { if (message.description) {
attrs['desc'] = message.description; attrs['desc'] = message.description;

View File

@ -29,7 +29,7 @@ export class Xtb implements Serializer {
load(content: string, url: string, messageBundle: MessageBundle): {[id: string]: ml.Node[]} { load(content: string, url: string, messageBundle: MessageBundle): {[id: string]: ml.Node[]} {
// Parse the xtb file into xml nodes // Parse the xtb file into xml nodes
const result = new XmlParser().parse(content, url); const result = new XmlParser().parse(content, url, true);
if (result.errors.length) { if (result.errors.length) {
throw new Error(`xtb parse errors:\n${result.errors.join('\n')}`); throw new Error(`xtb parse errors:\n${result.errors.join('\n')}`);
@ -188,12 +188,11 @@ class _Visitor implements ml.Visitor {
visitExpansion(expansion: ml.Expansion, context: any): any { visitExpansion(expansion: ml.Expansion, context: any): any {
const strCases = expansion.cases.map(c => c.visit(this, null)); const strCases = expansion.cases.map(c => c.visit(this, null));
return `{${expansion.switchValue}, ${expansion.type}, ${strCases.join(' ')}}`;
return `{${expansion.switchValue}, ${expansion.type}, strCases.join(' ')}`;
} }
visitExpansionCase(expansionCase: ml.ExpansionCase, context: any): any { visitExpansionCase(expansionCase: ml.ExpansionCase, context: any): any {
return `${expansionCase.value} {${ml.visitAll(this, expansionCase.expression, null)}}`; return `${expansionCase.value} {${ml.visitAll(this, expansionCase.expression, null).join('')}}`;
} }
private _addError(node: ml.Node, message: string): void { private _addError(node: ml.Node, message: string): void {