use of com.google.template.soy.msgs.restricted.SoyMsgPart 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.SoyMsgPart 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.SoyMsgPart in project closure-templates by google.
the class MsgUtils method buildMsgPartForSelect.
/**
* Builds the list of SoyMsgParts for the given MsgSelectNode.
*
* @param msgSelectNode The select node parsed from the Soy source.
* @param msgNode The MsgNode containing 'msgSelectNode'.
* @return A SoyMsgSelectPart.
*/
private static SoyMsgSelectPart buildMsgPartForSelect(MsgSelectNode msgSelectNode, MsgNode msgNode) {
// This is the list of the cases.
ImmutableList.Builder<SoyMsgPart.Case<String>> selectCases = ImmutableList.builder();
for (CaseOrDefaultNode child : msgSelectNode.getChildren()) {
ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
String caseValue;
if (child instanceof MsgSelectCaseNode) {
caseValue = ((MsgSelectCaseNode) child).getCaseValue();
} else if (child instanceof MsgSelectDefaultNode) {
caseValue = null;
} else {
throw new AssertionError("Unidentified node under a select node.");
}
selectCases.add(SoyMsgPart.Case.create(caseValue, caseMsgParts));
}
return new SoyMsgSelectPart(msgNode.getSelectVarName(msgSelectNode), selectCases.build());
}
use of com.google.template.soy.msgs.restricted.SoyMsgPart 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.SoyMsgPart in project closure-templates by google.
the class MsgUtils method buildMsgPartForPlural.
/**
* Builds the list of SoyMsgParts for the given MsgPluralNode.
*
* @param msgPluralNode The plural node parsed from the Soy source.
* @param msgNode The MsgNode containing 'msgPluralNode'.
* @return A SoyMsgPluralPart.
*/
private static SoyMsgPluralPart buildMsgPartForPlural(MsgPluralNode msgPluralNode, MsgNode msgNode) {
// This is the list of the cases.
ImmutableList.Builder<SoyMsgPart.Case<SoyMsgPluralCaseSpec>> pluralCases = ImmutableList.builder();
for (CaseOrDefaultNode child : msgPluralNode.getChildren()) {
ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
SoyMsgPluralCaseSpec caseSpec;
if (child instanceof MsgPluralCaseNode) {
caseSpec = new SoyMsgPluralCaseSpec(((MsgPluralCaseNode) child).getCaseNumber());
} else if (child instanceof MsgPluralDefaultNode) {
caseSpec = new SoyMsgPluralCaseSpec(Type.OTHER);
} else {
throw new AssertionError("Unidentified node under a plural node.");
}
pluralCases.add(SoyMsgPart.Case.create(caseSpec, caseMsgParts));
}
return new SoyMsgPluralPart(msgNode.getPluralVarName(msgPluralNode), msgPluralNode.getOffset(), pluralCases.build());
}
Aggregations