Search in sources :

Example 1 with SoyMsgRawTextPart

use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart 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 SoyMsgRawTextPart

use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart 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 SoyMsgRawTextPart

use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart 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 4 with SoyMsgRawTextPart

use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart 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)

Example 5 with SoyMsgRawTextPart

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

the class InsertMsgsVisitor method buildReplacementNodesFromTranslation.

/**
 * Private helper for visitMsgFallbackGroupNode() to build the list of replacement nodes for a
 * message from its translation.
 */
private void buildReplacementNodesFromTranslation(MsgNode msg, SoyMsg translation) {
    currReplacementNodes = Lists.newArrayList();
    for (SoyMsgPart msgPart : translation.getParts()) {
        if (msgPart instanceof SoyMsgRawTextPart) {
            // Append a new RawTextNode to the currReplacementNodes list.
            String rawText = ((SoyMsgRawTextPart) msgPart).getRawText();
            currReplacementNodes.add(new RawTextNode(nodeIdGen.genId(), rawText, msg.getSourceLocation()));
        } else if (msgPart instanceof SoyMsgPlaceholderPart) {
            // Get the representative placeholder node and iterate through its contents.
            String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
            MsgPlaceholderNode placeholderNode = msg.getRepPlaceholderNode(placeholderName);
            for (StandaloneNode contentNode : placeholderNode.getChildren()) {
                // simply add the content node to the currReplacementNodes list being built.
                if (contentNode instanceof MsgHtmlTagNode) {
                    currReplacementNodes.addAll(((MsgHtmlTagNode) contentNode).getChildren());
                } else {
                    currReplacementNodes.add(contentNode);
                }
            }
        } else {
            throw new AssertionError();
        }
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Aggregations

SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)8 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)8 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)7 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)3 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)3 SoyMsgPluralRemainderPart (com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)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 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)1 Case (com.google.template.soy.msgs.restricted.SoyMsgPart.Case)1 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)1 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)1 RawTextNode (com.google.template.soy.soytree.RawTextNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1