commit a96fd08d68887e0ab96766f1faf312f560fabbc8 Author: Ahmed Siam Date: Tue Jul 18 08:56:33 2023 +0300 Solve the issue appeared when i use several .dat files in the same process. diff --git a/src/i18n.cpp b/src/i18n.cpp index 7e7a812..d647744 100644 --- a/src/i18n.cpp +++ b/src/i18n.cpp @@ -11,16 +11,13 @@ #include "i18n.h" #include +std::unordered_map> I18n::locales_data; -const char* I18n::locales_path = ""; -std::unique_ptr I18n::locales_data; - -I18n::I18n(const char *locales_path) : resource("", "", status) +I18n::I18n(const char *locales_path, std::string package_name) : resource("", "", status) { status = U_ZERO_ERROR; - if (I18n::locales_path != locales_path) { - I18n::locales_path = locales_path; - + if (locales_data.find(package_name) == locales_data.end()) { + std::ifstream file; file.open(locales_path); @@ -31,11 +28,11 @@ I18n::I18n(const char *locales_path) : resource("", "", status) std::streamsize file_size = std::filesystem::file_size(std::filesystem::path{locales_path}); - locales_data = std::make_unique(file_size); + locales_data[package_name] = std::make_unique(file_size); - file.read(locales_data.get(), file_size); - - udata_setAppData("locales", locales_data.get(), &status); + file.read(locales_data[package_name].get(), file_size); + + udata_setAppData(package_name.c_str(), locales_data[package_name].get(), &status); if (!U_SUCCESS(status)) { std::cerr << "Error in loading data!" << std::endl; @@ -43,8 +40,8 @@ I18n::I18n(const char *locales_path) : resource("", "", status) exit(EXIT_FAILURE); } } - - resource = icu::ResourceBundle("locales", icu::Locale().getName(), status); + + resource = icu::ResourceBundle(package_name.c_str(), icu::Locale().getName(), status); if (!U_SUCCESS(status)) { std::cerr << "Error in initializing resource bundle" << std::endl; diff --git a/src/i18n.h b/src/i18n.h index 32e223f..cf9b816 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -5,16 +5,16 @@ #include #include #include +#include class I18n { private: - static const char *locales_path; + static std::unordered_map> locales_data; icu::ResourceBundle resource; - static std::unique_ptr locales_data; UErrorCode status; public: - I18n(const char *locales_path); + I18n(const char *locales_path, std::string package_name); 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); diff --git a/src/icuformat.cpp b/src/icuformat.cpp index 8fa2584..6f65d2f 100644 --- a/src/icuformat.cpp +++ b/src/icuformat.cpp @@ -4,19 +4,19 @@ int main(int argc, char* argv[]) { - if (argc < 3 || argc % 2 == 0) { - std::cout << "USAGE: icuformat \n"; + if (argc < 4 || argc % 2 != 0) { + std::cout << "USAGE: icuformat \n"; return 0; } - I18n i18n {argv[1]}; + I18n i18n {argv[1], argv[2]}; std::vector arg_names; std::vector arg_values; - if (argc > 3) { - int arg_values_start = (argc - 3) / 2 + 3; + if (argc > 4) { + int arg_values_start = (argc - 4) / 2 + 4; - for (int i = 3; i < arg_values_start; i++) { + for (int i = 4; i < arg_values_start; i++) { arg_names.push_back(argv[i]); } @@ -24,6 +24,6 @@ int main(int argc, char* argv[]) arg_values.push_back(argv[i]); } } - std::cout << i18n.format(argv[2], arg_names, arg_values) << std::endl; + std::cout << i18n.format(argv[3], arg_names, arg_values) << std::endl; return 0; } diff --git a/tests/test.sh b/tests/test.sh index 3cd0668..f8ef03b 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -8,5 +8,5 @@ pkgdata -p locales --mode archive -d . package_list.txt icuformat="./../../src/icuformat" -$icuformat locales.dat simple_message -$icuformat locales.dat complex_message name age Ahmed 15 +$icuformat locales.dat locales simple_message +$icuformat locales.dat locales complex_message name age Ahmed 15