use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart 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.SoyMsgRawTextPart 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.SoyMsgRawTextPart 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.SoyMsgRawTextPart 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();
}
use of com.google.template.soy.msgs.restricted.SoyMsgRawTextPart in project closure-templates by google.
the class InsertMsgsVisitor method buildReplacementNodesFromTranslation.
/**
* Private helper for visitMsgFallbackGroupNode() to build the list of replacement nodes for a
* message from its translation.
*/
private void buildReplacementNodesFromTranslation(MsgNode msg, SoyMsg translation) {
currReplacementNodes = Lists.newArrayList();
for (SoyMsgPart msgPart : translation.getParts()) {
if (msgPart instanceof SoyMsgRawTextPart) {
// Append a new RawTextNode to the currReplacementNodes list.
String rawText = ((SoyMsgRawTextPart) msgPart).getRawText();
currReplacementNodes.add(new RawTextNode(nodeIdGen.genId(), rawText, msg.getSourceLocation()));
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
// Get the representative placeholder node and iterate through its contents.
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
MsgPlaceholderNode placeholderNode = msg.getRepPlaceholderNode(placeholderName);
for (StandaloneNode contentNode : placeholderNode.getChildren()) {
// simply add the content node to the currReplacementNodes list being built.
if (contentNode instanceof MsgHtmlTagNode) {
currReplacementNodes.addAll(((MsgHtmlTagNode) contentNode).getChildren());
} else {
currReplacementNodes.add(contentNode);
}
}
} else {
throw new AssertionError();
}
}
}
Aggregations