libxml2:在评论部分居中文本
libxml2: center text in comment section
我尝试在 xml 文档的评论部分中将文本水平和垂直居中(在 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 ?
您可以使用 xmlNodeAddContent
and xmlNodeAddContentLen
. libxml2 also supports a couple of 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);
}
我尝试在 xml 文档的评论部分中将文本水平和垂直居中(在 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 ?
您可以使用 xmlNodeAddContent
and xmlNodeAddContentLen
. libxml2 also supports a couple of 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);
}