我尝试在xml doc,comment部分中水平和垂直居中文本(在po文件中翻译文本时自动将其文本化)。
为此,我混合了glib和libxml2 api:
static const xmlChar *text[] = {
N_("Configuration file"),
N_("This file was automatically generated."),
N_("Please MAKE SURE TO BACKUP THIS FILE before making changes."),
NULL
};
xmlChar *centered_text (const xmlChar **array)
{
GString *string;
guint i;
string = g_string_new ("\n");
for (i = 0; array[i]; i++)
{
gint width;
width = (80 - strlen (array[i])) / 2 + strlen (array[i]);
g_string_append_printf (string, "%*s\n", width, array[i]);
}
return g_string_free (string, FALSE);
}
.......................
xmlDocPtr doc;
xmlChar *content;
xmlNodePtr comment;
doc = xmlNewDoc ((const xmlChar *) "1.0");
content = centered_text (text);
comment = xmlNewDocComment (doc, (const xmlChar *) content);
xmlFree (content);
xmlAddChild ((xmlNodePtr) doc, comment);
输出文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Configuration file
This file was automatically generated.
Please MAKE SURE TO BACKUP THIS FILE before making changes.
-->
以下是没有对齐的意大利语翻译:
File di configurazione
Questo file è stato generato automaticamente.
Assicurati di file di questo backup prima di apportare modifiche.
有一种方法可以只使用libxml2吗?
答案 0 :(得分:0)
您可以将文字内容附加到包含xmlNodeAddContent
和xmlNodeAddContentLen
的评论节点。 libxml2 也支持几个functions for string manipulation,但不等同于g_string_append_printf
。我采用以下方法:
xmlDocPtr doc;
xmlNodePtr comment;
int i;
doc = xmlNewDoc((const xmlChar *)"1.0");
comment = xmlNewDocComment(doc, (const xmlChar *)"");
for (i = 0; array[i]; i++) {
/* 40 space characters. */
static const char space[] = " ";
int len = strlen(array[i]);
if (len < 80)
xmlNodeAddContentLen(comment, (const xmlChar *)space, (80 - len) / 2);
xmlNodeAddContentLen(comment, (const xmlChar *)array[i], len);
xmlNodeAddContentLen(comment, (const xmlChar *)"\n", 1);
}