Search in sources :

Example 1 with SoyMsgPart

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

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

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

the class MsgUtils method buildMsgPartForSelect.

/**
 * Builds the list of SoyMsgParts for the given MsgSelectNode.
 *
 * @param msgSelectNode The select node parsed from the Soy source.
 * @param msgNode The MsgNode containing 'msgSelectNode'.
 * @return A SoyMsgSelectPart.
 */
private static SoyMsgSelectPart buildMsgPartForSelect(MsgSelectNode msgSelectNode, MsgNode msgNode) {
    // This is the list of the cases.
    ImmutableList.Builder<SoyMsgPart.Case<String>> selectCases = ImmutableList.builder();
    for (CaseOrDefaultNode child : msgSelectNode.getChildren()) {
        ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
        String caseValue;
        if (child instanceof MsgSelectCaseNode) {
            caseValue = ((MsgSelectCaseNode) child).getCaseValue();
        } else if (child instanceof MsgSelectDefaultNode) {
            caseValue = null;
        } else {
            throw new AssertionError("Unidentified node under a select node.");
        }
        selectCases.add(SoyMsgPart.Case.create(caseValue, caseMsgParts));
    }
    return new SoyMsgSelectPart(msgNode.getSelectVarName(msgSelectNode), selectCases.build());
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) ImmutableList(com.google.common.collect.ImmutableList) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 4 with SoyMsgPart

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

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

the class MsgUtils method buildMsgPartForPlural.

/**
 * Builds the list of SoyMsgParts for the given MsgPluralNode.
 *
 * @param msgPluralNode The plural node parsed from the Soy source.
 * @param msgNode The MsgNode containing 'msgPluralNode'.
 * @return A SoyMsgPluralPart.
 */
private static SoyMsgPluralPart buildMsgPartForPlural(MsgPluralNode msgPluralNode, MsgNode msgNode) {
    // This is the list of the cases.
    ImmutableList.Builder<SoyMsgPart.Case<SoyMsgPluralCaseSpec>> pluralCases = ImmutableList.builder();
    for (CaseOrDefaultNode child : msgPluralNode.getChildren()) {
        ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
        SoyMsgPluralCaseSpec caseSpec;
        if (child instanceof MsgPluralCaseNode) {
            caseSpec = new SoyMsgPluralCaseSpec(((MsgPluralCaseNode) child).getCaseNumber());
        } else if (child instanceof MsgPluralDefaultNode) {
            caseSpec = new SoyMsgPluralCaseSpec(Type.OTHER);
        } else {
            throw new AssertionError("Unidentified node under a plural node.");
        }
        pluralCases.add(SoyMsgPart.Case.create(caseSpec, caseMsgParts));
    }
    return new SoyMsgPluralPart(msgNode.getPluralVarName(msgPluralNode), msgPluralNode.getOffset(), pluralCases.build());
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) MsgPluralCaseNode(com.google.template.soy.soytree.MsgPluralCaseNode) ImmutableList(com.google.common.collect.ImmutableList) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Aggregations

SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)14 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)9 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)7 ImmutableList (com.google.common.collect.ImmutableList)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)3 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)3 Test (org.junit.Test)3 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)2 MsgNode (com.google.template.soy.soytree.MsgNode)2 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Escaper (com.google.common.escape.Escaper)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)1 SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)1 SoyMsgPluralRemainderPart (com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart)1