* @return The generated XLIFF file content.
*/
static CharSequence generateXliff(
SoyMsgBundle msgBundle, String sourceLocaleString, @Nullable String targetLocaleString) {
CharEscaper attributeEscaper = CharEscapers.xmlEscaper();
CharEscaper contentEscaper = CharEscapers.xmlContentEscaper();
boolean hasTarget = targetLocaleString != null && targetLocaleString.length() > 0;
IndentedLinesBuilder ilb = new IndentedLinesBuilder(2);
ilb.appendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
ilb.appendLine("<xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\">");
ilb.increaseIndent();
ilb.appendLineStart(
"<file original=\"SoyMsgBundle\" datatype=\"x-soy-msg-bundle\"", " xml:space=\"preserve\"",
" source-language=\"", attributeEscaper.escape(sourceLocaleString), "\"");
if (hasTarget) {
ilb.appendParts(" target-language=\"", attributeEscaper.escape(targetLocaleString), "\"");
}
ilb.appendLineEnd(">");
ilb.increaseIndent();
ilb.appendLine("<body>");
ilb.increaseIndent();
for (SoyMsg msg : msgBundle) {
// Begin 'trans-unit'.
ilb.appendLineStart("<trans-unit id=\"", Long.toString(msg.getId()), "\"");
String contentType = msg.getContentType();
if (contentType != null && contentType.length() > 0) {
String xliffDatatype = CONTENT_TYPE_TO_XLIFF_DATATYPE_MAP.get(contentType);
if (xliffDatatype == null) {
xliffDatatype = contentType; // just use the contentType string
}
ilb.appendParts(" datatype=\"", attributeEscaper.escape(xliffDatatype), "\"");
}
ilb.appendLineEnd(">");
ilb.increaseIndent();
// Source.
ilb.appendLineStart("<source>");
for (SoyMsgPart msgPart : msg.getParts()) {
if (msgPart instanceof SoyMsgRawTextPart) {
String rawText = ((SoyMsgRawTextPart) msgPart).getRawText();
ilb.append(contentEscaper.escape(rawText));
} else {
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
ilb.appendParts("<x id=\"", attributeEscaper.escape(placeholderName), "\"/>");
}
}
ilb.appendLineEnd("</source>");
// Target.
if (hasTarget) {
ilb.appendLine("<target/>");
}
// Description and meaning.
String desc = msg.getDesc();
if (desc != null && desc.length() > 0) {
ilb.appendLine(
"<note priority=\"1\" from=\"description\">", contentEscaper.escape(desc), "</note>");
}
String meaning = msg.getMeaning();
if (meaning != null && meaning.length() > 0) {
ilb.appendLine(
"<note priority=\"1\" from=\"meaning\">", contentEscaper.escape(meaning), "</note>");
}
// End 'trans-unit'.
ilb.decreaseIndent();
ilb.appendLine("</trans-unit>");