Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix lt_tag_convert_from_locale() to get correct result

also added lt_tag_convert_from_locale_string()
  • Loading branch information...
commit 12fe0b91273ba0563059cbbb180100c20b660b0a 1 parent 03cab72
@tagoh authored
Showing with 100 additions and 38 deletions.
  1. +29 −0 liblangtag/lt-tag.c
  2. +40 −38 liblangtag/lt-tag.h
  3. +31 −0 tests/check-tag.c
View
29 liblangtag/lt-tag.c
@@ -1136,6 +1136,7 @@ _lt_tag_convert_from_locale_string(const char *locale,
} else {
lt_string_t *tag_string;
const char *script = NULL, *variant = NULL, *privateuse = NULL;
+ char *transform;
modifier = strchr(s, '@');
if (modifier) {
@@ -1182,6 +1183,16 @@ _lt_tag_convert_from_locale_string(const char *locale,
lt_string_append_printf(tag_string, "-%s", territory);
if (variant)
lt_string_append_printf(tag_string, "-%s", variant);
+ if (!lt_tag_parse(tag, lt_string_value(tag_string), &err)) {
+ lt_string_unref(tag_string);
+ goto bail;
+ }
+ transform = lt_tag_transform(tag, &err);
+ if (!transform)
+ goto bail;
+ lt_string_clear(tag_string);
+ lt_string_append(tag_string, transform);
+ free(transform);
if (codeset || privateuse) {
lt_string_append(tag_string, "-x");
if (codeset)
@@ -1730,6 +1741,24 @@ lt_tag_convert_from_locale(lt_error_t **error)
}
/**
+ * lt_tag_convert_from_locale_string:
+ * @locale: a locale string
+ * @error: (allow-none): a #lt_error_t or %NULL.
+ *
+ * Convert @locale to the language tag.
+ *
+ * Returns: (transfer full): a #lt_tag_t, %NULL if fails.
+ */
+lt_tag_t *
+lt_tag_convert_from_locale_string(const char *locale,
+ lt_error_t **error)
+{
+ lt_return_val_if_fail (locale != NULL, NULL);
+
+ return _lt_tag_convert_from_locale_string(locale, error);
+}
+
+/**
* lt_tag_convert_to_locale:
* @tag: a #lt_tag_t.
* @error: (allow-none): a #lt_error_t or %NULL.
View
78 liblangtag/lt-tag.h
@@ -38,44 +38,46 @@ LT_BEGIN_DECLS
typedef struct _lt_tag_t lt_tag_t;
-lt_tag_t *lt_tag_new (void);
-lt_tag_t *lt_tag_ref (lt_tag_t *tag);
-void lt_tag_unref (lt_tag_t *tag);
-lt_bool_t lt_tag_parse (lt_tag_t *tag,
- const char *tag_string,
- lt_error_t **error);
-lt_bool_t lt_tag_parse_with_extra_token(lt_tag_t *tag,
- const char *tag_string,
- lt_error_t **error);
-void lt_tag_clear (lt_tag_t *tag);
-lt_tag_t *lt_tag_copy (const lt_tag_t *tag);
-lt_bool_t lt_tag_truncate (lt_tag_t *tag,
- lt_error_t **error);
-const char *lt_tag_get_string (lt_tag_t *tag);
-char *lt_tag_canonicalize (lt_tag_t *tag,
- lt_error_t **error);
-char *lt_tag_convert_to_locale (lt_tag_t *tag,
- lt_error_t **error);
-lt_tag_t *lt_tag_convert_from_locale (lt_error_t **error);
-void lt_tag_dump (const lt_tag_t *tag);
-lt_bool_t lt_tag_compare (const lt_tag_t *v1,
- const lt_tag_t *v2);
-lt_bool_t lt_tag_match (const lt_tag_t *v1,
- const char *v2,
- lt_error_t **error);
-char *lt_tag_lookup (const lt_tag_t *tag,
- const char *pattern,
- lt_error_t **error);
-char *lt_tag_transform (lt_tag_t *tag,
- lt_error_t **error);
-const lt_lang_t *lt_tag_get_language (const lt_tag_t *tag);
-const lt_extlang_t *lt_tag_get_extlang (const lt_tag_t *tag);
-const lt_script_t *lt_tag_get_script (const lt_tag_t *tag);
-const lt_region_t *lt_tag_get_region (const lt_tag_t *tag);
-const lt_list_t *lt_tag_get_variants (const lt_tag_t *tag);
-const lt_extension_t *lt_tag_get_extension (const lt_tag_t *tag);
-const lt_string_t *lt_tag_get_privateuse (const lt_tag_t *tag);
-const lt_grandfathered_t *lt_tag_get_grandfathered (const lt_tag_t *tag);
+lt_tag_t *lt_tag_new (void);
+lt_tag_t *lt_tag_ref (lt_tag_t *tag);
+void lt_tag_unref (lt_tag_t *tag);
+lt_bool_t lt_tag_parse (lt_tag_t *tag,
+ const char *tag_string,
+ lt_error_t **error);
+lt_bool_t lt_tag_parse_with_extra_token (lt_tag_t *tag,
+ const char *tag_string,
+ lt_error_t **error);
+void lt_tag_clear (lt_tag_t *tag);
+lt_tag_t *lt_tag_copy (const lt_tag_t *tag);
+lt_bool_t lt_tag_truncate (lt_tag_t *tag,
+ lt_error_t **error);
+const char *lt_tag_get_string (lt_tag_t *tag);
+char *lt_tag_canonicalize (lt_tag_t *tag,
+ lt_error_t **error);
+char *lt_tag_convert_to_locale (lt_tag_t *tag,
+ lt_error_t **error);
+lt_tag_t *lt_tag_convert_from_locale (lt_error_t **error);
+lt_tag_t *lt_tag_convert_from_locale_string(const char *locale,
+ lt_error_t **error);
+void lt_tag_dump (const lt_tag_t *tag);
+lt_bool_t lt_tag_compare (const lt_tag_t *v1,
+ const lt_tag_t *v2);
+lt_bool_t lt_tag_match (const lt_tag_t *v1,
+ const char *v2,
+ lt_error_t **error);
+char *lt_tag_lookup (const lt_tag_t *tag,
+ const char *pattern,
+ lt_error_t **error);
+char *lt_tag_transform (lt_tag_t *tag,
+ lt_error_t **error);
+const lt_lang_t *lt_tag_get_language (const lt_tag_t *tag);
+const lt_extlang_t *lt_tag_get_extlang (const lt_tag_t *tag);
+const lt_script_t *lt_tag_get_script (const lt_tag_t *tag);
+const lt_region_t *lt_tag_get_region (const lt_tag_t *tag);
+const lt_list_t *lt_tag_get_variants (const lt_tag_t *tag);
+const lt_extension_t *lt_tag_get_extension (const lt_tag_t *tag);
+const lt_string_t *lt_tag_get_privateuse (const lt_tag_t *tag);
+const lt_grandfathered_t *lt_tag_get_grandfathered (const lt_tag_t *tag);
LT_END_DECLS
View
31 tests/check-tag.c
@@ -267,6 +267,36 @@ TDEF (lt_tag_transform) {
lt_tag_unref(t1);
} TEND
+TDEF (lt_tag_convert_from_locale_string) {
+ lt_tag_t *t1;
+
+ t1 = lt_tag_convert_from_locale_string("ja_JP.UTF-8", NULL);
+ fail_unless(t1 != NULL, "should be valid locale");
+ fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "ja-Jpan-JP") == 0, "wrongly converted to the tag");
+ lt_tag_unref(t1);
+
+ t1 = lt_tag_convert_from_locale_string("zh_CN.UTF-8", NULL);
+ fail_unless(t1 != NULL, "should be valid locale");
+ fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "zh-Hans-CN") == 0, "wrongly converted to the tag");
+ lt_tag_unref(t1);
+
+ t1 = lt_tag_convert_from_locale_string("zh_TW.UTF-8", NULL);
+ fail_unless(t1 != NULL, "should be valid locale");
+ fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "zh-Hant-TW") == 0, "wrongly converted to the tag");
+ lt_tag_unref(t1);
+
+ t1 = lt_tag_convert_from_locale_string("zh_SG.UTF-8", NULL);
+ fail_unless(t1 != NULL, "should be valid locale");
+ fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "zh-Hans-SG") == 0, "wrongly converted to the tag");
+ lt_tag_unref(t1);
+
+ t1 = lt_tag_convert_from_locale_string("sr_RS.UTF-8", NULL);
+ fail_unless(t1 != NULL, "should be valid locale");
+ fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "sr-Cyrl-RS") == 0, "wrongly converted to the tag");
+ lt_tag_unref(t1);
+
+} TEND
+
/************************************************************/
Suite *
tester_suite(void)
@@ -281,6 +311,7 @@ tester_suite(void)
T (lt_tag_canonicalize);
T (lt_tag_match);
T (lt_tag_transform);
+ T (lt_tag_convert_from_locale_string);
suite_add_tcase(s, tc);
Please sign in to comment.
Something went wrong with that request. Please try again.