refactor(core): simplify i18n serializers code
This commit is contained in:
parent
c60ba7a72f
commit
146af1fed9
@ -17,23 +17,3 @@ export interface Serializer {
|
|||||||
|
|
||||||
digest(message: i18n.Message): string;
|
digest(message: i18n.Message): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a map of placeholder to content indexed by message ids
|
|
||||||
export function extractPlaceholders(messageMap: {[msgKey: string]: i18n.Message}) {
|
|
||||||
const phByMsgId: {[msgId: string]: {[name: string]: string}} = {};
|
|
||||||
|
|
||||||
Object.keys(messageMap).forEach(msgId => { phByMsgId[msgId] = messageMap[msgId].placeholders; });
|
|
||||||
|
|
||||||
return phByMsgId;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a map of placeholder to message ids indexed by message ids
|
|
||||||
export function extractPlaceholderToMessage(messageMap: {[msgKey: string]: i18n.Message}) {
|
|
||||||
const phToMsgByMsgId: {[msgId: string]: {[name: string]: i18n.Message}} = {};
|
|
||||||
|
|
||||||
Object.keys(messageMap).forEach(msgId => {
|
|
||||||
phToMsgByMsgId[msgId] = messageMap[msgId].placeholderToMessage;
|
|
||||||
});
|
|
||||||
|
|
||||||
return phToMsgByMsgId;
|
|
||||||
}
|
|
@ -16,7 +16,7 @@ import * as i18n from '../i18n_ast';
|
|||||||
import {MessageBundle} from '../message_bundle';
|
import {MessageBundle} from '../message_bundle';
|
||||||
import {I18nError} from '../parse_util';
|
import {I18nError} from '../parse_util';
|
||||||
|
|
||||||
import {Serializer, extractPlaceholderToMessage, extractPlaceholders} from './serializer';
|
import {Serializer} from './serializer';
|
||||||
import * as xml from './xml_helper';
|
import * as xml from './xml_helper';
|
||||||
|
|
||||||
const _VERSION = '1.2';
|
const _VERSION = '1.2';
|
||||||
@ -175,8 +175,7 @@ class _LoadVisitor implements ml.Visitor {
|
|||||||
private _msgId: string;
|
private _msgId: string;
|
||||||
private _target: ml.Node[];
|
private _target: ml.Node[];
|
||||||
private _errors: I18nError[];
|
private _errors: I18nError[];
|
||||||
private _placeholders: {[phName: string]: string};
|
private _sourceMessage: i18n.Message;
|
||||||
private _placeholderToMessage: {[phName: string]: i18n.Message};
|
|
||||||
|
|
||||||
constructor(private _serializer: Serializer) {}
|
constructor(private _serializer: Serializer) {}
|
||||||
|
|
||||||
@ -193,8 +192,6 @@ class _LoadVisitor implements ml.Visitor {
|
|||||||
|
|
||||||
const messageMap: {[msgId: string]: i18n.Message} = {};
|
const messageMap: {[msgId: string]: i18n.Message} = {};
|
||||||
messageBundle.getMessages().forEach(m => messageMap[this._serializer.digest(m)] = m);
|
messageBundle.getMessages().forEach(m => messageMap[this._serializer.digest(m)] = m);
|
||||||
const placeholdersByMsgId = extractPlaceholders(messageMap);
|
|
||||||
const placeholderToMessageByMsgId = extractPlaceholderToMessage(messageMap);
|
|
||||||
|
|
||||||
this._messageNodes
|
this._messageNodes
|
||||||
.filter(message => {
|
.filter(message => {
|
||||||
@ -218,8 +215,7 @@ class _LoadVisitor implements ml.Visitor {
|
|||||||
})
|
})
|
||||||
.forEach(message => {
|
.forEach(message => {
|
||||||
const msgId = message[0];
|
const msgId = message[0];
|
||||||
this._placeholders = placeholdersByMsgId[msgId] || {};
|
this._sourceMessage = messageMap[msgId];
|
||||||
this._placeholderToMessage = placeholderToMessageByMsgId[msgId] || {};
|
|
||||||
// TODO(vicb): make sure there is no `_TRANSLATIONS_TAG` nor `_TRANSLATION_TAG`
|
// TODO(vicb): make sure there is no `_TRANSLATIONS_TAG` nor `_TRANSLATION_TAG`
|
||||||
this._translatedMessages[msgId] = ml.visitAll(this, message[1]).join('');
|
this._translatedMessages[msgId] = ml.visitAll(this, message[1]).join('');
|
||||||
});
|
});
|
||||||
@ -257,11 +253,12 @@ class _LoadVisitor implements ml.Visitor {
|
|||||||
this._addError(element, `<${_PLACEHOLDER_TAG}> misses the "id" attribute`);
|
this._addError(element, `<${_PLACEHOLDER_TAG}> misses the "id" attribute`);
|
||||||
} else {
|
} else {
|
||||||
const phName = idAttr.value;
|
const phName = idAttr.value;
|
||||||
if (this._placeholders.hasOwnProperty(phName)) {
|
if (this._sourceMessage.placeholders.hasOwnProperty(phName)) {
|
||||||
return this._placeholders[phName];
|
return this._sourceMessage.placeholders[phName];
|
||||||
}
|
}
|
||||||
if (this._placeholderToMessage.hasOwnProperty(phName)) {
|
if (this._sourceMessage.placeholderToMessage.hasOwnProperty(phName)) {
|
||||||
const refMsgId = this._serializer.digest(this._placeholderToMessage[phName]);
|
const refMsg = this._sourceMessage.placeholderToMessage[phName];
|
||||||
|
const refMsgId = this._serializer.digest(refMsg);
|
||||||
if (this._translatedMessages.hasOwnProperty(refMsgId)) {
|
if (this._translatedMessages.hasOwnProperty(refMsgId)) {
|
||||||
return this._translatedMessages[refMsgId];
|
return this._translatedMessages[refMsgId];
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import * as i18n from '../i18n_ast';
|
|||||||
import {MessageBundle} from '../message_bundle';
|
import {MessageBundle} from '../message_bundle';
|
||||||
import {I18nError} from '../parse_util';
|
import {I18nError} from '../parse_util';
|
||||||
|
|
||||||
import {Serializer, extractPlaceholderToMessage, extractPlaceholders} from './serializer';
|
import {Serializer} from './serializer';
|
||||||
import {digest} from './xmb';
|
import {digest} from './xmb';
|
||||||
|
|
||||||
const _TRANSLATIONS_TAG = 'translationbundle';
|
const _TRANSLATIONS_TAG = 'translationbundle';
|
||||||
@ -72,8 +72,7 @@ class _Visitor implements ml.Visitor {
|
|||||||
private _bundleDepth: number;
|
private _bundleDepth: number;
|
||||||
private _translationDepth: number;
|
private _translationDepth: number;
|
||||||
private _errors: I18nError[];
|
private _errors: I18nError[];
|
||||||
private _placeholders: {[phName: string]: string};
|
private _sourceMessage: i18n.Message;
|
||||||
private _placeholderToMessage: {[phName: string]: i18n.Message};
|
|
||||||
|
|
||||||
constructor(private _serializer: Serializer) {}
|
constructor(private _serializer: Serializer) {}
|
||||||
|
|
||||||
@ -86,13 +85,11 @@ class _Visitor implements ml.Visitor {
|
|||||||
this._translationDepth = 0;
|
this._translationDepth = 0;
|
||||||
this._errors = [];
|
this._errors = [];
|
||||||
|
|
||||||
// Find all messages
|
// load all translations
|
||||||
ml.visitAll(this, nodes, null);
|
ml.visitAll(this, nodes, null);
|
||||||
|
|
||||||
const messageMap: {[msgId: string]: i18n.Message} = {};
|
const messageMap: {[msgId: string]: i18n.Message} = {};
|
||||||
messageBundle.getMessages().forEach(m => messageMap[this._serializer.digest(m)] = m);
|
messageBundle.getMessages().forEach(m => messageMap[this._serializer.digest(m)] = m);
|
||||||
const placeholdersByMsgId = extractPlaceholders(messageMap);
|
|
||||||
const placeholderToMessageByMsgId = extractPlaceholderToMessage(messageMap);
|
|
||||||
|
|
||||||
this._messageNodes
|
this._messageNodes
|
||||||
.filter(message => {
|
.filter(message => {
|
||||||
@ -116,9 +113,7 @@ class _Visitor implements ml.Visitor {
|
|||||||
})
|
})
|
||||||
.forEach(message => {
|
.forEach(message => {
|
||||||
const msgId = message[0];
|
const msgId = message[0];
|
||||||
this._placeholders = placeholdersByMsgId[msgId] || {};
|
this._sourceMessage = messageMap[msgId];
|
||||||
this._placeholderToMessage = placeholderToMessageByMsgId[msgId] || {};
|
|
||||||
|
|
||||||
// TODO(vicb): make sure there is no `_TRANSLATIONS_TAG` nor `_TRANSLATION_TAG`
|
// TODO(vicb): make sure there is no `_TRANSLATIONS_TAG` nor `_TRANSLATION_TAG`
|
||||||
this._translatedMessages[msgId] = ml.visitAll(this, message[1]).join('');
|
this._translatedMessages[msgId] = ml.visitAll(this, message[1]).join('');
|
||||||
});
|
});
|
||||||
@ -161,13 +156,14 @@ class _Visitor implements ml.Visitor {
|
|||||||
this._addError(element, `<${_PLACEHOLDER_TAG}> misses the "name" attribute`);
|
this._addError(element, `<${_PLACEHOLDER_TAG}> misses the "name" attribute`);
|
||||||
} else {
|
} else {
|
||||||
const phName = nameAttr.value;
|
const phName = nameAttr.value;
|
||||||
if (this._placeholders.hasOwnProperty(phName)) {
|
if (this._sourceMessage.placeholders.hasOwnProperty(phName)) {
|
||||||
return this._placeholders[phName];
|
return this._sourceMessage.placeholders[phName];
|
||||||
}
|
}
|
||||||
if (this._placeholderToMessage.hasOwnProperty(phName)) {
|
if (this._sourceMessage.placeholderToMessage.hasOwnProperty(phName)) {
|
||||||
const refMessageId = this._serializer.digest(this._placeholderToMessage[phName]);
|
const refMsg = this._sourceMessage.placeholderToMessage[phName];
|
||||||
if (this._translatedMessages.hasOwnProperty(refMessageId)) {
|
const refMsgId = this._serializer.digest(refMsg);
|
||||||
return this._translatedMessages[refMessageId];
|
if (this._translatedMessages.hasOwnProperty(refMsgId)) {
|
||||||
|
return this._translatedMessages[refMsgId];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO(vicb): better error message for when
|
// TODO(vicb): better error message for when
|
||||||
|
Loading…
x
Reference in New Issue
Block a user