Skip to content

Commit

Permalink
Add i18n library
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedsiam0 committed May 12, 2023
1 parent f288374 commit 76e208e
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 80 deletions.
37 changes: 36 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export LOCALES_DIR=$(datadir)/$(PACKAGE_NAME)/locales

SUBDIRS = src locales
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -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
Binary file added icuformat
Binary file not shown.
52 changes: 0 additions & 52 deletions icuformat.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions locales/Makefile.am
Original file line number Diff line number Diff line change
@@ -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
21 changes: 2 additions & 19 deletions locales/root.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
root {
hello_message { "Hello World\n" }
apertium_usage {
"USAGE: {0} [-d datadir] [-f format] [-u] <direction> [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" }
}
5 changes: 5 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -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)"'
33 changes: 33 additions & 0 deletions src/i18n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <unicode/ustream.h>
#include <unicode/resbund.h>
#include <unicode/locid.h>
#include <unicode/ustring.h>
#include <unicode/msgfmt.h>
#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;
}
14 changes: 14 additions & 0 deletions src/i18n.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _I18N_
#define _I18N_
#include <unicode/resbund.h>
#include <unicode/ustring.h>

class I18n {
private:
icu::ResourceBundle resource;
UErrorCode status = U_ZERO_ERROR;
public:
I18n();
icu::UnicodeString format(const char* key);
};
#endif
Binary file added src/icuformat
Binary file not shown.
17 changes: 17 additions & 0 deletions src/icuformat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <unicode/ustream.h>
#include "i18n.h"

int main(int argc, char* argv[])
{
if (argc != 2) {
std::cout << "USAGE: icuformat <key>\n";
return 0;
}

I18n i18n;

std::cout << i18n.format(argv[1]) << std::endl;

return 0;
}
Binary file added src/test
Binary file not shown.
10 changes: 10 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include <unicode/ustream.h>
#include "i18n.h"

int main() {
I18n i18n;

std::cout << i18n.format("test_message") << std::endl;
return 0;
}
3 changes: 0 additions & 3 deletions test.sh

This file was deleted.

0 comments on commit 76e208e

Please sign in to comment.