use of com.google.template.soy.msgs.SoyMsgBundle in project closure-templates by google.
the class SoyFileSet method extractMsgs.
/**
* Extracts all messages from this Soy file set into a SoyMsgBundle (which can then be turned into
* an extracted messages file with the help of a SoyMsgBundleHandler).
*
* @return A SoyMsgBundle containing all the extracted messages (locale "en").
* @throws SoyCompilationException If compilation fails.
*/
public SoyMsgBundle extractMsgs() {
resetErrorReporter();
SoyMsgBundle bundle = doExtractMsgs();
reportWarnings();
return bundle;
}
use of com.google.template.soy.msgs.SoyMsgBundle in project closure-templates by google.
the class SoyMsgPruner method compile.
@Override
void compile(SoyFileSet.Builder sfsBuilder, Injector injector) throws IOException {
sfsBuilder.setAllowExternalCalls(allowExternalCalls);
SoyFileSet sfs = sfsBuilder.build();
SoyMsgBundleHandler msgBundleHandler = new SoyMsgBundleHandler(messagePlugin);
// Main loop.
for (int i = 0; i < inputMsgFiles.size(); i++) {
// Get the input msg bundle.
File inputMsgFilePath = inputMsgFiles.get(i);
SoyMsgBundle origTransMsgBundle = msgBundleHandler.createFromFile(inputMsgFilePath);
if (origTransMsgBundle.getLocaleString() == null) {
throw new IOException("Error opening or parsing message file " + inputMsgFilePath);
}
// Do the pruning.
SoyMsgBundle prunedTransSoyMsgBundle = sfs.pruneTranslatedMsgs(origTransMsgBundle);
// Write out the pruned msg bundle.
File outputMsgFilePath = outputMsgFiles.get(i);
msgBundleHandler.writeToTranslatedMsgsFile(prunedTransSoyMsgBundle, new OutputFileOptions(), outputMsgFilePath);
}
}
use of com.google.template.soy.msgs.SoyMsgBundle 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");
}
use of com.google.template.soy.msgs.SoyMsgBundle 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");
}
use of com.google.template.soy.msgs.SoyMsgBundle in project closure-templates by google.
the class SoyMsgBundleImplTest method testBasic.
@Test
public void testBasic() {
List<SoyMsg> inMsgs = Lists.newArrayList();
SourceLocation source1 = new SourceLocation("/path/to/source1", 10, 1, 10, 10);
inMsgs.add(SoyMsg.builder().setId(0x123).setLocaleString("x-zz").setDesc("Boo message.").setSourceLocation(source1).setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("Boo!"))).build());
inMsgs.add(SoyMsg.builder().setId(0xABC).setLocaleString("x-zz").setMeaning("abc").setDesc("").setIsHidden(true).setContentType("text/html").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("Hello, "), new SoyMsgPlaceholderPart("NAME", /* placeholderExample= */
null), SoyMsgRawTextPart.of("!"))).build());
SourceLocation source2 = new SourceLocation("/path/to/source2", 20, 1, 20, 10);
inMsgs.add(SoyMsg.builder().setId(0x123).setLocaleString("x-zz").setDesc("Boo message 2.").setIsHidden(false).setSourceLocation(source2).setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("Boo 2!"))).build());
SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", inMsgs);
assertThat(msgBundle.getLocaleString()).isEqualTo("x-zz");
assertThat(msgBundle.isRtl()).isFalse();
assertThat(msgBundle).hasSize(2);
List<SoyMsg> outMsgs = Lists.newArrayList();
for (SoyMsg msg : msgBundle) {
outMsgs.add(msg);
}
assertThat(outMsgs).hasSize(2);
SoyMsg booMsg = msgBundle.getMsg(0x123);
assertThat(outMsgs.get(0)).isEqualTo(booMsg);
assertThat(booMsg.getId()).isEqualTo(0x123);
assertThat(booMsg.getLocaleString()).isEqualTo("x-zz");
assertThat(booMsg.getMeaning()).isEqualTo(null);
assertThat(booMsg.getDesc()).isEqualTo("Boo message.");
assertThat(booMsg.isHidden()).isFalse();
assertThat(booMsg.getContentType()).isEqualTo(null);
assertThat(booMsg.getSourceLocations()).hasSize(2);
assertThat(booMsg.getSourceLocations()).containsExactly(source1, source2);
List<SoyMsgPart> booMsgParts = booMsg.getParts();
assertThat(booMsgParts).hasSize(1);
assertThat(((SoyMsgRawTextPart) booMsgParts.get(0)).getRawText()).isEqualTo("Boo!");
SoyMsg helloMsg = msgBundle.getMsg(0xABC);
assertThat(outMsgs.get(1)).isEqualTo(helloMsg);
assertThat(helloMsg.getId()).isEqualTo(0xABC);
assertThat(helloMsg.getLocaleString()).isEqualTo("x-zz");
assertThat(helloMsg.getMeaning()).isEqualTo("abc");
assertThat(helloMsg.getDesc()).isEmpty();
assertThat(helloMsg.isHidden()).isTrue();
assertThat(helloMsg.getContentType()).isEqualTo("text/html");
assertThat(helloMsg.getSourceLocations()).isEmpty();
List<SoyMsgPart> helloMsgParts = helloMsg.getParts();
assertThat(helloMsgParts).hasSize(3);
assertThat(((SoyMsgRawTextPart) helloMsgParts.get(0)).getRawText()).isEqualTo("Hello, ");
assertThat(((SoyMsgPlaceholderPart) helloMsgParts.get(1)).getPlaceholderName()).isEqualTo("NAME");
assertThat(((SoyMsgRawTextPart) helloMsgParts.get(2)).getRawText()).isEqualTo("!");
}
Aggregations