Search in sources :

Example 6 with SoyMsg

use of com.google.template.soy.msgs.restricted.SoyMsg 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());
}
Also used : SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) URL(java.net.URL) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart) Test(org.junit.Test)

Example 7 with SoyMsg

use of com.google.template.soy.msgs.restricted.SoyMsg in project closure-templates by google.

the class SoyFileSet method pruneTranslatedMsgs.

/**
 * Prunes messages from a given message bundle, keeping only messages used in this Soy file set.
 *
 * <p>Important: Do not use directly. This is subject to change and your code will break.
 *
 * <p>Note: This method memoizes intermediate results to improve efficiency in the case that it is
 * called multiple times (which is a common case). Thus, this method will not work correctly if
 * the underlying Soy files are modified between calls to this method.
 *
 * @param origTransMsgBundle The message bundle to prune.
 * @return The pruned message bundle.
 * @throws SoyCompilationException If compilation fails.
 */
public SoyMsgBundle pruneTranslatedMsgs(SoyMsgBundle origTransMsgBundle) {
    resetErrorReporter();
    if (memoizedExtractedMsgIdsForPruning == null) {
        ParseResult result = parse(passManagerBuilder(SyntaxVersion.V1_0).allowUnknownGlobals().disableAllTypeChecking(), // can't resolve strict types
        SoyTypeRegistry.DEFAULT_UNKNOWN, new PluginResolver(PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
        throwIfErrorsPresent();
        SoyFileSetNode soyTree = result.fileSet();
        TemplateRegistry registry = result.registry();
        List<TemplateNode> allPublicTemplates = Lists.newArrayList();
        for (SoyFileNode soyFile : soyTree.getChildren()) {
            for (TemplateNode template : soyFile.getChildren()) {
                if (template.getVisibility() == Visibility.PUBLIC) {
                    allPublicTemplates.add(template);
                }
            }
        }
        Map<TemplateNode, TransitiveDepTemplatesInfo> depsInfoMap = new FindTransitiveDepTemplatesVisitor(registry).execOnMultipleTemplates(allPublicTemplates);
        TransitiveDepTemplatesInfo mergedDepsInfo = TransitiveDepTemplatesInfo.merge(depsInfoMap.values());
        SoyMsgBundle extractedMsgBundle = new ExtractMsgsVisitor().execOnMultipleNodes(mergedDepsInfo.depTemplateSet);
        ImmutableSet.Builder<Long> extractedMsgIdsBuilder = ImmutableSet.builder();
        for (SoyMsg extractedMsg : extractedMsgBundle) {
            extractedMsgIdsBuilder.add(extractedMsg.getId());
        }
        throwIfErrorsPresent();
        memoizedExtractedMsgIdsForPruning = extractedMsgIdsBuilder.build();
    }
    // ------ Prune. ------
    ImmutableList.Builder<SoyMsg> prunedTransMsgsBuilder = ImmutableList.builder();
    for (SoyMsg transMsg : origTransMsgBundle) {
        if (memoizedExtractedMsgIdsForPruning.contains(transMsg.getId())) {
            prunedTransMsgsBuilder.add(transMsg);
        }
    }
    throwIfErrorsPresent();
    return new SoyMsgBundleImpl(origTransMsgBundle.getLocaleString(), prunedTransMsgsBuilder.build());
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) ImmutableList(com.google.common.collect.ImmutableList) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) PluginResolver(com.google.template.soy.soyparse.PluginResolver) FindTransitiveDepTemplatesVisitor(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) ExtractMsgsVisitor(com.google.template.soy.msgs.internal.ExtractMsgsVisitor) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) ImmutableSet(com.google.common.collect.ImmutableSet) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle)

Example 8 with SoyMsg

use of com.google.template.soy.msgs.restricted.SoyMsg 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");
}
Also used : SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) MsgNode(com.google.template.soy.soytree.MsgNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart) Test(org.junit.Test)

Aggregations

SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)8 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)4 MsgNode (com.google.template.soy.soytree.MsgNode)4 Test (org.junit.Test)4 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)3 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)3 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Escaper (com.google.common.escape.Escaper)1 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 ExtractMsgsVisitor (com.google.template.soy.msgs.internal.ExtractMsgsVisitor)1 MsgPartsAndIds (com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds)1 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)1