Search in sources :

Example 1 with SoyMsgPlaceholderPart

use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.

the class GenJsCodeVisitorAssistantForMsgs method buildGoogMsgContentStr.

/**
 * Builds the message content string for a goog.getMsg() call.
 *
 * @param msgParts The parts of the message.
 * @param doUseBracedPhs Whether to use braced placeholders.
 * @return The message content string for a goog.getMsg() call.
 */
private static String buildGoogMsgContentStr(ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
    msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts);
    StringBuilder msgStrSb = new StringBuilder();
    for (SoyMsgPart msgPart : msgParts) {
        if (msgPart instanceof SoyMsgRawTextPart) {
            msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
        } else if (msgPart instanceof SoyMsgPlaceholderPart) {
            String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
            if (doUseBracedPhs) {
                // Add placeholder to message text.
                msgStrSb.append("{").append(placeholderName).append("}");
            } else {
                // For goog.getMsg(), we must change the placeholder name to lower camel-case format.
                String googMsgPlaceholderName = genGoogMsgPlaceholderName(placeholderName);
                // Add placeholder to message text. Note the '$' for goog.getMsg() syntax.
                msgStrSb.append("{$").append(googMsgPlaceholderName).append("}");
            }
        } else {
            throw new AssertionError();
        }
    }
    return msgStrSb.toString();
}
Also used : SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 2 with SoyMsgPlaceholderPart

use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.

the class SoyMsgIdComputer method buildMsgContentStrForMsgIdComputation.

/**
 * Private helper to build the canonical message content string that should be used for msg id
 * computation.
 *
 * <p>Note: For people who know what "presentation" means in this context, the result string
 * should be exactly the presentation string.
 *
 * @param msgParts The parts of the message.
 * @param doUseBracedPhs Whether to use braced placeholders.
 * @return The canonical message content string that should be used for msg id computation.
 */
@VisibleForTesting
static String buildMsgContentStrForMsgIdComputation(ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
    msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts);
    StringBuilder msgStrSb = new StringBuilder();
    for (SoyMsgPart msgPart : msgParts) {
        if (msgPart instanceof SoyMsgRawTextPart) {
            msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
        } else if (msgPart instanceof SoyMsgPlaceholderPart) {
            if (doUseBracedPhs) {
                msgStrSb.append('{');
            }
            msgStrSb.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName());
            if (doUseBracedPhs) {
                msgStrSb.append('}');
            }
        } else {
            throw new AssertionError();
        }
    }
    return msgStrSb.toString();
}
Also used : SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with SoyMsgPlaceholderPart

use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.

the class MsgUtils method buildMsgPartsForChildren.

// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
 * Builds the list of SoyMsgParts for all the children of a given parent node.
 *
 * @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
 *     MsgSelectDefaultNode.
 * @param msgNode The MsgNode containing 'parent'.
 */
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(BlockNode parent, MsgNode msgNode) {
    ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
    for (StandaloneNode child : parent.getChildren()) {
        if (child instanceof RawTextNode) {
            String rawText = ((RawTextNode) child).getRawText();
            msgParts.add(SoyMsgRawTextPart.of(rawText));
        } else if (child instanceof MsgPlaceholderNode) {
            PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child);
            msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example()));
        } else if (child instanceof MsgPluralNode) {
            msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
        } else if (child instanceof MsgSelectNode) {
            msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
        }
    }
    return msgParts.build();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderInfo(com.google.template.soy.soytree.MsgNode.PlaceholderInfo) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 4 with SoyMsgPlaceholderPart

use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.

the class MsgCompiler method partToPartExpression.

/**
 * Returns an {@link Expression} that evaluates to an equivalent SoyMsgPart as the argument.
 */
