Branch data Line data Source code
1 : 4 : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
2 : : // SPDX-FileCopyrightText: 2009 Red Hat, Inc.
3 : :
4 : : /* exported bindtextdomain, dcgettext, dgettext, dngettext, domain, dpgettext,
5 : : gettext, LocaleCategory, ngettext, pgettext, setlocale, textdomain */
6 : :
7 : : /**
8 : : * This module provides a convenience layer for the "gettext" family of functions,
9 : : * relying on GLib for the actual implementation.
10 : : *
11 : : * Usage:
12 : : *
13 : : * const Gettext = imports.gettext;
14 : : *
15 : : * Gettext.textdomain("myapp");
16 : : * Gettext.bindtextdomain("myapp", "/usr/share/locale");
17 : : *
18 : : * let translated = Gettext.gettext("Hello world!");
19 : : */
20 : :
21 : 4 : const GLib = imports.gi.GLib;
22 : 4 : const GjsPrivate = imports.gi.GjsPrivate;
23 : :
24 : 4 : var LocaleCategory = GjsPrivate.LocaleCategory;
25 : :
26 : 4 : function setlocale(category, locale) {
27 : 4 : return GjsPrivate.setlocale(category, locale);
28 : : }
29 : :
30 : 0 : function textdomain(dom) {
31 : 0 : return GjsPrivate.textdomain(dom);
32 : : }
33 : 0 : function bindtextdomain(dom, location) {
34 : 0 : return GjsPrivate.bindtextdomain(dom, location);
35 : : }
36 : :
37 : 0 : function gettext(msgid) {
38 : 0 : return GLib.dgettext(null, msgid);
39 : : }
40 : 0 : function dgettext(dom, msgid) {
41 : 0 : return GLib.dgettext(dom, msgid);
42 : : }
43 : 0 : function dcgettext(dom, msgid, category) {
44 : 0 : return GLib.dcgettext(dom, msgid, category);
45 : : }
46 : :
47 : 0 : function ngettext(msgid1, msgid2, n) {
48 : 0 : return GLib.dngettext(null, msgid1, msgid2, n);
49 : : }
50 : 0 : function dngettext(dom, msgid1, msgid2, n) {
51 : 0 : return GLib.dngettext(dom, msgid1, msgid2, n);
52 : : }
53 : : // FIXME: missing dcngettext ?
54 : :
55 : 0 : function pgettext(context, msgid) {
56 : 0 : return GLib.dpgettext2(null, context, msgid);
57 : : }
58 : 0 : function dpgettext(dom, context, msgid) {
59 : 0 : return GLib.dpgettext2(dom, context, msgid);
60 : : }
61 : :
62 : : /**
63 : : * Create an object with bindings for gettext, ngettext,
64 : : * and pgettext bound to a particular translation domain.
65 : : *
66 : : * @param {string} domainName Translation domain string
67 : : * @returns {object} an object with gettext bindings
68 : : */
69 : 0 : function domain(domainName) {
70 : 0 : return {
71 : 0 : gettext(msgid) {
72 : 0 : return GLib.dgettext(domainName, msgid);
73 : : },
74 : :
75 : 0 : ngettext(msgid1, msgid2, n) {
76 : 0 : return GLib.dngettext(domainName, msgid1, msgid2, n);
77 : : },
78 : :
79 : 0 : pgettext(context, msgid) {
80 : 0 : return GLib.dpgettext2(domainName, context, msgid);
81 : : },
82 : : };
83 : : }
|