elua: ngettext support

This commit is contained in:
Daniel Kolesa 2014-05-13 17:08:05 +01:00 committed by Daniel Kolesa
parent 63ccaee7fd
commit 6d739a8746
2 changed files with 36 additions and 2 deletions

View File

@ -8,9 +8,12 @@ local gettext = ...
local bind_textdomain = gettext.bind_textdomain
local dgettext = gettext.dgettext
local dngettext = gettext.dngettext
if dgettext then
dgettext = ffi.cast("char *(*)(const char*, const char*)", dgettext)
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 = {}
@ -37,6 +40,8 @@ M.set_default_domain = function(dom)
end
local cast, ffistr = ffi.cast, ffi.string
local floor = math.floor
local type = type
if dgettext then
M.gettext = function(dom, msgid)
@ -52,8 +57,33 @@ if dgettext then
end
return ffistr(lmsgid)
end
M.ngettext = function(dom, msgid, plmsgid, n)
if not n then
plmsgid = msgid
msgid = dom
dom = default_domain
end
n = (type(n) == "number") and floor(n) or 0
if not domains[dom] then
if not msgid or n == 1 then return msgid end
return plmsgid
end
local cmsgid = cast("const char*", msgid)
local cplmsgid = cast("const char*", plmsgid)
local lmsgid = dngettext(dom, cmsgid, cplmsgid, n)
if n == 1 then
if cmsgid == lmsgid then return msgid end
else
if cplmsgid == lmsgid then return plmsgid end
end
return ffistr(lmsgid)
end
else
M.gettext = function(dom, msgid) return msgid end
M.gettext = function(dom, msgid) return msgid end
M.ngettext = function(dom, msgid, plmsgid, n)
if n == 1 then return msgid end
return plmsgid
end
end
return M

View File

@ -402,6 +402,8 @@ static int lua_main(lua_State *L) {
#if ENABLE_NLS
char *(*dgettextp)(const char*, const char*) = dgettext;
char *(*dngettextp)(const char*, const char*, const char*, unsigned long)
= dngettext;
#endif
progname = (argv[0] && argv[0][0]) ? argv[0] : "elua";
@ -475,6 +477,8 @@ static int lua_main(lua_State *L) {
#if ENABLE_NLS
lua_pushlightuserdata(L, *((void**)&dgettextp));
lua_setfield(L, -2, "dgettext");
lua_pushlightuserdata(L, *((void**)&dngettextp));
lua_setfield(L, -2, "dngettext");
#endif
lua_call(L, 1, 0);