private Expression partToPartExpression(SoyMsgPart part) {
    if (part instanceof SoyMsgPlaceholderPart) {
        return SOY_MSG_PLACEHOLDER_PART.construct(constant(((SoyMsgPlaceholderPart) part).getPlaceholderName()), constantNull(STRING_TYPE));
    } else if (part instanceof SoyMsgPluralPart) {
        SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) part;
        List<Expression> caseExprs = new ArrayList<>(pluralPart.getCases().size());
        for (Case<SoyMsgPluralCaseSpec> item : pluralPart.getCases()) {
            Expression spec;
            if (item.spec().getType() == Type.EXPLICIT) {
                spec = SOY_MSG_PLURAL_CASE_SPEC_LONG.construct(constant(item.spec().getExplicitValue()));
            } else {
                spec = SOY_MSG_PLURAL_CASE_SPEC_TYPE.construct(FieldRef.enumReference(item.spec().getType()).accessor());
            }
            caseExprs.add(CASE_CREATE.invoke(spec, partsToPartsList(item.parts())));
        }
        return SOY_MSG_PURAL_PART.construct(constant(pluralPart.getPluralVarName()), constant(pluralPart.getOffset()), BytecodeUtils.asList(caseExprs));
    } else if (part instanceof SoyMsgPluralRemainderPart) {
        return SOY_MSG_PLURAL_REMAINDER_PART.construct(constant(((SoyMsgPluralRemainderPart) part).getPluralVarName()));
    } else if (part instanceof SoyMsgRawTextPart) {
        return SOY_MSG_RAW_TEXT_PART_OF.invoke(constant(((SoyMsgRawTextPart) part).getRawText(), variables));
    } else if (part instanceof SoyMsgSelectPart) {
        SoyMsgSelectPart selectPart = (SoyMsgSelectPart) part;
        List<Expression> caseExprs = new ArrayList<>(selectPart.getCases().size());
        for (Case<String> item : selectPart.getCases()) {
            caseExprs.add(CASE_CREATE.invoke(item.spec() == null ? constantNull(STRING_TYPE) : constant(item.spec()), partsToPartsList(item.parts())));
        }
        return SOY_MSG_SELECT_PART.construct(constant(selectPart.getSelectVarName()), BytecodeUtils.asList(caseExprs));
    } else {
        throw new AssertionError("unrecognized part: " + part);
    }
}
Also used : SoyMsgPluralRemainderPart(com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) ArrayList(java.util.ArrayList) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) Case(com.google.template.soy.msgs.restricted.SoyMsgPart.Case) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 5 with SoyMsgPlaceholderPart

use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.

the class MsgFuncGenerator method processMsgPartsHelper.

/**
 * Private helper to build valid Python string for a list of {@link SoyMsgPart}s.
 *
 * <p>It only processes {@link SoyMsgRawTextPart} and {@link SoyMsgPlaceholderPart} and ignores
 * others, because we didn't generate a direct string for plural and select nodes.
 *
 * <p>For {@link SoyMsgRawTextPart}, it appends the raw text and applies necessary escaping; For
 * {@link SoyMsgPlaceholderPart}, it turns the placeholder's variable name into Python replace
 * format.
 *
 * @param parts The SoyMsgPart parts to convert.
 * @param escaper A Function which provides escaping for raw text.
 * @return A String representing all the {@code parts} in Python.
 */
private static String processMsgPartsHelper(ImmutableList<SoyMsgPart> parts, Function<String, String> escaper) {
    StringBuilder rawMsgTextSb = new StringBuilder();
    for (SoyMsgPart part : parts) {
        if (part instanceof SoyMsgRawTextPart) {
            rawMsgTextSb.append(escaper.apply(((SoyMsgRawTextPart) part).getRawText()));
        }
        if (part instanceof SoyMsgPlaceholderPart) {
            String phName = ((SoyMsgPlaceholderPart) part).getPlaceholderName();
            rawMsgTextSb.append("{" + phName + "}");
        }
    }
    return rawMsgTextSb.toString();
}
Also used : SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Aggregations

SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)11 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)9 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)8 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)3 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)3 RawTextNode (com.google.template.soy.soytree.RawTextNode)3 ImmutableList (com.google.common.collect.ImmutableList)2 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)2 SoyMsgPluralRemainderPart (com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart)2 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)2 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Escaper (com.google.common.escape.Escaper)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)1 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)1