summaryrefslogtreecommitdiff
path: root/src/scripts/elua/core/gettext.lua
blob: 3659b4e04381fc179fdeaced1ac8ae41d105e129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- elua gettext module

local M = {}

local gettext = ...

local bind_textdomain         = gettext.bind_textdomain
local dgettext                = gettext.dgettext
local dngettext               = gettext.dngettext

local domains = {}

local default_domain

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.set_default_domain = function(dom)
    if not domains[dom] then return false end
    default_domain = dom
    return true
end

local floor = math.floor
local type = type

if dgettext then
    M.gettext = function(dom, msgid)
        if not msgid then
            msgid = dom
            dom   = default_domain
        end
        if not domains[dom] or not msgid then return msgid end
        local lmsgid = dgettext(dom, msgid)
        if msgid == lmsgid then
            return msgid
        end
        return lmsgid
    end
    M.dgettext = M.gettext
    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 lmsgid = dngettext(dom, msgid, plmsgid, n)
        if n == 1 then
            if msgid == lmsgid then return msgid end
        else
            if plmsgid == lmsgid then return plmsgid end
        end
        return lmsgid
    end
    M.dngettext = M.ngettext
else
    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