diff options
author | Jiri Moskovcak <jmoskovc@redhat.com> | 2011-04-05 14:27:30 +0200 |
---|---|---|
committer | Jiri Moskovcak <jmoskovc@redhat.com> | 2011-04-05 14:27:30 +0200 |
commit | 04cc4f8c63a635bcdbce4d887e7c6b3483e6f141 (patch) | |
tree | 39c935b47e8d790659988f3662af5ef6e7e2c993 /src/lib/event_xml_parser.c | |
parent | 5b5056f681b1d70ebc0ed613e56c1c3820b55fc4 (diff) | |
download | abrt-04cc4f8c63a635bcdbce4d887e7c6b3483e6f141.tar.gz abrt-04cc4f8c63a635bcdbce4d887e7c6b3483e6f141.tar.xz abrt-04cc4f8c63a635bcdbce4d887e7c6b3483e6f141.zip |
make event xml description translatable
Diffstat (limited to 'src/lib/event_xml_parser.c')
-rw-r--r-- | src/lib/event_xml_parser.c | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/src/lib/event_xml_parser.c b/src/lib/event_xml_parser.c index a6aa9a1f..8bb89027 100644 --- a/src/lib/event_xml_parser.c +++ b/src/lib/event_xml_parser.c @@ -34,6 +34,9 @@ struct my_parse_data event_option_t *cur_option; }; +static char *elem_lang; +static char *locale; + static const char *const option_types[] = { "text", @@ -43,6 +46,39 @@ static const char *const option_types[] = NULL }; +static char * get_element_lang(const gchar **att_names, const gchar **att_values) +{ + char * short_locale_end = strchr(locale, '_'); + VERB2 log("locale: %s", locale); + int i; + for (i = 0; att_names[i] != NULL; ++i) + { + VERB2 log("attr: %s:%s", att_names[i], att_values[i]); + if (strcmp(att_names[i], "xml:lang") == 0) + { + if (strcmp(att_values[i], locale) == 0) + { + VERB2 log("found translation for: %s", locale); + return xstrdup(att_values[i]); + } + + /* try to match shorter locale + * e.g: "cs" with cs_CZ + */ + if (strncmp(att_values[i], locale, short_locale_end-locale) == 0) + { + VERB2 log("found translation for shortlocale: %s", locale); + return xstrndup(att_values[i], short_locale_end-locale); + } + } + } + /* if the element has no attribute then it's a default non-localized value */ + if (i == 0) + return xstrdup("C"); + /* if the element is in different language then the current locale */ + return NULL; +} + static int cmp_event_option_name_with_string(gconstpointer a, gconstpointer b) { return strcmp(((event_option_t *)a)->name, (char *)b); @@ -132,6 +168,15 @@ static void start_element(GMarkupParseContext *context, } } } + else if (strcmp(element_name, LABEL_ELEMENT) == 0) + { + elem_lang = get_element_lang(attribute_names, attribute_values); + } + else if (strcmp(element_name, DESCRIPTION_ELEMENT) == 0) + { + elem_lang = get_element_lang(attribute_names, attribute_values); + } + } // Called for close tags </foo> @@ -142,6 +187,12 @@ static void end_element(GMarkupParseContext *context, { struct my_parse_data *parse_data = user_data; + if (elem_lang != NULL) + { + free(elem_lang); + elem_lang = NULL; + } + if (strcmp(element_name, OPTION_ELEMENT) == 0) { consume_cur_option(parse_data); @@ -171,8 +222,16 @@ static void text(GMarkupParseContext *context, if (strcmp(inner_element, LABEL_ELEMENT) == 0) { VERB2 log("new label:'%s'", text_copy); - free(opt->label); - opt->label = text_copy; + /* set the value only if we found a value for the current locale (elem_lang != "C") + * OR + * the label is still NULL and we found the default "C" value + */ + if (elem_lang != NULL && ((strcmp(elem_lang, "C") != 0) + || (opt->label == NULL && (strcmp(elem_lang, "C") == 0)))) + { + free(opt->label); + opt->label = text_copy; + } return; } /* @@ -227,8 +286,12 @@ static void text(GMarkupParseContext *context, if (strcmp(inner_element, DESCRIPTION_ELEMENT) == 0) { VERB2 log("event description:'%s'", text_copy); - free(ui->description); - ui->description = text_copy; + if (elem_lang != NULL && ((strcmp(elem_lang, "C") != 0) + || (ui->description == NULL && (strcmp(elem_lang, "C") == 0)))) + { + free(ui->description); + ui->description = text_copy; + } return; } } @@ -267,7 +330,7 @@ static void error(GMarkupParseContext *context, void load_event_description_from_file(event_config_t *event_config, const char* filename) { struct my_parse_data parse_data = { event_config, NULL }; - + locale = setlocale(LC_ALL, ""); GMarkupParser parser; memset(&parser, 0, sizeof(parser)); /* just in case */ parser.start_element = &start_element; |