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);
}
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");
}
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");
}
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());
}
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());
}
Aggregations