use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.
the class GenJsCodeVisitorAssistantForMsgs method buildGoogMsgContentStr.
/**
* Builds the message content string for a goog.getMsg() call.
*
* @param msgParts The parts of the message.
* @param doUseBracedPhs Whether to use braced placeholders.
* @return The message content string for a goog.getMsg() call.
*/
private static String buildGoogMsgContentStr(ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts);
StringBuilder msgStrSb = new StringBuilder();
for (SoyMsgPart msgPart : msgParts) {
if (msgPart instanceof SoyMsgRawTextPart) {
msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
if (doUseBracedPhs) {
// Add placeholder to message text.
msgStrSb.append("{").append(placeholderName).append("}");
} else {
// For goog.getMsg(), we must change the placeholder name to lower camel-case format.
String googMsgPlaceholderName = genGoogMsgPlaceholderName(placeholderName);
// Add placeholder to message text. Note the '$' for goog.getMsg() syntax.
msgStrSb.append("{$").append(googMsgPlaceholderName).append("}");
}
} else {
throw new AssertionError();
}
}
return msgStrSb.toString();
}
use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.
the class SoyMsgIdComputer method buildMsgContentStrForMsgIdComputation.
/**
* Private helper to build the canonical message content string that should be used for msg id
* computation.
*
* <p>Note: For people who know what "presentation" means in this context, the result string
* should be exactly the presentation string.
*
* @param msgParts The parts of the message.
* @param doUseBracedPhs Whether to use braced placeholders.
* @return The canonical message content string that should be used for msg id computation.
*/
@VisibleForTesting
static String buildMsgContentStrForMsgIdComputation(ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts);
StringBuilder msgStrSb = new StringBuilder();
for (SoyMsgPart msgPart : msgParts) {
if (msgPart instanceof SoyMsgRawTextPart) {
msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
if (doUseBracedPhs) {
msgStrSb.append('{');
}
msgStrSb.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName());
if (doUseBracedPhs) {
msgStrSb.append('}');
}
} else {
throw new AssertionError();
}
}
return msgStrSb.toString();
}
use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.
the class MsgUtils method buildMsgPartsForChildren.
// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
* 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();
}
use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.
the class MsgCompiler method partToPartExpression.
/**
* Returns an {@link Expression} that evaluates to an equivalent SoyMsgPart as the argument.
*/
private Expression partToPartExpression(SoyMsgPart part) {
if (part instanceof SoyMsgPlaceholderPart) {
return SOY_MSG_PLACEHOLDER_PART.construct(constant(((SoyMsgPlaceholderPart) part).getPlaceholderName()), constantNull(STRING_TYPE));
} else if (part instanceof SoyMsgPluralPart) {
SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) part;
List<Expression> caseExprs = new ArrayList<>(pluralPart.getCases().size());
for (Case<SoyMsgPluralCaseSpec> item : pluralPart.getCases()) {
Expression spec;
if (item.spec().getType() == Type.EXPLICIT) {
spec = SOY_MSG_PLURAL_CASE_SPEC_LONG.construct(constant(item.spec().getExplicitValue()));
} else {
spec = SOY_MSG_PLURAL_CASE_SPEC_TYPE.construct(FieldRef.enumReference(item.spec().getType()).accessor());
}
caseExprs.add(CASE_CREATE.invoke(spec, partsToPartsList(item.parts())));
}
return SOY_MSG_PURAL_PART.construct(constant(pluralPart.getPluralVarName()), constant(pluralPart.getOffset()), BytecodeUtils.asList(caseExprs));
} else if (part instanceof SoyMsgPluralRemainderPart) {
return SOY_MSG_PLURAL_REMAINDER_PART.construct(constant(((SoyMsgPluralRemainderPart) part).getPluralVarName()));
} else if (part instanceof SoyMsgRawTextPart) {
return SOY_MSG_RAW_TEXT_PART_OF.invoke(constant(((SoyMsgRawTextPart) part).getRawText(), variables));
} else if (part instanceof SoyMsgSelectPart) {
SoyMsgSelectPart selectPart = (SoyMsgSelectPart) part;
List<Expression> caseExprs = new ArrayList<>(selectPart.getCases().size());
for (Case<String> item : selectPart.getCases()) {
caseExprs.add(CASE_CREATE.invoke(item.spec() == null ? constantNull(STRING_TYPE) : constant(item.spec()), partsToPartsList(item.parts())));
}
return SOY_MSG_SELECT_PART.construct(constant(selectPart.getSelectVarName()), BytecodeUtils.asList(caseExprs));
} else {
throw new AssertionError("unrecognized part: " + part);
}
}
use of com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart in project closure-templates by google.
the class MsgFuncGenerator method processMsgPartsHelper.
/**
* 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();
}
Aggregations