commit c7cb89d7033555eabeb79fe1702fd91d4ceca34b Author: Ahmed Siam Date: Thu Jun 1 20:56:58 2023 +0300 Package data diff --git a/.gitignore b/.gitignore index f7bf8b6..c1c7a76 100644 --- a/.gitignore +++ b/.gitignore @@ -32,8 +32,9 @@ Makefile.in /*.pc *.res -./src/icuformat -./src/test +*.dat +/src/icuformat +/src/test .vscode stamp-h1 /build \ No newline at end of file diff --git a/Makefile.am b/Makefile.am index 9d00566..924c064 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,4 @@ -export LOCALES_DIR=$(datadir)/$(PACKAGE_NAME)/locales +export LOCALES_DIR=$(datadir)/$(PACKAGE_NAME) +export LOCALES_DATA=$(LOCALES_DIR)/$(PACKAGE_NAME) SUBDIRS = src locales diff --git a/locales/Makefile.am b/locales/Makefile.am index f20f498..63448f9 100644 --- a/locales/Makefile.am +++ b/locales/Makefile.am @@ -1,5 +1,7 @@ -root.res en.res: root.txt en.txt - genrb -d . ./*.txt +locales.dat: root.txt en.txt + genrb -d . root.txt en.txt + echo root.res en.res > package_list.txt + pkgdata -w -p locales --mode archive -d . package_list.txt localesdir = $(LOCALES_DIR) -dist_locales_DATA = root.res en.res +dist_locales_DATA = locales.dat diff --git a/locales/en.txt b/locales/en.txt index 0bedbd6..711a2ea 100644 --- a/locales/en.txt +++ b/locales/en.txt @@ -1,2 +1,2 @@ -en { +en:table { } \ No newline at end of file diff --git a/locales/package_list.txt b/locales/package_list.txt new file mode 100644 index 0000000..d5134dd --- /dev/null +++ b/locales/package_list.txt @@ -0,0 +1 @@ +root.res en.res diff --git a/locales/root.txt b/locales/root.txt index 08c8ca0..727f17e 100644 --- a/locales/root.txt +++ b/locales/root.txt @@ -1,4 +1,5 @@ -root { - hello_message { "Hello World" } - test_message { "Message for test" } +root:table { + hello_message:string { "Hello World" } + test_message:string { "Message for test" } + complex_message:string { "Hello {0}, you are in {1}!" } } diff --git a/src/i18n.cpp b/src/i18n.cpp index 3fea04b..d884960 100644 --- a/src/i18n.cpp +++ b/src/i18n.cpp @@ -3,31 +3,45 @@ #include #include #include +#include #include "i18n.h" -I18n::I18n() : resource(LOCALES_DIR, icu::Locale().getName(), status) +I18n::I18n(const char* locales_path) : resource("", "", status) { + status = U_ZERO_ERROR; + + u_setDataDirectory(locales_path); + resource = icu::ResourceBundle("locales", icu::Locale().getName(), status); + if (!U_SUCCESS(status)) { std::cerr << "Error in accessing locales directory!" << std::endl; exit(EXIT_FAILURE); } + status = U_ZERO_ERROR; } -icu::UnicodeString I18n::format(const char* key) +icu::UnicodeString I18n::format(const char* key, const std::vector args) { + icu::UnicodeString pattern; icu::UnicodeString output; - icu::ResourceBundle pattern = resource.get(key, status); + icu::ResourceBundle resource_object = resource.get(key, status); if (!U_SUCCESS(status)) { std::cerr << "Error: key not found!" << std::endl; exit(EXIT_FAILURE); } - output = pattern.getString(status); + pattern = resource_object.getString(status); if (!U_SUCCESS(status)) { std::cerr << "Error in getting key text!" << std::endl; exit(EXIT_FAILURE); } + icu::MessageFormat::format(pattern, args.data(), args.size(), output, status); + if (!U_SUCCESS(status)) { + std::cerr << "Error in formatting!" << std::endl; + exit(EXIT_FAILURE); + } + return output; } \ No newline at end of file diff --git a/src/i18n.h b/src/i18n.h index 073bdf9..d1479ee 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -2,13 +2,16 @@ #define _I18N_ #include #include +#include +#include -class I18n { - private: +class I18n +{ +private: icu::ResourceBundle resource; - UErrorCode status = U_ZERO_ERROR; - public: - I18n(); - icu::UnicodeString format(const char* key); + UErrorCode status; +public: + I18n(const char* locales_dir); + icu::UnicodeString format(const char* key, const std::vector args = {}); }; #endif \ No newline at end of file diff --git a/src/icuformat.cpp b/src/icuformat.cpp index 7fc9b49..c0d33a7 100644 --- a/src/icuformat.cpp +++ b/src/icuformat.cpp @@ -4,14 +4,19 @@ int main(int argc, char* argv[]) { - if (argc != 2) { - std::cout << "USAGE: icuformat \n"; + if (argc < 2) { + std::cout << "USAGE: icuformat \n"; return 0; } - I18n i18n; + I18n i18n {LOCALES_DIR}; + + std::vector arguments; + for (int i = 2; i < argc; i++) { + arguments.push_back(argv[i]); + } - std::cout << i18n.format(argv[1]) << std::endl; + std::cout << i18n.format(argv[1], arguments) << std::endl; return 0; } \ No newline at end of file diff --git a/src/test.cpp b/src/test.cpp index 67bfcc4..7cf5636 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -1,10 +1,20 @@ #include #include +#include +#include #include "i18n.h" - int main() { - I18n i18n; + /* + UErrorCode status = U_ZERO_ERROR; + u_setDataDirectory(LOCALES_DATA); + void *data = udata_open(LOCALES_DATA, "res", "icuformat", &status); + if (!U_SUCCESS(status)) { + std::cerr << status << std::endl; + exit(EXIT_FAILURE); + }*/ + I18n i18n {LOCALES_DIR}; std::cout << i18n.format("test_message") << std::endl; + std::cout << i18n.format("complex_message", {"Ahmed", "Egypt"}) << std::endl; return 0; } \ No newline at end of file