/** Append the placeholder to the output stream. */ private static void writePlaceholder( SoyMsgPlaceholderPart placeholder, Map<String, Object> placeholders, Appendable out) throws IOException { String placeholderName = placeholder.getPlaceholderName(); String str = (String) placeholders.get(placeholderName); if (str == null) { throw new IllegalArgumentException( "No value provided for placeholder: '" + placeholderName + "'"); } out.append(str); }
/** * Process a {@code SoyMsgPlaceholderPart} and updates the internal data structures. * * @param msgPlaceholderPart the Placeholder part. */ private void visitPart(SoyMsgPlaceholderPart msgPlaceholderPart) { // Since the content of a placeholder is not altered by translation, just render // the corresponding placeholder node. visit(msgNode.getRepPlaceholderNode(msgPlaceholderPart.getPlaceholderName())); }
/** * Process a {@code SoyMsgPlaceholderPart} and updates the internal data structures. * @param msgPlaceholderPart the Placeholder part. */ private void visitPart(SoyMsgPlaceholderPart msgPlaceholderPart) { // Since the content of a placeholder is not altered by translation, just render // the corresponding placeholder node. visit(msgNode.getRepPlaceholderNode(msgPlaceholderPart.getPlaceholderName())); }
/** * Process a {@code SoyMsgPlaceholderPart} and updates the internal data structures. * * @param msgPlaceholderPart the Placeholder part. */ private void visitPart(SoyMsgPlaceholderPart msgPlaceholderPart) { // Since the content of a placeholder is not altered by translation, just render // the corresponding placeholder node. visit(msgNode.getRepPlaceholderNode(msgPlaceholderPart.getPlaceholderName())); }
/** * Private helper to build valid Python string for a list of {@link SoyMsgPart}s. * * <p>It only processes {@link SoyMsgRawTextPart} and {@link SoyMsgPlaceholderPart} and ignores * others, because we didn't generate a direct string for plural and select nodes. * * <p>For {@link SoyMsgRawTextPart}, it appends the raw text and applies necessary escaping; For * {@link SoyMsgPlaceholderPart}, it turns the placeholder's variable name into Python replace * format. * * @param parts The SoyMsgPart parts to convert. * @param escaper A Function which provides escaping for raw text. * @return A String representing all the {@code parts} in Python. */ private static String processMsgPartsHelper( ImmutableList<SoyMsgPart> parts, Function<String, String> escaper) { StringBuilder rawMsgTextSb = new StringBuilder(); for (SoyMsgPart part : parts) { if (part instanceof SoyMsgRawTextPart) { rawMsgTextSb.append(escaper.apply(((SoyMsgRawTextPart) part).getRawText())); } if (part instanceof SoyMsgPlaceholderPart) { String phName = ((SoyMsgPlaceholderPart) part).getPlaceholderName(); rawMsgTextSb.append("{" + phName + "}"); } } return rawMsgTextSb.toString(); }
/** * Private helper to build valid Python string for a list of {@link SoyMsgPart}s. * * <p>It only processes {@link SoyMsgRawTextPart} and {@link SoyMsgPlaceholderPart} and ignores * others, because we didn't generate a direct string for plural and select nodes. * * <p>For {@link SoyMsgRawTextPart}, it appends the raw text and applies necessary escaping; For * {@link SoyMsgPlaceholderPart}, it turns the placeholder's variable name into Python replace * format. * * @param parts The SoyMsgPart parts to convert. * @param escaper A Function which provides escaping for raw text. * @return A String representing all the {@code parts} in Python. */ private static String processMsgPartsHelper( ImmutableList<SoyMsgPart> parts, Function<String, String> escaper) { StringBuilder rawMsgTextSb = new StringBuilder(); for (SoyMsgPart part : parts) { if (part instanceof SoyMsgRawTextPart) { rawMsgTextSb.append(escaper.apply(((SoyMsgRawTextPart) part).getRawText())); } if (part instanceof SoyMsgPlaceholderPart) { String phName = ((SoyMsgPlaceholderPart) part).getPlaceholderName(); rawMsgTextSb.append("{" + phName + "}"); } } return rawMsgTextSb.toString(); }
msgStrSb.append('{'); msgStrSb.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName()); if (doUseBracedPhs) { msgStrSb.append('}');
private SoyMsgPart compactPlaceholder(SoyMsgPlaceholderPart part) { return new SoyMsgPlaceholderPart( intern(part.getPlaceholderName()), /* placeholderExample=*/ null); }
msgStrSb.append('{'); msgStrSb.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName()); if (doUseBracedPhs) { msgStrSb.append('}');
private SoyMsgPart compactPlaceholder(SoyMsgPlaceholderPart part) { return new SoyMsgPlaceholderPart( intern(part.getPlaceholderName()), /* placeholderExample=*/ null); }
/** * Generates code bits for a normal {@code MsgPlaceholderNode} inside a message. * * @param placeholder A node of type {@code MsgPlaceholderNode}. * @param msgNode The enclosing {@code MsgNode} object. * @param codeGenInfo Data structure holding information on placeholder names, plural variable * names, and select variable names to be used for message code generation. */ private void genGoogMsgCodeForPlaceholder( SoyMsgPlaceholderPart placeholder, MsgNode msgNode, GoogMsgPlaceholderCodeGenInfo codeGenInfo) { // For plural/select, the placeholder is an ICU placeholder, i.e. kept in all-caps. But for // goog.getMsg(), we must change the placeholder name to lower camel-case format. String googMsgPlaceholderName = codeGenInfo.isPlrselMsg ? placeholder.getPlaceholderName() : genGoogMsgPlaceholderName(placeholder.getPlaceholderName()); if (codeGenInfo.placeholders.contains(googMsgPlaceholderName)) { return; } MsgPlaceholderNode reprNode = msgNode.getRepPlaceholderNode(placeholder.getPlaceholderName()); codeGenInfo.placeholders.put(googMsgPlaceholderName, genGoogMsgPlaceholder(reprNode)); }
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); if (doUseBracedPhs) {
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); if (doUseBracedPhs) {
/** * Appends a list of SoyMsgParts to the passed StringBuilder. * * @param stringBuilder The StringBuilder to append to. * @param soyMsgParts The list of SoyMsgParts to add. * @param doUseBracedPhs Whether to use braced placeholders. */ private static void appendMsgPartsToTcStringBuilder( StringBuilder stringBuilder, List<SoyMsgPart> soyMsgParts, boolean doUseBracedPhs) { for (SoyMsgPart msgPart : soyMsgParts) { if (msgPart instanceof SoyMsgRawTextPart) { stringBuilder.append(((SoyMsgRawTextPart) msgPart).getRawText()); } else if (msgPart instanceof SoyMsgPlaceholderPart) { if (doUseBracedPhs) { stringBuilder.append('{'); } stringBuilder.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName()); if (doUseBracedPhs) { stringBuilder.append('}'); } } else if (msgPart instanceof SoyMsgPluralRemainderPart) { stringBuilder.append(IcuSyntaxUtils.getPluralRemainderString()); } else if (msgPart instanceof SoyMsgPluralPart) { appendPluralToStringBuilder(stringBuilder, (SoyMsgPluralPart) msgPart, doUseBracedPhs); } else if (msgPart instanceof SoyMsgSelectPart) { appendSelectToStringBuilder(stringBuilder, (SoyMsgSelectPart) msgPart, doUseBracedPhs); } } }
SoyMsgPlaceholderPart placeholder) throws AssertionError { String placeholderName = placeholder.getPlaceholderName(); if (!placeholderNameToPutStatement.containsKey(placeholderName)) { MsgPlaceholderNode repPlaceholderNode = originalMsg.getRepPlaceholderNode(placeholder.getPlaceholderName()); if (repPlaceholderNode.numChildren() == 0) { throw new IllegalStateException("empty rep node for: " + placeholderName); placeholder.getPlaceholderName(), putEntyInMap.withSourceLocation(repPlaceholderNode.getSourceLocation()));
/** Private helper for visitMsgFallbackGroupNode() to render a message from its translation. */ private void renderMsgFromTranslation( MsgNode msg, ImmutableList<SoyMsgPart> msgParts, @Nullable ULocale locale) { SoyMsgPart firstPart = msgParts.get(0); if (firstPart instanceof SoyMsgPluralPart) { new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgPluralPart) firstPart); } else if (firstPart instanceof SoyMsgSelectPart) { new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgSelectPart) firstPart); } else { for (SoyMsgPart msgPart : msgParts) { if (msgPart instanceof SoyMsgRawTextPart) { RenderVisitor.append( master.getCurrOutputBufForUseByAssistants(), ((SoyMsgRawTextPart) msgPart).getRawText()); } else if (msgPart instanceof SoyMsgPlaceholderPart) { String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); visit(msg.getRepPlaceholderNode(placeholderName)); } else { throw new AssertionError(); } } } }
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); MsgPlaceholderNode placeholderNode = msg.getRepPlaceholderNode(placeholderName); for (StandaloneNode contentNode : placeholderNode.getChildren()) {
/** Private helper for visitMsgFallbackGroupNode() to render a message from its translation. */ private void renderMsgFromTranslation( MsgNode msg, ImmutableList<SoyMsgPart> msgParts, @Nullable ULocale locale) { SoyMsgPart firstPart = msgParts.get(0); if (firstPart instanceof SoyMsgPluralPart) { new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgPluralPart) firstPart); } else if (firstPart instanceof SoyMsgSelectPart) { new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgSelectPart) firstPart); } else { for (SoyMsgPart msgPart : msgParts) { if (msgPart instanceof SoyMsgRawTextPart) { RenderVisitor.append( master.getCurrOutputBufForUseByAssistants(), ((SoyMsgRawTextPart) msgPart).getRawText()); } else if (msgPart instanceof SoyMsgPlaceholderPart) { String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); visit(msg.getRepPlaceholderNode(placeholderName)); } else { throw new AssertionError(); } } } }
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName(); MsgPlaceholderNode placeholderNode = msg.getRepPlaceholderNode(placeholderName); for (StandaloneNode contentNode : placeholderNode.getChildren()) {
visit(msgNode.getRepPlaceholderNode(placeholder.getPlaceholderName()).getChild(0)); } else { throw new AssertionError("unexpected part: " + part);