Search in sources :

Example 1 with SoyMsgPluralPart

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

Example 2 with SoyMsgPluralPart

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

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

the class IcuSyntaxUtils method convertMsgPartsHelper.

/**
 * Private helper for {@code convertMsgPartsToEmbeddedIcuSyntax()} to convert msg parts.
 *
 * @param newMsgPartsBuilder The new msg parts being built.
 * @param currRawTextSb The collector for the current raw text, which hasn't yet been turned into
 *     a SoyMsgRawTextPart and added to newMsgPartsBuilder because it might not be complete.
 * @param origMsgParts The msg parts to convert.
 * @param isInPlrselPart Whether we're currently within a plural/select part's subtree.
 */
private static void convertMsgPartsHelper(ImmutableList.Builder<SoyMsgPart> newMsgPartsBuilder, StringBuilder currRawTextSb, List<SoyMsgPart> origMsgParts, boolean isInPlrselPart) {
    for (SoyMsgPart origMsgPart : origMsgParts) {
        if (origMsgPart instanceof SoyMsgRawTextPart) {
            String rawText = ((SoyMsgRawTextPart) origMsgPart).getRawText();
            if (isInPlrselPart) {
                rawText = icuEscape(rawText);
            }
            currRawTextSb.append(rawText);
        } else if (origMsgPart instanceof SoyMsgPlaceholderPart) {
            // a msg part for it and clear the collector.
            if (currRawTextSb.length() > 0) {
                newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString()));
                currRawTextSb.setLength(0);
            }
            // Reuse the msg part for the placeholder since it's immutable.
            newMsgPartsBuilder.add(origMsgPart);
        } else if (origMsgPart instanceof SoyMsgPluralRemainderPart) {
            currRawTextSb.append(getPluralRemainderString());
        } else if (origMsgPart instanceof SoyMsgPluralPart) {
            convertPluralPartHelper(newMsgPartsBuilder, currRawTextSb, (SoyMsgPluralPart) origMsgPart);
        } else if (origMsgPart instanceof SoyMsgSelectPart) {
            convertSelectPartHelper(newMsgPartsBuilder, currRawTextSb, (SoyMsgSelectPart) origMsgPart);
        }
    }
}
Also used : SoyMsgPluralRemainderPart(com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 4 with SoyMsgPluralPart

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

the class MsgFuncGenerator method pyFuncForPluralMsg.

private PyStringExpr pyFuncForPluralMsg() {
    SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) msgParts.get(0);
    MsgPluralNode pluralNode = msgNode.getRepPluralNode(pluralPart.getPluralVarName());
    Map<PyExpr, PyExpr> nodePyVarToPyExprMap = collectVarNameListAndToPyExprMap();
    Map<PyExpr, PyExpr> caseSpecStrToMsgTexts = new LinkedHashMap<>();
    for (Case<SoyMsgPluralCaseSpec> pluralCase : pluralPart.getCases()) {
        caseSpecStrToMsgTexts.put(new PyStringExpr("'" + pluralCase.spec() + "'"), new PyStringExpr("'" + processMsgPartsHelper(pluralCase.parts(), escaperForIcuSection) + "'"));
    }
    prepareFunc.addArg(msgId).addArg(PyExprUtils.convertMapToPyExpr(caseSpecStrToMsgTexts)).addArg(PyExprUtils.convertIterableToPyTupleExpr(nodePyVarToPyExprMap.keySet()));
    // Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
    // Note that pluralExpr represent the Soy expression inside the attributes of a plural tag.
    PyExpr pluralPyExpr = translateToPyExprVisitor.exec(pluralNode.getExpr());
    return renderFunc.addArg(prepareFunc.asPyExpr()).addArg(pluralPyExpr).addArg(PyExprUtils.convertMapToPyExpr(nodePyVarToPyExprMap)).asPyStringExpr();
}
Also used : SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SoyMsgPluralPart

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

the class RenderVisitorAssistantForMsgs method renderMsgFromTranslation.

/**
 * Private helper for visitMsgFallbackGroupNode() to render a message from its translation.
 */
private void renderMsgFromTranslation(MsgNode msg, ImmutableList<SoyMsgPart> msgParts, @Nullable ULocale locale) {
    SoyMsgPart firstPart = msgParts.get(0);
    if (firstPart instanceof SoyMsgPluralPart) {
        new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgPluralPart) firstPart);
    } else if (firstPart instanceof SoyMsgSelectPart) {
        new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgSelectPart) firstPart);
    } else {
        for (SoyMsgPart msgPart : msgParts) {
            if (msgPart instanceof SoyMsgRawTextPart) {
                RenderVisitor.append(master.getCurrOutputBufForUseByAssistants(), ((SoyMsgRawTextPart) msgPart).getRawText());
            } else if (msgPart instanceof SoyMsgPlaceholderPart) {
                String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
                visit(msg.getRepPlaceholderNode(placeholderName));
            } else {
                throw new AssertionError();
            }
        }
    }
}
Also used : SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Aggregations

SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)5 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)3 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)3 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)3 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)3 ImmutableList (com.google.common.collect.ImmutableList)2 SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)2 SoyMsgPluralRemainderPart (com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 Case (com.google.template.soy.msgs.restricted.SoyMsgPart.Case)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)1 CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)1 MsgPluralCaseNode (com.google.template.soy.soytree.MsgPluralCaseNode)1 MsgPluralDefaultNode (com.google.template.soy.soytree.MsgPluralDefaultNode)1 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1