Search in sources :

Example 11 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class InsertMsgsVisitorTest method testFallbackMsgsUsingMsgBundle.

@Test
public void testFallbackMsgsUsingMsgBundle() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
    // Build the translated message bundle.
    List<SoyMsg> translatedMsgs = Lists.newArrayList();
    MsgNode trans1FirstInstance = ((MsgFallbackGroupNode) template.getChild(1)).getChild(0);
    translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(trans1FirstInstance)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("ztrans1"))).build());
    MsgNode trans2FirstInstance = ((MsgFallbackGroupNode) template.getChild(2)).getChild(1);
    translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(trans2FirstInstance)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("ztrans2"))).build());
    SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", translatedMsgs);
    // Execute the visitor.
    new InsertMsgsVisitor(msgBundle, FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("ztrans1");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("ztrans2");
    assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("ztrans1");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 12 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class InsertMsgsVisitorTest method testBasicMsgsUsingMsgBundle.

@Test
public void testBasicMsgsUsingMsgBundle() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(5);
    MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
    assertThat(msg.numChildren()).isEqualTo(5);
    MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
    assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
    MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
    assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
    // Build the translated message bundle.
    List<SoyMsg> translatedMsgs = Lists.newArrayList();
    // Original (en): random{{FOO}}{{START_LINK}}slimy{{END_LINK}}
    // Translation (x-zz): {{START_LINK}}zslimy{{END_LINK}}{{FOO}}zrandom
    translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(msg)).setLocaleString("x-zz").setParts(ImmutableList.of(new SoyMsgPlaceholderPart("START_LINK", /* placeholderExample= */
    null), SoyMsgRawTextPart.of("zslimy"), new SoyMsgPlaceholderPart("END_LINK", /* placeholderExample= */
    null), new SoyMsgPlaceholderPart("FOO", /* placeholderExample= */
    null), SoyMsgRawTextPart.of("zrandom"))).build());
    // Note: This bundle has no translation for the message "dairy{$moo}".
    SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", translatedMsgs);
    // Execute the visitor.
    new InsertMsgsVisitor(msgBundle, FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(12);
    assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("<a href=\"");
    assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$goo");
    assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("\">");
    assertThat(((RawTextNode) template.getChild(5)).getRawText()).isEqualTo("zslimy");
    assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("</a>");
    assertThat(((PrintNode) template.getChild(7)).getExpr().toSourceString()).isEqualTo("$foo");
    assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("zrandom");
    assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
    assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
    assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Example 13 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class CheckNonEmptyMsgNodesPass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    for (MsgFallbackGroupNode groupNode : SoyTreeUtils.getAllNodesOfType(file, MsgFallbackGroupNode.class)) {
        for (MsgNode msg : groupNode.getChildren()) {
            if (isEmpty(msg)) {
                errorReporter.report(msg.getSourceLocation(), EMPTY_MSG_ERROR);
                // remove the whole group.
                // a number of msgnode methods throw if there are no children and having a fallback group
                // with 0 children is also unexpected.
                groupNode.getParent().removeChild(groupNode);
                break;
            }
        }
    }
}
Also used : MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgNode(com.google.template.soy.soytree.MsgNode)

Example 14 with MsgNode

use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.

the class RenderVisitorAssistantForMsgs method visitMsgFallbackGroupNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
@Override
protected void visitMsgFallbackGroupNode(MsgFallbackGroupNode node) {
    boolean foundTranslation = false;
    if (msgBundle != null) {
        for (MsgNode msg : node.getChildren()) {
            ImmutableList<SoyMsgPart> translation = msgBundle.getMsgParts(MsgUtils.computeMsgIdForDualFormat(msg));
            if (!translation.isEmpty()) {
                renderMsgFromTranslation(msg, translation, msgBundle.getLocale());
                foundTranslation = true;
                break;
            }
        }
    }
    if (!foundTranslation) {
        renderMsgFromSource(node.getChild(0));
    }
}
Also used : MsgNode(com.google.template.soy.soytree.MsgNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 15 with MsgNode

use of com.google.template.soy.soytree.MsgNode 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());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgPluralCaseNode(com.google.template.soy.soytree.MsgPluralCaseNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Aggregations

MsgNode (com.google.template.soy.soytree.MsgNode)22 Test (org.junit.Test)17 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)13 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)7 RawTextNode (com.google.template.soy.soytree.RawTextNode)6 ErrorReporter (com.google.template.soy.error.ErrorReporter)5 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)4 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)4 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)2 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)2 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 MsgPartsAndIds (com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds)1