summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira TAGOH <akira@tagoh.org>2015-11-20 20:24:15 +0900
committerAkira TAGOH <akira@tagoh.org>2015-11-20 20:24:15 +0900
commit6d381ceb2dced8ed7e6aaa8237df1f9db02b475d (patch)
tree3979d769fdfc55cc50dfff91ae3c45aa2a15b19a
parentccd2d8d96cfef940efda3accd45abb6050a88ed9 (diff)
Support BCP47-like locale representation
applied to lt_tag_convert_from_locale() and lt_tag_convert_from_locale_string() only.
-rw-r--r--liblangtag/lt-tag.c24
-rw-r--r--tests/check-tag.c6
-rw-r--r--tests/tag.c13
3 files changed, 34 insertions, 9 deletions
diff --git a/liblangtag/lt-tag.c b/liblangtag/lt-tag.c
index dcdf3aa..95fd927 100644
--- a/liblangtag/lt-tag.c
+++ b/liblangtag/lt-tag.c
@@ -1,7 +1,7 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* lt-tag.c
- * Copyright (C) 2011-2012 Akira TAGOH
+ * Copyright (C) 2011-2015 Akira TAGOH
*
* Authors:
* Akira TAGOH <akira@tagoh.org>
@@ -1122,7 +1122,7 @@ static lt_tag_t *
_lt_tag_convert_from_locale_string(const char *locale,
lt_error_t **error)
{
- char *s, *territory, *codeset, *modifier;
+ char *s, *territory, *codeset, *modifier, *script = NULL, *p;
lt_tag_t *tag;
lt_error_t *err = NULL;
@@ -1135,7 +1135,7 @@ _lt_tag_convert_from_locale_string(const char *locale,
goto bail;
} else {
lt_string_t *tag_string;
- const char *script = NULL, *variant = NULL, *privateuse = NULL;
+ const char *sscript = NULL, *variant = NULL, *privateuse = NULL;
char *transform;
modifier = strchr(s, '@');
@@ -1148,10 +1148,19 @@ _lt_tag_convert_from_locale_string(const char *locale,
*codeset = 0;
codeset++;
}
- territory = strchr(s, '_');
+ p = strchr(s, '_');
+ if (p) {
+ *p = 0;
+ p++;
+ }
+ /* For platforms where represent locale as BCP47 */
+ territory = strchr(p, '_');
if (territory) {
*territory = 0;
territory++;
+ script = p;
+ } else {
+ territory = p;
}
if (codeset &&
(lt_strcasecmp(codeset, "utf-8") == 0 ||
@@ -1161,6 +1170,7 @@ _lt_tag_convert_from_locale_string(const char *locale,
/* check if the language is a locale alias */
if (strlen(s) > 3 &&
!territory &&
+ !script &&
!codeset &&
!modifier) {
const char *loc = lt_tag_get_locale_from_locale_alias(s);
@@ -1172,12 +1182,14 @@ _lt_tag_convert_from_locale_string(const char *locale,
goto bail;
}
}
- if (!_lt_tag_convert_script_from_locale_modifier(modifier, &script))
+ if (!_lt_tag_convert_script_from_locale_modifier(modifier, &sscript))
if (!_lt_tag_convert_variant_from_locale_modifier(modifier, &variant))
privateuse = _lt_tag_convert_privaseuse_from_locale_modifier(modifier);
tag_string = lt_string_new(s);
- if (script)
+ if (sscript)
+ lt_string_append_printf(tag_string, "-%s", sscript);
+ else if (script)
lt_string_append_printf(tag_string, "-%s", script);
if (territory)
lt_string_append_printf(tag_string, "-%s", territory);
diff --git a/tests/check-tag.c b/tests/check-tag.c
index 9573d88..976303e 100644
--- a/tests/check-tag.c
+++ b/tests/check-tag.c
@@ -1,7 +1,7 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* check-tag.c
- * Copyright (C) 2011-2012 Akira TAGOH
+ * Copyright (C) 2011-2015 Akira TAGOH
*
* Authors:
* Akira TAGOH <akira@tagoh.org>
@@ -319,6 +319,10 @@ TDEF (lt_tag_convert_from_locale_string) {
fail_unless(lt_strcasecmp(lt_tag_get_string(t1), "sr-Cyrl-RS") == 0, "wrongly converted to the tag");
lt_tag_unref(t1);
+ t1 = lt_tag_convert_from_locale_string("sr_Cyrl_RS.UTF-8", NULL);
+ fail_unless(t1 != NULL, "shold be dealt as 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
/************************************************************/
diff --git a/tests/tag.c b/tests/tag.c
index 266da1f..aa8c109 100644
--- a/tests/tag.c
+++ b/tests/tag.c
@@ -1,7 +1,7 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* tag.c
- * Copyright (C) 2011-2012 Akira TAGOH
+ * Copyright (C) 2011-2015 Akira TAGOH
*
* Authors:
* Akira TAGOH <akira@tagoh.org>
@@ -34,7 +34,7 @@ main(int argc,
if (lt_strcmp0(argv[1], "help") == 0) {
help:
printf("Usage: %s <command> ...\n"
- "commands: canonicalize, ecanonicalize, dump, from_locale, lookup, match, to_locale, transform\n",
+ "commands: canonicalize, ecanonicalize, dump, from_locale, from_locale_s, lookup, match, to_locale, transform\n",
argv[0]);
} else if (lt_strcmp0(argv[1], "canonicalize") == 0) {
char *s;
@@ -55,6 +55,15 @@ main(int argc,
} else if (lt_strcmp0(argv[1], "dump") == 0) {
if (lt_tag_parse(tag, argv[2], NULL))
lt_tag_dump(tag);
+ } else if (lt_strcmp0(argv[1], "from_locale_s") == 0) {
+ lt_tag_unref(tag);
+ tag = lt_tag_convert_from_locale_string(argv[2], NULL);
+ if (tag) {
+ const char *s = lt_tag_get_string(tag);
+
+ printf("Tag: %s\n", s);
+ lt_tag_dump(tag);
+ }
} else if (lt_strcmp0(argv[1], "from_locale") == 0) {
lt_tag_unref(tag);
tag = lt_tag_convert_from_locale(NULL);