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());
}
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);
}
}
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();
}
Aggregations