Search in sources :

Example 1 with SoyMsgBundle

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;
}
Also used : SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle)

Example 2 with SoyMsgBundle

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);
    }
}
Also used : OutputFileOptions(com.google.template.soy.msgs.SoyMsgBundleHandler.OutputFileOptions) IOException(java.io.IOException) File(java.io.File) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) SoyMsgBundleHandler(com.google.template.soy.msgs.SoyMsgBundleHandler)

Example 3 with SoyMsgBundle

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");
}
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 4 with SoyMsgBundle

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");
}
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 5 with SoyMsgBundle

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("!");
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) Test(org.junit.Test)

Aggregations

SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)14 Test (org.junit.Test)7 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)5 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)4 SoyMsgBundleHandler (com.google.template.soy.msgs.SoyMsgBundleHandler)3 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)3 MsgNode (com.google.template.soy.soytree.MsgNode)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)2 OutputFileOptions (com.google.template.soy.msgs.SoyMsgBundleHandler.OutputFileOptions)2 ExtractMsgsVisitor (com.google.template.soy.msgs.internal.ExtractMsgsVisitor)2 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 PluginResolver (com.google.template.soy.soyparse.PluginResolver)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)2 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)2 File (java.io.File)2