use of com.google.template.soy.soytree.MsgPluralCaseNode 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.soytree.MsgPluralCaseNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmtWithPlural.
// -----------------------------------------------------------------------------------------------
// Tests for plural/select messages.
@Test
public void testParseMsgStmtWithPlural() throws Exception {
String templateBody = "{@param num_people : ?}{@param person : ?}{@param place : ?}\n" + " {msg desc=\"A sample plural message\"}\n" + " {plural $num_people offset=\"1\"}\n" + " {case 0}I see no one in {$place}.\n" + " {case 1}I see {$person} in {$place}.\n" + " {case 2}I see {$person} and one other person in {$place}.\n" + " {default}I see {$person} and {remainder($num_people)} " + "other people in {$place}.\n" + " {/plural}" + " {/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(1, mn.numChildren());
assertEquals("A sample plural message", mn.getDesc());
MsgPluralNode pn = (MsgPluralNode) mn.getChild(0);
assertEquals("$num_people", pn.getExpr().toSourceString());
assertEquals(1, pn.getOffset());
// 3 cases and default
assertEquals(4, pn.numChildren());
// Case 0
MsgPluralCaseNode cn0 = (MsgPluralCaseNode) pn.getChild(0);
assertEquals(3, cn0.numChildren());
assertEquals(0, cn0.getCaseNumber());
RawTextNode rtn01 = (RawTextNode) cn0.getChild(0);
assertEquals("I see no one in ", rtn01.getRawText());
MsgPlaceholderNode phn01 = (MsgPlaceholderNode) cn0.getChild(1);
assertEquals("{$place}", phn01.toSourceString());
RawTextNode rtn02 = (RawTextNode) cn0.getChild(2);
assertEquals(".", rtn02.getRawText());
// Case 1
MsgPluralCaseNode cn1 = (MsgPluralCaseNode) pn.getChild(1);
assertEquals(5, cn1.numChildren());
assertEquals(1, cn1.getCaseNumber());
RawTextNode rtn11 = (RawTextNode) cn1.getChild(0);
assertEquals("I see ", rtn11.getRawText());
MsgPlaceholderNode phn11 = (MsgPlaceholderNode) cn1.getChild(1);
assertEquals("{$person}", phn11.toSourceString());
RawTextNode rtn12 = (RawTextNode) cn1.getChild(2);
assertEquals(" in ", rtn12.getRawText());
MsgPlaceholderNode phn12 = (MsgPlaceholderNode) cn1.getChild(3);
assertEquals("{$place}", phn12.toSourceString());
RawTextNode rtn13 = (RawTextNode) cn1.getChild(4);
assertEquals(".", rtn13.getRawText());
// Case 2
MsgPluralCaseNode cn2 = (MsgPluralCaseNode) pn.getChild(2);
assertEquals(5, cn2.numChildren());
assertEquals(2, cn2.getCaseNumber());
RawTextNode rtn21 = (RawTextNode) cn2.getChild(0);
assertEquals("I see ", rtn21.getRawText());
MsgPlaceholderNode phn21 = (MsgPlaceholderNode) cn2.getChild(1);
assertEquals("{$person}", phn21.toSourceString());
RawTextNode rtn22 = (RawTextNode) cn2.getChild(2);
assertEquals(" and one other person in ", rtn22.getRawText());
MsgPlaceholderNode phn22 = (MsgPlaceholderNode) cn2.getChild(3);
assertEquals("{$place}", phn22.toSourceString());
RawTextNode rtn23 = (RawTextNode) cn2.getChild(4);
assertEquals(".", rtn23.getRawText());
// Default
MsgPluralDefaultNode dn = (MsgPluralDefaultNode) pn.getChild(3);
assertEquals(7, dn.numChildren());
RawTextNode rtnd1 = (RawTextNode) dn.getChild(0);
assertEquals("I see ", rtnd1.getRawText());
MsgPlaceholderNode phnd1 = (MsgPlaceholderNode) dn.getChild(1);
assertEquals("{$person}", phnd1.toSourceString());
RawTextNode rtnd2 = (RawTextNode) dn.getChild(2);
assertEquals(" and ", rtnd2.getRawText());
MsgPlaceholderNode phnd2 = (MsgPlaceholderNode) dn.getChild(3);
assertEquals("{$num_people - 1}", phnd2.toSourceString());
RawTextNode rtnd3 = (RawTextNode) dn.getChild(4);
assertEquals(" other people in ", rtnd3.getRawText());
MsgPlaceholderNode phnd3 = (MsgPlaceholderNode) dn.getChild(5);
assertEquals("{$place}", phnd3.toSourceString());
RawTextNode rtnd4 = (RawTextNode) dn.getChild(6);
assertEquals(".", rtnd4.getRawText());
}
Aggregations