commit 18ea1508c281ae29c6f3209ec8bfe1b2b10b2eb1 Author: Ahmed Siam Date: Sun Mar 12 16:28:45 2023 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8f2c54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.res +.vscode +icuformat diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecda966 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# icuformat + +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 +``` +## How to use +follow build instructions then run by executing: +``` +./icuformat key args +``` +you can also use `test.sh` \ No newline at end of file diff --git a/icuformat.cpp b/icuformat.cpp new file mode 100644 index 0000000..c0670fe --- /dev/null +++ b/icuformat.cpp @@ -0,0 +1,52 @@ +#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/en.txt b/locales/en.txt new file mode 100644 index 0000000..0bedbd6 --- /dev/null +++ b/locales/en.txt @@ -0,0 +1,2 @@ +en { +} \ No newline at end of file diff --git a/locales/root.txt b/locales/root.txt new file mode 100644 index 0000000..4ec5dc7 --- /dev/null +++ b/locales/root.txt @@ -0,0 +1,21 @@ +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" + } +} diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..0794d23 --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +./icuformat apertium_usage "$(basename "$0")" \ No newline at end of file