Search in sources :

Example 1 with SoyMsgBundleImpl

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

the class ExtractMsgsVisitor method execOnMultipleNodes.

/**
 * Returns a SoyMsgBundle containing all messages extracted from the given nodes (locale string is
 * null).
 */
public SoyMsgBundle execOnMultipleNodes(Iterable<? extends SoyNode> nodes) {
    msgs = Lists.newArrayList();
    for (SoyNode node : nodes) {
        visit(node);
    }
    Collections.sort(msgs, SOURCE_LOCATION_ORDERING);
    return new SoyMsgBundleImpl(null, msgs);
}
Also used : SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)

Example 2 with SoyMsgBundleImpl

use of com.google.template.soy.msgs.restricted.SoyMsgBundleImpl 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 3 with SoyMsgBundleImpl

use of com.google.template.soy.msgs.restricted.SoyMsgBundleImpl 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 4 with SoyMsgBundleImpl

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

the class XliffParser method parseXliffTargetMsgs.

/**
 * Parses the content of a translated XLIFF file and creates a SoyMsgBundle.
 *
 * @param xliffContent The XLIFF content to parse.
 * @return The resulting SoyMsgBundle.
 * @throws SAXException If there's an error parsing the data.
 * @throws SoyMsgException If there's an error in parsing the data.
 */
static SoyMsgBundle parseXliffTargetMsgs(String xliffContent) throws SAXException {
    // Get a SAX parser.
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser;
    try {
        saxParser = saxParserFactory.newSAXParser();
    } catch (ParserConfigurationException pce) {
        throw new AssertionError("Could not get SAX parser for XML.");
    }
    // Construct the handler for SAX parsing.
    XliffSaxHandler xliffSaxHandler = new XliffSaxHandler();
    // Parse the XLIFF content.
    try {
        saxParser.parse(new InputSource(new StringReader(xliffContent)), xliffSaxHandler);
    } catch (IOException e) {
        throw new AssertionError("Should not fail in reading a string.");
    }
    // Build a SoyMsgBundle from the parsed data (stored in xliffSaxHandler).
    return new SoyMsgBundleImpl(xliffSaxHandler.getTargetLocaleString(), xliffSaxHandler.getMsgs());
}
Also used : InputSource(org.xml.sax.InputSource) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 5 with SoyMsgBundleImpl

use of com.google.template.soy.msgs.restricted.SoyMsgBundleImpl 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)

Aggregations

SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)6 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)4 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)4 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)3 MsgNode (com.google.template.soy.soytree.MsgNode)3 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 Test (org.junit.Test)3 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 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 ExtractMsgsVisitor (com.google.template.soy.msgs.internal.ExtractMsgsVisitor)1 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)1 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)1 FindTransitiveDepTemplatesVisitor (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor)1 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)1 PluginResolver (com.google.template.soy.soyparse.PluginResolver)1 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)1