From 5198cf844ac06802e9ffc666a73b21581af18c36 Mon Sep 17 00:00:00 2001 From: Hyunjee Kim Date: Tue, 27 Mar 2018 11:32:37 +0900 Subject: [PATCH] [i18ninfo] Add Plural Rules menu Change-Id: I765a969d6e843dda67bb52de6286cc24ed17f9aa Signed-off-by: Hyunjee Kim --- i18ninfo/i18ninfo.cpp | 107 +++++++++++++++++++++++++++++++++++++-- i18ninfo/i18ninfo_argp.h | 29 +++++++++-- 2 files changed, 128 insertions(+), 8 deletions(-) diff --git a/i18ninfo/i18ninfo.cpp b/i18ninfo/i18ninfo.cpp index bfd0d35..6e7f9cb 100644 --- a/i18ninfo/i18ninfo.cpp +++ b/i18ninfo/i18ninfo.cpp @@ -224,7 +224,7 @@ char *__print_i18n_type_string(const char *type, int num) case 11: COPY_STR(str, "I18N_UNUMBER_CURRENCY_PLURAL", strlen("I18N_UNUMBER_CURRENCY_PLURAL")); break; -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) case 12: COPY_STR(str, "I18N_UNUMBER_CURRENCY_ACCOUNTING", strlen("I18N_UNUMBER_CURRENCY_ACCOUNTING")); break; @@ -1119,7 +1119,7 @@ static int __show_ubidi(char *input_text) return 0; } -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) int32_t _ustring_append_n(i18n_uchar *dest, int32_t dest_length, const i18n_uchar *src, int32_t src_length) { for (int32_t i = 0; i < src_length; ++i) { @@ -1459,6 +1459,91 @@ static int __show_measure_unit(char *input_number) } #endif +#if (__TIZEN_VER >= 5) +static int __test_plural_rules(char *input_number) +{ + + printf(" - To show plural rules\n"); + printf(" - Rule text : one: n is 1; few: n in 2..4\n"); + printf(" - Input number : %s\n", input_number ? input_number : "3"); + + printf("****************************************\n"); + + i18n_plural_rules_h rules = NULL; + + const char *rule_text = "one: n is 1; few: n in 2..4"; + PRINT_DEBUG_LOG("%s \n", rule_text); + + /* Creates a plural rules object from a description. */ + int error_code = i18n_plural_rules_create_rules_from_descr(rule_text, &rules); + + /* Create an enumeration of the keywords for the given locale. */ + i18n_uenumeration_h keywords = NULL; + error_code = i18n_plural_rules_get_keywords(rules, &keywords); + CHECK_ERROR("i18n_plural_rules_get_keywords", error_code); + + /* Get the number of the keywords that the enumeration contains. */ + int32_t count = i18n_uenumeration_count(keywords); + printf("Number of keywords: %d\n", count); + + /* Get the first keyword from the enumeration. */ + int len; + const char *keyword = i18n_uenumeration_next(keywords, &len); + CHECK_ERROR("i18n_uenumeration_next", get_last_result()); + + /* Iterate over all of the keywords found in the enumeration. */ + printf("Display list of keywords: "); + while (keyword != NULL) { + /* Display the currently processed keyword. */ + printf("%s ", keyword); + + /* Get the next keyword from the enumeration. */ + keyword = i18n_uenumeration_next(keywords, &len); + CHECK_ERROR("i18n_uenumeration_next", get_last_result()) + } + printf("\n"); + + /* Release the keywords enumeration as it is not needed anymore. */ + error_code = i18n_uenumeration_destroy(keywords); + if (error_code != I18N_ERROR_NONE) { + CHECK_ERROR("i18n_uenumeration_destroy", error_code); + } + + /* Create unumber format, to get number from user input. */ + i18n_unumber_format_h unumber; + error_code = i18n_unumber_create(I18N_UNUMBER_DECIMAL, NULL, -1, default_locale, NULL, &unumber); + CHECK_ERROR("i18n_unumber_create", error_code); + + if (!input_number) + input_number = (char *)"3"; + PRINT_DEBUG_LOG("Input number : %s\n", input_number); + + i18n_uchar i18nchar_input[BUF_SIZE] = { 0 }; + i18n_ustring_copy_ua(i18nchar_input, input_number); + + /* Parse string to double. */ + double input = i18n_unumber_parse_double(unumber, i18nchar_input, BUF_SIZE, NULL); + + /* Get rule from user defined number and print it to user. */ + i18n_uchar buffer[BUF_SIZE] = { 0 }; + int output_length = -1; + error_code = i18n_plural_rules_select_double(rules, input, BUF_SIZE, buffer, &output_length); + CHECK_ERROR("i18n_plural_rules_select_double", error_code); + + char output_buffer[BUF_SIZE] = { 0 }; + i18n_ustring_copy_au_n(output_buffer, buffer, BUF_SIZE); + printf("Matched rule: %s \n", output_buffer); + + /* Release the rules as it is not needed anymore. */ + error_code = i18n_plural_rules_destroy(rules); + if (error_code != I18N_ERROR_NONE) { + CHECK_ERROR("i18n_plural_rules_destroy", error_code); + } + + return 0; +} +#endif + void allTest() { int ret = 0, i = 0; @@ -1582,7 +1667,7 @@ void showUbidi(char *input) __show_ubidi(input); } -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) void testNumberConvert(char *input) { int ret = 0; @@ -1603,6 +1688,15 @@ void showMeasureUnit(char *input) } #endif +#if (__TIZEN_VER >= 5) +void testPluralRules(char *input) +{ + printf("\n== Test Plural Rules"); + printf("\n****************************************\n"); + __test_plural_rules(input); +} +#endif + void showDatentimeFormat(char *locale) { int ret = 0, i = 0; @@ -1810,7 +1904,7 @@ int main(int argc, char *argv[]) showUbidi(arguments.testTextArg); } -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) if (arguments.testNumberConvert) { testNumberConvert(arguments.testNumberConvertArg); } @@ -1819,6 +1913,11 @@ int main(int argc, char *argv[]) showMeasureUnit(arguments.testInputNumArg); } #endif +#if (__TIZEN_VER >= 5) + if (arguments.testPluralRules) { + testPluralRules(arguments.testPluralRulesArg); + } +#endif return 0; } diff --git a/i18ninfo/i18ninfo_argp.h b/i18ninfo/i18ninfo_argp.h index 3302eb2..d07fe62 100644 --- a/i18ninfo/i18ninfo_argp.h +++ b/i18ninfo/i18ninfo_argp.h @@ -28,9 +28,12 @@ static struct argp_option options[] = { {"set-locale", 'l', "locale", 0, "Set specific locale", 0 }, {"print-ASCII", 'p', 0, 0, "Print results as the ASCIIDoc Format", 0 }, {"show-ubidi", 'b', 0, OPTION_ARG_OPTIONAL, "Show examples of the Unicode Bidirectional Algorithm", 0 }, -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) {"test-number-converting", 'c', "Convert", OPTION_ARG_OPTIONAL, "Number converting test", 0 }, {"show-measure-unit", 'm', "Number", OPTION_ARG_OPTIONAL, "Show measure unit", 0 }, +#endif +#if (__TIZEN_VER >= 5) + {"test-plural-rules", 'r', "input", OPTION_ARG_OPTIONAL, "Show Plural Rules", 0}, #endif { 0 } }; @@ -51,10 +54,13 @@ struct arguments { int testStringIter; int printASCII; int showUbidi; -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) int testNumberConvert; int showMeasureUnit; #endif +#if (__TIZEN_VER >= 5) + int testPluralRules; +#endif char *arg1; /* arg1 */ char **strings; /* [string…] */ @@ -65,9 +71,12 @@ struct arguments { char *testStringIterArg; char *setLocale; char *testTextArg; -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) char *testNumberConvertArg; char *testInputNumArg; +#endif +#if (__TIZEN_VER >= 5) + char *testPluralRulesArg; #endif char *timezoneId; }; @@ -174,7 +183,7 @@ parse_opt(int key, char *arg, struct argp_state *state) } break; -#if (__TIZEN_VER > 4) +#if (__TIZEN_VER >= 4) case 'm': arguments->arg_flag = 16; arguments->showMeasureUnit = 1; @@ -197,6 +206,18 @@ parse_opt(int key, char *arg, struct argp_state *state) } break; +#endif +#if (__TIZEN_VER >= 5) + case 'r': + arguments->arg_flag = 17; + arguments->testPluralRules = 1; + + nextArg = state->argv[state->next]; + if (nextArg && *nextArg != '-') { + arguments->testPluralRulesArg = nextArg; + state->next++; + } + break; #endif case 'T': -- 2.34.1