[i18ninfo] Add Plural Rules menu 12/173912/2 accepted/tizen/unified/20180403.182347 submit/tizen/20180402.005209
authorHyunjee Kim <hj0426.kim@samsung.com>
Tue, 27 Mar 2018 02:32:37 +0000 (11:32 +0900)
committerHyunjee Kim <hj0426.kim@samsung.com>
Tue, 27 Mar 2018 08:17:14 +0000 (17:17 +0900)
Change-Id: I765a969d6e843dda67bb52de6286cc24ed17f9aa
Signed-off-by: Hyunjee Kim <hj0426.kim@samsung.com>
i18ninfo/i18ninfo.cpp
i18ninfo/i18ninfo_argp.h

index bfd0d35..6e7f9cb 100644 (file)
@@ -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;
 }
index 3302eb2..d07fe62 100644 (file)
@@ -28,10 +28,13 @@ 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,10 +71,13 @@ 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;
@@ -198,6 +207,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':
                arguments->arg_flag = 15;