commit 39c35cfae07b6430b50222a8b739197cf8bc15f3 Author: Ahmed Siam Date: Sat Jun 17 12:53:36 2023 +0300 Use names instead of indexes for message arguments diff --git a/src/i18n.cpp b/src/i18n.cpp index d75c175..37cfa1e 100644 --- a/src/i18n.cpp +++ b/src/i18n.cpp @@ -74,3 +74,41 @@ icu::UnicodeString I18n::format(const char* key, const std::vector arg_names, const std::vector arg_values) +{ + icu::UnicodeString pattern; + icu::UnicodeString output; + + icu::ResourceBundle resource_object = resource.get(key, status); + if (!U_SUCCESS(status)) { + std::cerr << "Error: key not found!" << std::endl; + std::cerr << u_errorName(status) << std::endl; + exit(EXIT_FAILURE); + } + + pattern = resource_object.getString(status); + if (!U_SUCCESS(status)) { + std::cerr << "Error in getting key text!" << std::endl; + std::cerr << u_errorName(status) << std::endl; + exit(EXIT_FAILURE); + } + + icu::MessageFormat formatter {pattern, status}; + if (!U_SUCCESS(status)) { + std::cerr << "Error in formatting!" << std::endl; + std::cerr << u_errorName(status) << std::endl; + exit(EXIT_FAILURE); + } + + formatter.format(arg_names.data(), arg_values.data(), arg_values.size(), output, status); + + if (!U_SUCCESS(status)) { + std::cerr << "Error in formatting!" << std::endl; + std::cerr << u_errorName(status) << std::endl; + exit(EXIT_FAILURE); + } + + return output; +} + diff --git a/src/i18n.h b/src/i18n.h index b1ef574..0479073 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -13,5 +13,6 @@ private: public: I18n(const char *locales_path); icu::UnicodeString format(const char* key, const std::vector args = {}); + icu::UnicodeString format(const char* key, const std::vector arg_names, const std::vector arg_values); }; #endif \ No newline at end of file diff --git a/src/icuformat.cpp b/src/icuformat.cpp index 886b90e..8fa2584 100644 --- a/src/icuformat.cpp +++ b/src/icuformat.cpp @@ -4,18 +4,26 @@ int main(int argc, char* argv[]) { - if (argc < 3) { - std::cout << "USAGE: icuformat \n"; + if (argc < 3 || argc % 2 == 0) { + std::cout << "USAGE: icuformat \n"; return 0; } I18n i18n {argv[1]}; - std::vector arguments; - for (int i = 3; i < argc; i++) { - arguments.push_back(argv[i]); - } + std::vector arg_names; + std::vector arg_values; + if (argc > 3) { + int arg_values_start = (argc - 3) / 2 + 3; - std::cout << i18n.format(argv[2], arguments) << std::endl; + for (int i = 3; i < arg_values_start; i++) { + arg_names.push_back(argv[i]); + } + + for (int i = arg_values_start; i < argc; i++) { + arg_values.push_back(argv[i]); + } + } + std::cout << i18n.format(argv[2], arg_names, arg_values) << std::endl; return 0; } diff --git a/tests/locales/root.txt b/tests/locales/root.txt index 6114b7d..e7243b4 100644 --- a/tests/locales/root.txt +++ b/tests/locales/root.txt @@ -1,4 +1,4 @@ root:table { simple_message:string { "Hello World" } - complex_message:string { "Hello {0}, you are {1} years old!" } + complex_message:string { "Hello {name}, you are {age} years old!" } } diff --git a/tests/test.sh b/tests/test.sh index bcf57a3..3cd0668 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -9,4 +9,4 @@ pkgdata -p locales --mode archive -d . package_list.txt icuformat="./../../src/icuformat" $icuformat locales.dat simple_message -$icuformat locales.dat complex_message Ahmed 19 +$icuformat locales.dat complex_message name age Ahmed 15