use of com.google.template.soy.msgs.restricted.SoyMsgPart 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));
}
}
use of com.google.template.soy.msgs.restricted.SoyMsgPart in project closure-templates by google.
the class XliffMsgPluginTest method testParseTranslatedMsgsFile.
@Test
public void testParseTranslatedMsgsFile() throws Exception {
URL translatedMsgsFile = Resources.getResource(XliffMsgPluginTest.class, "test_data/test-v2_translated_x-zz.xlf");
XliffMsgPlugin msgPlugin = new XliffMsgPlugin();
SoyMsgBundle msgBundle = msgPlugin.parseTranslatedMsgsFile(Resources.toString(translatedMsgsFile, UTF_8));
assertEquals(5, msgBundle.getNumMsgs());
List<SoyMsg> msgs = Lists.newArrayList();
for (SoyMsg msg : msgBundle) {
msgs.add(msg);
}
assertEquals(5, msgs.size());
SoyMsg moscowMsg = msgs.get(0);
assertEquals(626010707674174792L, moscowMsg.getId());
List<SoyMsgPart> moscowMsgParts = moscowMsg.getParts();
assertEquals(1, moscowMsgParts.size());
assertEquals("Zmoscow", ((SoyMsgRawTextPart) moscowMsgParts.get(0)).getRawText());
assertEquals(948230478248061386L, msgs.get(1).getId());
SoyMsg mooseMsg = msgs.get(2);
assertEquals(2764913337766789440L, mooseMsg.getId());
List<SoyMsgPart> mooseMsgParts = mooseMsg.getParts();
assertEquals(7, mooseMsgParts.size());
assertEquals("Zmoose ", ((SoyMsgRawTextPart) mooseMsgParts.get(0)).getRawText());
assertEquals("START_ITALIC", ((SoyMsgPlaceholderPart) mooseMsgParts.get(1)).getPlaceholderName());
assertEquals("zalso", ((SoyMsgRawTextPart) mooseMsgParts.get(2)).getRawText());
assertEquals("END_ITALIC", ((SoyMsgPlaceholderPart) mooseMsgParts.get(3)).getPlaceholderName());
assertEquals(" zsays ", ((SoyMsgRawTextPart) mooseMsgParts.get(4)).getRawText());
assertEquals("XXX", ((SoyMsgPlaceholderPart) mooseMsgParts.get(5)).getPlaceholderName());
assertEquals(".", ((SoyMsgRawTextPart) mooseMsgParts.get(6)).getRawText());
SoyMsg cowMsg = msgs.get(3);
assertEquals(6632711700686641662L, cowMsg.getId());
List<SoyMsgPart> cowMsgParts = cowMsg.getParts();
assertEquals(3, cowMsgParts.size());
assertEquals("Zcow zsays ", ((SoyMsgRawTextPart) cowMsgParts.get(0)).getRawText());
assertEquals("MOO", ((SoyMsgPlaceholderPart) cowMsgParts.get(1)).getPlaceholderName());
assertEquals(".", ((SoyMsgRawTextPart) cowMsgParts.get(2)).getRawText());
assertEquals(8577643341484516105L, msgs.get(4).getId());
}
use of com.google.template.soy.msgs.restricted.SoyMsgPart in project closure-templates by google.
the class RenderVisitorTest method testRenderMsgStmtWithFallback.
@Test
public void testRenderMsgStmtWithFallback() throws Exception {
String templateBody = "" + " {msg desc=\"\"}\n" + " blah\n" + " {fallbackmsg desc=\"\"}\n" + " bleh\n" + " {/msg}\n";
// Without msg bundle.
assertRender(templateBody, "blah");
// With msg bundle.
SoyFileNode file = SoyFileSetParserBuilder.forFileContents("{namespace test}\n{template .foo}\n" + templateBody + "{/template}").parse().fileSet().getChild(0);
MsgFallbackGroupNode msgGroup = SoyTreeUtils.getAllNodesOfType(file.getChild(0), MsgFallbackGroupNode.class).get(0);
MsgNode fallbackMsg = msgGroup.getChild(1);
SoyMsg translatedFallbackMsg = SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(fallbackMsg)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("zbleh"))).build();
SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", Lists.newArrayList(translatedFallbackMsg));
assertThat(renderWithDataAndMsgBundle(templateBody, TEST_DATA, msgBundle)).isEqualTo("zbleh");
}
use of com.google.template.soy.msgs.restricted.SoyMsgPart in project closure-templates by google.
the class SoyMsgIdComputerTest method testKnownMsgIds.
@Test
public void testKnownMsgIds() {
// Important: Do not change these hard-coded values. Changes to the algorithm will break
// backwards compatibility.
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_WORLD_MSG_PARTS, null, null)).isEqualTo(3022994926184248873L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_WORLD_MSG_PARTS, null, "text/html")).isEqualTo(3022994926184248873L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_NAME_MSG_PARTS, null, null)).isEqualTo(6936162475751860807L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_NAME_MSG_PARTS, null, "text/html")).isEqualTo(6936162475751860807L);
assertThat(SoyMsgIdComputer.computeMsgId(PLURAL_MSG_PARTS, null, null)).isEqualTo(947930983556630648L);
assertThat(SoyMsgIdComputer.computeMsgIdUsingBracedPhs(PLURAL_MSG_PARTS, null, null)).isEqualTo(1429579464553183506L);
ImmutableList<SoyMsgPart> archiveMsgParts = ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("Archive"));
assertThat(SoyMsgIdComputer.computeMsgId(archiveMsgParts, "noun", null)).isEqualTo(7224011416745566687L);
assertThat(SoyMsgIdComputer.computeMsgId(archiveMsgParts, "verb", null)).isEqualTo(4826315192146469447L);
ImmutableList<SoyMsgPart> unicodeMsgParts = ImmutableList.of(new SoyMsgPlaceholderPart("\u2222\uEEEE", /* placeholderExample= */
null), SoyMsgRawTextPart.of("\u9EC4\u607A"));
assertThat(SoyMsgIdComputer.computeMsgId(unicodeMsgParts, null, null)).isEqualTo(7971596007260280311L);
assertThat(SoyMsgIdComputer.computeMsgId(unicodeMsgParts, null, "application/javascript")).isEqualTo(5109146044343713753L);
}
Aggregations