commit cc86f3c9fa1d18efa819e129ee5baf1314209666 Author: Ahmed Siam Date: Tue Jul 11 21:35:40 2023 +0300 Load locales file once diff --git a/src/i18n.cpp b/src/i18n.cpp index 1db54e3..7e7a812 100644 --- a/src/i18n.cpp +++ b/src/i18n.cpp @@ -11,30 +11,37 @@ #include "i18n.h" #include + +const char* I18n::locales_path = ""; +std::unique_ptr I18n::locales_data; + I18n::I18n(const char *locales_path) : resource("", "", status) { status = U_ZERO_ERROR; + if (I18n::locales_path != locales_path) { + I18n::locales_path = locales_path; - std::ifstream file; - file.open(locales_path); + std::ifstream file; + file.open(locales_path); - if (!file.is_open()) { - std::cerr << "Error in opening data file!" << std::endl; - exit(EXIT_FAILURE); - } + if (!file.is_open()) { + std::cerr << "Error in opening data file!" << std::endl; + exit(EXIT_FAILURE); + } - std::streamsize file_size = std::filesystem::file_size(std::filesystem::path{locales_path}); + std::streamsize file_size = std::filesystem::file_size(std::filesystem::path{locales_path}); - locales_data = std::make_unique(file_size); + locales_data = std::make_unique(file_size); - file.read(locales_data.get(), file_size); + file.read(locales_data.get(), file_size); - udata_setAppData("locales", locales_data.get(), &status); + udata_setAppData("locales", locales_data.get(), &status); - if (!U_SUCCESS(status)) { - std::cerr << "Error in loading data!" << std::endl; - std::cerr << u_errorName(status) << std::endl; - exit(EXIT_FAILURE); + if (!U_SUCCESS(status)) { + std::cerr << "Error in loading data!" << std::endl; + std::cerr << u_errorName(status) << std::endl; + exit(EXIT_FAILURE); + } } resource = icu::ResourceBundle("locales", icu::Locale().getName(), status); diff --git a/src/i18n.h b/src/i18n.h index 2461186..32e223f 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -9,8 +9,9 @@ class I18n { private: + static const char *locales_path; icu::ResourceBundle resource; - std::unique_ptr locales_data; + static std::unique_ptr locales_data; UErrorCode status; public: I18n(const char *locales_path);