Search in sources :

Example 1 with MsgPartsAndIds

use of com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds in project closure-templates by google.

the class ExtractMsgsVisitor method visitMsgNode.

@Override
protected void visitMsgNode(MsgNode node) {
    MsgPartsAndIds msgPartsAndIds = MsgUtils.buildMsgPartsAndComputeMsgIdForDualFormat(node);
    SoyMsg.Builder builder = SoyMsg.builder().setId(msgPartsAndIds.id);
    if (node.getMeaning() != null) {
        builder.setMeaning(node.getMeaning());
    }
    SoyMsg msg = builder.setDesc(node.getDesc()).setIsHidden(node.isHidden()).setContentType(node.getContentType()).setSourceLocation(node.getSourceLocation()).setIsPlrselMsg(node.isPlrselMsg()).setParts(msgPartsAndIds.parts).build();
    msgs.add(msg);
}
Also used : SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) MsgPartsAndIds(com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds)

Example 2 with MsgPartsAndIds

use of com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds in project closure-templates by google.

the class SoyNodeCompiler method visitMsgFallbackGroupNode.

/**
 * MsgFallbackGroupNodes have either one or two children. In the 2 child case the second child is
 * the {@code {fallbackmsg}} entry. For this we generate code that looks like:
 *
 * <pre>{@code
 * if (renderContext.hasMsg(primaryId)) {
 *   <render primary msg>
 * } else {
 *   <render fallback msg>
 * }
 * }</pre>
 *
 * <p>All of the logic for actually rendering {@code msg} nodes is handled by the {@link
 * MsgCompiler}.
 */
@Override
protected Statement visitMsgFallbackGroupNode(MsgFallbackGroupNode node) {
    MsgNode msg = node.getMsg();
    MsgPartsAndIds idAndParts = MsgUtils.buildMsgPartsAndComputeMsgIdForDualFormat(msg);
    ImmutableList<SoyPrintDirective> escapingDirectives = node.getEscapingDirectives();
    Statement renderDefault = getMsgCompiler().compileMessage(idAndParts, msg, escapingDirectives);
    // need to check for presence.
    if (node.hasFallbackMsg()) {
        MsgNode fallback = node.getFallbackMsg();
        MsgPartsAndIds fallbackIdAndParts = MsgUtils.buildMsgPartsAndComputeMsgIdForDualFormat(fallback);
        // TODO(lukes): consider changing the control flow here by 'inlining' the usePrimaryMsg logic
        // it would save some lookups.  Right now we will do to 2- 3 calls to
        // SoyMsgBundle.getMsgParts (each of which requires a binary search).  We could reduce that
        // to 1-2 in the worse case by inlining and storing the lists in local variables.
        IfBlock ifAvailableRenderDefault = IfBlock.create(parameterLookup.getRenderContext().usePrimaryMsg(idAndParts.id, fallbackIdAndParts.id), renderDefault);
        return ControlFlow.ifElseChain(ImmutableList.of(ifAvailableRenderDefault), Optional.of(getMsgCompiler().compileMessage(fallbackIdAndParts, fallback, escapingDirectives)));
    } else {
        return renderDefault;
    }
}
Also used : SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) Statement(com.google.template.soy.jbcsrc.restricted.Statement) IfBlock(com.google.template.soy.jbcsrc.ControlFlow.IfBlock) MsgPartsAndIds(com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds) MsgNode(com.google.template.soy.soytree.MsgNode)

Aggregations

MsgPartsAndIds (com.google.template.soy.msgs.internal.MsgUtils.MsgPartsAndIds)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)1 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)1 MsgNode (com.google.template.soy.soytree.MsgNode)1