/** * Given a list of msg parts: (a) if it contains any plural/select parts, then builds a new list * of msg parts where plural/select parts in the original msg parts are all embedded as raw text * in ICU format, (b) if it doesn't contain any plural/select parts, then simply returns the * original msg parts instead of creating a new list of identical msg parts. * * @param origMsgParts The msg parts to convert. * @return A new list of msg parts with embedded ICU syntax if the original msg parts contain * plural/select parts, otherwise the original msg parts. */ public static ImmutableList<SoyMsgPart> convertMsgPartsToEmbeddedIcuSyntax( ImmutableList<SoyMsgPart> origMsgParts) { // If origMsgParts doesn't have plural/select parts, simply return it. if (!MsgPartUtils.hasPlrselPart(origMsgParts)) { return origMsgParts; } // Build the new msg parts. ImmutableList.Builder<SoyMsgPart> newMsgPartsBuilder = ImmutableList.builder(); StringBuilder currRawTextSb = new StringBuilder(); convertMsgPartsHelper( newMsgPartsBuilder, currRawTextSb, origMsgParts, /* isInPlrselPart= */ false); if (currRawTextSb.length() > 0) { newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString())); } return newMsgPartsBuilder.build(); }
/** * Given a list of msg parts: (a) if it contains any plural/select parts, then builds a new list * of msg parts where plural/select parts in the original msg parts are all embedded as raw text * in ICU format, (b) if it doesn't contain any plural/select parts, then simply returns the * original msg parts instead of creating a new list of identical msg parts. * * @param origMsgParts The msg parts to convert. * @return A new list of msg parts with embedded ICU syntax if the original msg parts contain * plural/select parts, otherwise the original msg parts. */ public static ImmutableList<SoyMsgPart> convertMsgPartsToEmbeddedIcuSyntax( ImmutableList<SoyMsgPart> origMsgParts) { // If origMsgParts doesn't have plural/select parts, simply return it. if (!MsgPartUtils.hasPlrselPart(origMsgParts)) { return origMsgParts; } // Build the new msg parts. ImmutableList.Builder<SoyMsgPart> newMsgPartsBuilder = ImmutableList.builder(); StringBuilder currRawTextSb = new StringBuilder(); convertMsgPartsHelper( newMsgPartsBuilder, currRawTextSb, origMsgParts, /* isInPlrselPart= */ false); if (currRawTextSb.length() > 0) { newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString())); } return newMsgPartsBuilder.build(); }
currMsgParts.add(SoyMsgRawTextPart.of(currRawTextPart)); currRawTextPart = null;
currMsgParts.add(SoyMsgRawTextPart.of(currRawTextPart)); currRawTextPart = null;
@Override public void endElement(String uri, String localName, String qName) { if (qName.equals("target")) { // End 'target': Save the preceding raw text (if any). Then create a SoyMsg object from the // collected message data and add it to msgs list. if (currRawTextPart != null) { currMsgParts.add(SoyMsgRawTextPart.of(currRawTextPart)); currRawTextPart = null; } isInMsg = false; if (!currMsgParts.isEmpty()) { msgs.add( SoyMsg.builder() .setId(currMsgId) .setLocaleString(targetLocaleString) .setParts(currMsgParts) .build()); } } }
@Override public void endElement(String uri, String localName, String qName) { if (qName.equals("target")) { // End 'target': Save the preceding raw text (if any). Then create a SoyMsg object from the // collected message data and add it to msgs list. if (currRawTextPart != null) { currMsgParts.add(SoyMsgRawTextPart.of(currRawTextPart)); currRawTextPart = null; } isInMsg = false; if (!currMsgParts.isEmpty()) { msgs.add( SoyMsg.builder() .setId(currMsgId) .setLocaleString(targetLocaleString) .setParts(currMsgParts) .build()); } } }
newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString())); currRawTextSb.setLength(0);
newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString())); currRawTextSb.setLength(0);
/** * Builds the list of SoyMsgParts for all the children of a given parent node. * * @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or * MsgSelectDefaultNode. * @param msgNode The MsgNode containing 'parent'. */ private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren( BlockNode parent, MsgNode msgNode) { ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder(); for (StandaloneNode child : parent.getChildren()) { if (child instanceof RawTextNode) { String rawText = ((RawTextNode) child).getRawText(); msgParts.add(SoyMsgRawTextPart.of(rawText)); } else if (child instanceof MsgPlaceholderNode) { PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child); msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example())); } else if (child instanceof MsgPluralNode) { msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode)); } else if (child instanceof MsgSelectNode) { msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode)); } } return msgParts.build(); }
private static void doBuildMsgPartsForChildren( MsgBlockNode parent, MsgNode msgNode, ImmutableList.Builder<SoyMsgPart> msgParts) { for (StandaloneNode child : parent.getChildren()) { if (child instanceof RawTextNode) { String rawText = ((RawTextNode) child).getRawText(); msgParts.add(SoyMsgRawTextPart.of(rawText)); } else if (child instanceof MsgPlaceholderNode) { PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child); msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example())); } else if (child instanceof MsgPluralNode) { msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode)); } else if (child instanceof MsgSelectNode) { msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode)); } else if (child instanceof VeLogNode) { doBuildMsgPartsForChildren((VeLogNode) child, msgNode, msgParts); } else { throw new AssertionError("unexpected child: " + child); } } }