From 76b445fb6df772a0aabed0640f3f5f014da87c28 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 28 Apr 2014 13:55:12 +0100 Subject: [PATCH] elua: i18n/l10n support including gettext module for user apps, bump gettext requirement to 0.18.3 (required for lua support in xgettext) --- configure.ac | 2 +- src/Makefile_Elua.am | 3 +- src/bin/elua/core/gettext.lua | 30 +++++++++++++++++++ src/bin/elua/main.c | 54 ++++++++++++++++++++++++++++++++++- src/bin/elua/main.h | 7 +++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/bin/elua/core/gettext.lua diff --git a/configure.ac b/configure.ac index d1d3507eb8..ead28b487a 100644 --- a/configure.ac +++ b/configure.ac @@ -249,7 +249,7 @@ EFL_INIT ### gettext -AM_GNU_GETTEXT_VERSION([0.17]) +AM_GNU_GETTEXT_VERSION([0.18.3]) m4_ifdef([AC_GNU_GETTEXT], [ AC_GNU_GETTEXT([external]) diff --git a/src/Makefile_Elua.am b/src/Makefile_Elua.am index ae275289af..f818793ce3 100644 --- a/src/Makefile_Elua.am +++ b/src/Makefile_Elua.am @@ -8,7 +8,8 @@ bin_elua_elua_SOURCES = \ bin_elua_elua_CPPFLAGS = -I$(top_builddir)/src/lib/efl \ -DELUA_CORE_DIR="\"$(datadir)/elua/core\"" \ - -DELUA_MODULES_DIR="\"$(datadir)/elua/modules\"" @ELUA_CFLAGS@ + -DELUA_MODULES_DIR="\"$(datadir)/elua/modules\"" @ELUA_CFLAGS@ \ + -DLOCALE_DIR=\"@LOCALE_DIR@\" bin_elua_elua_LDADD = @ELUA_LIBS@ bin_elua_elua_DEPENDENCIES = @ELUA_INTERNAL_LIBS@ diff --git a/src/bin/elua/core/gettext.lua b/src/bin/elua/core/gettext.lua new file mode 100644 index 0000000000..ede2741b79 --- /dev/null +++ b/src/bin/elua/core/gettext.lua @@ -0,0 +1,30 @@ +-- elua gettext module + +local M = {} + +local gettext = ... + +local bind_textdomain = gettext.bind_textdomain +local dgettext = gettext.dgettext + +local domains = {} + +M.register_domain = function(dom, dir) + local d, err = bind_textdomain(dom, dir) + if not d then + return false, err + end + domains[dom] = d + return true +end + +M.get_domain = function(dom) + return domains[dom] +end + +M.gettext = function(dom, msgid) + if not domains[dom] then return msgid end + return dgettext(dom, msgid) +end + +return M \ No newline at end of file diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index d323d66c52..dffe98d825 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -195,6 +195,43 @@ const luaL_reg cutillib[] = { { NULL, NULL } }; +static int gettext_bind_textdomain(lua_State *L) { + const char *textdomain = luaL_checkstring(L, 1); + const char *dirname = luaL_checkstring(L, 2); + const char *ret; + if (!textdomain[0] || !strcmp(textdomain, PACKAGE)) { + lua_pushnil(L); + lua_pushliteral(L, "invalid textdomain"); + return 2; + } + if (!(ret = bindtextdomain(textdomain, dirname))) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + bind_textdomain_codeset(textdomain, "UTF-8"); + lua_pushstring(L, ret); + return 1; +} + +static int gettext_dgettext(lua_State *L) { + const char *textdomain = luaL_checkstring(L, 1); + const char *msgid = luaL_checkstring(L, 2); + const char *lmsgid = dgettext(textdomain, msgid); + if (msgid == lmsgid) { + lua_pushvalue(L, 2); + } else { + lua_pushstring(L, lmsgid); + } + return 1; +} + +const luaL_reg gettextlib[] = { + { "bind_textdomain", gettext_bind_textdomain }, + { "dgettext" , gettext_dgettext }, + { NULL, NULL } +}; + static void print_help(const char *progname, FILE *stream) { fprintf(stream, "Usage: %s [OPTIONS] [SCRIPT [ARGS]]\n\n" "A main entry for all EFL/LuaJIT powered applications.\n\n" @@ -287,7 +324,6 @@ static int lua_main(lua_State *L) { } } snprintf(modfile, sizeof(modfile), "%s/module.lua", coref); - if (report(L, elua_loadfile(L, modfile))) { m->status = 1; return 0; @@ -301,6 +337,15 @@ static int lua_main(lua_State *L) { luaL_register(L, NULL, cutillib); lua_call(L, 2, 0); + snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref); + if (report(L, elua_loadfile(L, modfile))) { + m->status = 1; + return 0; + } + lua_createtable(L, 0, 0); + luaL_register(L, NULL, gettextlib); + lua_call(L, 1, 0); + elua_register_cache(L); lua_gc(L, LUA_GCRESTART, 0); @@ -346,6 +391,13 @@ int main(int argc, char **argv) { struct Main_Data m; lua_State *L = NULL; +#if ENABLE_NLS + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALE_DIR); + bind_textdomain_codeset(PACKAGE, "UTF-8"); + textdomain(PACKAGE); +#endif + eina_init(); if (!(el_log_domain = eina_log_domain_register("elua", diff --git a/src/bin/elua/main.h b/src/bin/elua/main.h index e5b5fb1405..8f7315b970 100644 --- a/src/bin/elua/main.h +++ b/src/bin/elua/main.h @@ -5,6 +5,13 @@ # include #endif +#ifdef ENABLE_NLS +#include +#define _(x) dgettext(PACKAGE, x) +#else +#define _(x) (x) +#endif + #ifndef ELUA_CORE_DIR #define ELUA_CORE_DIR "." #endif