commit 76e208ec5e3a9be63b60b5618a1c876e1fc6bd45 Author: Ahmed Siam Date: Fri May 12 23:06:51 2023 +0300 Add i18n library diff --git a/.gitignore b/.gitignore index a8f2c54..26a6055 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,38 @@ +*.la +*.lo +*.o +*.pyc +*~ + +**/.deps/ +**/.dirstamp +test-driver + +# / +/autom4te.cache + +/compile +/config.guess +/config.status +/config.sub +/configure +/depcomp +/install-sh +/libtool +/ltmain.sh +/missing +/config.h +/config.h.in +/config.log +/INSTALL +Makefile +Makefile.in + +/aclocal.m4 + +/*.pc *.res +/src/hello_with_icu .vscode -icuformat +stamp-h1 +/build \ No newline at end of file diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..9d00566 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,3 @@ +export LOCALES_DIR=$(datadir)/$(PACKAGE_NAME)/locales + +SUBDIRS = src locales diff --git a/README.md b/README.md index d0b5691..94bf429 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,18 @@ Note: instructions expect that you are in linux and installed icu. ## How to build ``` -g++ icuformat.cpp -o icuformat `pkg-config --libs --cflags icu-uc icu-i18n icu-io` -genrb -d locales ./locales/*.txt +autoreconf --install +./configure --prefix path_of_installation +make +make install ``` ## How to use follow build instructions then run by executing: ``` -./icuformat key args +./path_of_install/bin/icuformat key ``` - -you can also use `test.sh` +or use for test +``` +./path_of_install/bin/test +``` \ No newline at end of file diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..257e4db --- /dev/null +++ b/configure.ac @@ -0,0 +1,12 @@ +AC_INIT([icuformat], [1.0], [none]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign]) +AC_PROG_CXX +AC_CONFIG_FILES([ + Makefile + src/Makefile + locales/Makefile +]) + +PKG_CHECK_MODULES(ICU, [icu-i18n, icu-io, icu-uc], CPPFLAGS="$CPPFLAGS $ICU_CFLAGS"; LIBS="$LIBS $ICU_LIBS") + +AC_OUTPUT diff --git a/icuformat b/icuformat new file mode 100644 index 0000000..5baf220 Binary files /dev/null and b/icuformat differ diff --git a/icuformat.cpp b/icuformat.cpp deleted file mode 100644 index c0670fe..0000000 --- a/icuformat.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char* argv[]) { - - if (argc == 1) { - std::cout << "USAGE: icuformat \n"; - return 0; - } - - UErrorCode status1 = U_ZERO_ERROR; - UErrorCode status2 = U_ZERO_ERROR; - - icu::UnicodeString output; - - icu::Locale system_local {}; - - icu::ResourceBundle my_resource("./locales", system_local.getName(), status1); - if (!U_SUCCESS(status1)) { - std::cout << "Error in accessing locales directory!" << std::endl; - return 1; - } - - icu::ResourceBundle pattern = my_resource.get(argv[1], status1); - if (!U_SUCCESS(status1)) { - std::cout << "Error: key not found!" << std::endl; - return 1; - } - - std::vector arguments; - for (int i = 2; i < argc; i++) { - arguments.push_back(argv[i]); - } - - icu::MessageFormat::format(pattern.getString(status1), arguments.data(), (argc - 2), output, status2); - if(!U_SUCCESS(status1)) { - std::cout << "Error in getting key text!" << std::endl; - return 1; - } - if (!U_SUCCESS(status2)) { - std::cout << "Error in formatting output!" << std::endl; - return 1; - } - - std::cout << output; - return 0; -} \ No newline at end of file diff --git a/locales/Makefile.am b/locales/Makefile.am new file mode 100644 index 0000000..f20f498 --- /dev/null +++ b/locales/Makefile.am @@ -0,0 +1,5 @@ +root.res en.res: root.txt en.txt + genrb -d . ./*.txt + +localesdir = $(LOCALES_DIR) +dist_locales_DATA = root.res en.res diff --git a/locales/root.txt b/locales/root.txt index 4ec5dc7..08c8ca0 100644 --- a/locales/root.txt +++ b/locales/root.txt @@ -1,21 +1,4 @@ root { - hello_message { "Hello World\n" } - apertium_usage { - "USAGE: {0} [-d datadir] [-f format] [-u] [in [out]]\n" - " -d datadir directory of linguistic data\n" - " -f format one of: txt (default), html, rtf, odt, odp, docx, wxml, xlsx, pptx,\n" - " xpresstag, html-noent, html-alt, latex, latex-raw, line\n" - " -a display ambiguity\n" - " -u don\'t display marks \'*\' for unknown words\n" - " -n don\'t insert period before possible sentence-ends\n" - " -m memory.tmx use a translation memory to recycle translations\n" - " -o direction translation direction using the translation memory,\n" - " by default \'direction\' is used instead\n" - " -l lists the available translation directions and exits\n" - " -V print Apertium version\n" - " -z force null-flush mode on all parts of the pipe\n" - " direction typically, LANG1-LANG2, but see modes.xml in language data\n" - " in input file (stdin by default)\n" - " out output file (stdout by default)\n" - } + hello_message { "Hello World" } + test_message { "Message for test" } } diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..60de62d --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,5 @@ +bin_PROGRAMS = icuformat test +icuformat_SOURCES = icuformat.cpp i18n.cpp +test_SOURCES = test.cpp i18n.cpp + +AM_CPPFLAGS = -DLOCALES_DIR='"$(LOCALES_DIR)"' diff --git a/src/i18n.cpp b/src/i18n.cpp new file mode 100644 index 0000000..3fea04b --- /dev/null +++ b/src/i18n.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include "i18n.h" + +I18n::I18n() : resource(LOCALES_DIR, icu::Locale().getName(), status) +{ + if (!U_SUCCESS(status)) { + std::cerr << "Error in accessing locales directory!" << std::endl; + exit(EXIT_FAILURE); + } +} + +icu::UnicodeString I18n::format(const char* key) +{ + icu::UnicodeString output; + + icu::ResourceBundle pattern = resource.get(key, status); + if (!U_SUCCESS(status)) { + std::cerr << "Error: key not found!" << std::endl; + exit(EXIT_FAILURE); + } + + output = pattern.getString(status); + if (!U_SUCCESS(status)) { + std::cerr << "Error in getting key text!" << std::endl; + exit(EXIT_FAILURE); + } + + return output; +} \ No newline at end of file diff --git a/src/i18n.h b/src/i18n.h new file mode 100644 index 0000000..073bdf9 --- /dev/null +++ b/src/i18n.h @@ -0,0 +1,14 @@ +#ifndef _I18N_ +#define _I18N_ +#include +#include + +class I18n { + private: + icu::ResourceBundle resource; + UErrorCode status = U_ZERO_ERROR; + public: + I18n(); + icu::UnicodeString format(const char* key); +}; +#endif \ No newline at end of file diff --git a/src/icuformat b/src/icuformat new file mode 100644 index 0000000..a40521d Binary files /dev/null and b/src/icuformat differ diff --git a/src/icuformat.cpp b/src/icuformat.cpp new file mode 100644 index 0000000..7fc9b49 --- /dev/null +++ b/src/icuformat.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "i18n.h" + +int main(int argc, char* argv[]) +{ + if (argc != 2) { + std::cout << "USAGE: icuformat \n"; + return 0; + } + + I18n i18n; + + std::cout << i18n.format(argv[1]) << std::endl; + + return 0; +} \ No newline at end of file diff --git a/src/test b/src/test new file mode 100644 index 0000000..6d27b05 Binary files /dev/null and b/src/test differ diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..67bfcc4 --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,10 @@ +#include +#include +#include "i18n.h" + +int main() { + I18n i18n; + + std::cout << i18n.format("test_message") << std::endl; + return 0; +} \ No newline at end of file diff --git a/test.sh b/test.sh deleted file mode 100644 index 0794d23..0000000 --- a/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -./icuformat apertium_usage "$(basename "$0")" \ No newline at end of file