Search in sources :

Example 1 with SoyMsgPluralCaseSpec

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

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

the class MsgCompiler method putPluralPartIntoMap.

private void putPluralPartIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgPluralPart plural) {
    MsgPluralNode repPluralNode = originalMsg.getRepPluralNode(plural.getPluralVarName());
    if (!placeholderNameToPutStatement.containsKey(plural.getPluralVarName())) {
        Label reattachPoint = new Label();
        Expression value = soyNodeCompiler.compileToInt(repPluralNode.getExpr(), reattachPoint);
        placeholderNameToPutStatement.put(plural.getPluralVarName(), putToMap(mapExpression, plural.getPluralVarName(), value).labelStart(reattachPoint).withSourceLocation(repPluralNode.getSourceLocation()));
    }
    // Recursively visit plural cases
    for (Case<SoyMsgPluralCaseSpec> caseOrDefault : plural.getCases()) {
        putPlaceholdersIntoMap(mapExpression, originalMsg, caseOrDefault.parts(), placeholderNameToPutStatement);
    }
}
Also used : SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Label(org.objectweb.asm.Label) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode)

Example 3 with SoyMsgPluralCaseSpec

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

Aggregations

SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)3 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)2 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)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 LinkedHashMap (java.util.LinkedHashMap)1 Label (org.objectweb.asm.Label)1