elua: correctly wrap gettext funcs

Now, we cannot directly register funcs defined by a different
signature than the lua standard (int (*)(lua_State *)) so we
have to correctly wrap those with proper conversions etc.
This commit is contained in:
Daniel Kolesa 2017-11-24 10:53:27 +01:00
parent 6587613a58
commit 40214e16c7
2 changed files with 38 additions and 21 deletions

View File

@ -360,14 +360,43 @@ _elua_get_localeconv(lua_State *L)
return 1; return 1;
}; };
#ifdef ENABLE_NLS
static int
_elua_dgettext(lua_State *L)
{
const char *domain = luaL_checkstring(L, 1);
const char *msgid = luaL_checkstring(L, 2);
char *ret = dgettext(domain, msgid);
if (!ret)
lua_pushnil(L);
else
lua_pushstring(L, ret);
return 1;
}
static int
_elua_dngettext(lua_State *L)
{
const char *domain = luaL_checkstring(L, 1);
const char *msgid = luaL_checkstring(L, 2);
const char *plmsgid = luaL_checkstring(L, 3);
char *ret = dngettext(domain, msgid, plmsgid, luaL_checklong(L, 4));
if (!ret)
lua_pushnil(L);
else
lua_pushstring(L, ret);
return 1;
}
#endif
const luaL_Reg gettextlib[] = const luaL_Reg gettextlib[] =
{ {
{ "bind_textdomain", _elua_gettext_bind_textdomain }, { "bind_textdomain", _elua_gettext_bind_textdomain },
{ "get_message_language", _elua_get_message_language }, { "get_message_language", _elua_get_message_language },
{ "get_localeconv", _elua_get_localeconv }, { "get_localeconv", _elua_get_localeconv },
#ifdef ENABLE_NLS #ifdef ENABLE_NLS
{ "dgettext", dgettext }, { "dgettext", _elua_dgettext },
{ "dgettext", dngettext }, { "dngettext", _elua_dngettext },
#endif #endif
{ NULL, NULL } { NULL, NULL }
}; };

View File

@ -1,7 +1,5 @@
-- elua gettext module -- elua gettext module
local ffi = require("ffi")
local M = {} local M = {}
local gettext = ... local gettext = ...
@ -10,12 +8,6 @@ local bind_textdomain = gettext.bind_textdomain
local dgettext = gettext.dgettext local dgettext = gettext.dgettext
local dngettext = gettext.dngettext local dngettext = gettext.dngettext
if dgettext then
dgettext = ffi.cast("char *(*)(const char*, const char*)", dgettext)
dngettext = ffi.cast("char *(*)(const char*, const char*, const char*, "
.. "unsigned long)", dngettext)
end
local domains = {} local domains = {}
local default_domain local default_domain
@ -39,7 +31,6 @@ M.set_default_domain = function(dom)
return true return true
end end
local cast, ffistr = ffi.cast, ffi.string
local floor = math.floor local floor = math.floor
local type = type local type = type
@ -50,12 +41,11 @@ if dgettext then
dom = default_domain dom = default_domain
end end
if not domains[dom] or not msgid then return msgid end if not domains[dom] or not msgid then return msgid end
local cmsgid = cast("const char*", msgid) local lmsgid = dgettext(dom, msgid)
local lmsgid = dgettext(dom, cmsgid) if msgid == lmsgid then
if cmsgid == lmsgid then
return msgid return msgid
end end
return ffistr(lmsgid) return lmsgid
end end
M.dgettext = M.gettext M.dgettext = M.gettext
M.ngettext = function(dom, msgid, plmsgid, n) M.ngettext = function(dom, msgid, plmsgid, n)
@ -69,15 +59,13 @@ if dgettext then
if not msgid or n == 1 then return msgid end if not msgid or n == 1 then return msgid end
return plmsgid return plmsgid
end end
local cmsgid = cast("const char*", msgid) local lmsgid = dngettext(dom, msgid, plmsgid, n)
local cplmsgid = cast("const char*", plmsgid)
local lmsgid = dngettext(dom, cmsgid, cplmsgid, n)
if n == 1 then if n == 1 then
if cmsgid == lmsgid then return msgid end if msgid == lmsgid then return msgid end
else else
if cplmsgid == lmsgid then return plmsgid end if plmsgid == lmsgid then return plmsgid end
end end
return ffistr(lmsgid) return lmsgid
end end
M.dngettext = M.ngettext M.dngettext = M.ngettext
else else