refactor(ivy): i18n - move marker block serialization to helpers (#32867)

Previously the metadata and placeholder blocks were serialized in
a variety of places. Moreover the code for creating the `LocalizedString`
AST node was doing serialization, which break the separation of concerns.

Now this is all done by the code that renders the AST and is refactored into
helper functions to avoid repeating the behaviour.

PR Close #32867
This commit is contained in:
Pete Bacon Darwin
2019-10-02 18:17:56 +01:00
committed by atscott
parent 97d5700456
commit 9b15588188
6 changed files with 65 additions and 55 deletions

View File

@ -9,7 +9,7 @@ import * as i18n from '../../../i18n/i18n_ast';
import * as o from '../../../output/output_ast';
import {serializeIcuNode} from './icu_serializer';
import {metaFromI18nMessage, serializeI18nMeta} from './meta';
import {metaFromI18nMessage} from './meta';
import {formatI18nPlaceholderName} from './util';
export function createLocalizeStatements(
@ -17,15 +17,10 @@ export function createLocalizeStatements(
params: {[name: string]: o.Expression}): o.Statement[] {
const statements = [];
const metaBlock = serializeI18nMeta(metaFromI18nMessage(message));
const {messageParts, placeHolders} = serializeI18nMessageForLocalize(message);
// Update first message part with metadata
messageParts[0] = `:${metaBlock}:${messageParts[0]}`;
statements.push(new o.ExpressionStatement(variable.set(
o.localizedString(messageParts, placeHolders, placeHolders.map(ph => params[ph])))));
statements.push(new o.ExpressionStatement(variable.set(o.localizedString(
metaFromI18nMessage(message), messageParts, placeHolders,
placeHolders.map(ph => params[ph])))));
return statements;
}

View File

@ -184,7 +184,7 @@ export function parseI18nMeta(meta?: string): I18nMeta {
*
* @param meta The metadata to serialize
*/
export function serializeI18nMeta(meta: I18nMeta): string {
export function serializeI18nMetaBlock(meta: I18nMeta): string {
let metaBlock = meta.description || '';
if (meta.meaning) {
metaBlock = `${meta.meaning}|${metaBlock}`;
@ -192,7 +192,22 @@ export function serializeI18nMeta(meta: I18nMeta): string {
if (meta.id) {
metaBlock = `${metaBlock}@@${meta.id}`;
}
return metaBlock;
return metaBlock !== '' ? `:${metaBlock}:` : '';
}
/**
* Convert a placeholder into marked block for rendering.
*
* We want our tagged literals to include placeholder name information to aid runtime translation.
*
* The expressions are marked with placeholder names by postfixing the expression with
* `:placeHolderName:`. To achieve this, we actually "prefix" the message part that follows the
* expression.
*
* @param placeholderName The placeholder name to serialize
*/
export function serializeI18nPlaceholderBlock(placeholderName: string): string {
return placeholderName !== '' ? `:${placeholderName}:` : '';
}
// Converts i18n meta information for a message (id, description, meaning)