-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-intl.c
165 lines (141 loc) · 4.4 KB
/
be-intl.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
be-intl.c - Berry bindings to gettext
Copyright (c) 2021, Díaz Devera Víctor <mastervitronic@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/***
* Berry bindings to gettext
*
* This documentation is partial, and doesn't cover all functionality yet.
* @module intl
* @author Díaz Devera Víctor (Máster Vitronic) <mastervitronic@gmail.com>
*/
#define BERRY_MODULE
#include <berry/berry.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#define _(STRING) gettext(STRING)
#define VERSION "1.0.0"
/***
* Berry gettext library version.
* @function version
* @treturn string version string "major.minor.revision"
*/
static int m_version(bvm *vm) {
be_pushstring(vm, VERSION);
be_return(vm);
}
/***
* Look up msgid in the current default message catalog for the current
* LC_MESSAGES locale. If not found, returns msgid itself (the default
* text)
* @function gettext
* @tparam string msgid
* @treturn string result
*/
static int m_gettext(bvm *vm) {
const char * msgid = be_tostring(vm, 1);
be_pushstring(vm, _(msgid));
be_return(vm);
}
/***
* Similar to 'gettext' but select the plural form corresponding to the
* number count.
* @function ngettext
* @tparam string single
* @tparam string plural
* @tparam int count
* @treturn string result
*/
static int m_ngettext(bvm *vm) {
const char * single = be_tostring(vm, 1);
const char * plural = be_tostring(vm, 2);
const int count = be_toint(vm, 3);
be_pushstring(vm, ngettext(single,plural,count));
be_return(vm);
}
/***
* Support for the locale chosen by the user.
* @tparam string category
* @tparam string locale
* @function setlocale
*/
static int m_setlocale(bvm *vm) {
const char * category = be_tostring(vm, 1);
const char * locale = be_tostring(vm, 2);
int number;
if (strcmp(category, "LC_CTYPE") == 0) {
number = 0;
} else if (strcmp(category, "LC_NUMERIC") == 0) {
number = 1;
} else if (strcmp(category, "LC_TIME") == 0) {
number = 2;
} else if (strcmp(category, "LC_COLLATE") == 0) {
number = 3;
} else if (strcmp(category, "LC_MONETARY") == 0) {
number = 4;
} else if (strcmp(category, "LC_MESSAGES") == 0) {
number = 5;
} else if (strcmp(category, "LC_ALL") == 0) {
number = 6;
}
setlocale(number, locale);
be_return(vm);
}
/***
* Specify that the domainname message catalog will be found
* in dirname rather than in the system locale data base.
* @tparam string domainname
* @tparam string dirname
* @function bindtextdomain
*/
static int m_bindtextdomain(bvm *vm) {
const char * domainname = be_tostring(vm, 1);
const char * dirname = be_tostring(vm, 2);
bindtextdomain(domainname, dirname);
be_return(vm);
}
/***
* Set the current default message catalog to domainname.
* If domainname is null, return the current default.
* If domainname is "", reset to the default of "messages".
* @tparam string domainname
* @function textdomain
*/
static int m_textdomain(bvm *vm) {
const char * domainname = be_tostring(vm, 1);
if (strcmp(domainname, "nil") == 0){
domainname = NULL;
}
textdomain(domainname);
be_return(vm);
}
static void bind_member(bvm *vm, const char *attr, bntvfunc f) {
be_pushntvfunction(vm, f);
be_setmember(vm, -2, attr);
be_pop(vm, 1);
}
int berry_export(bvm *vm) {
be_newmodule(vm);
be_setname(vm, -1, "intl");
bind_member(vm, "version", m_version);
bind_member(vm, "gettext", m_gettext);
bind_member(vm, "ngettext", m_ngettext);
bind_member(vm, "setlocale", m_setlocale);
bind_member(vm, "bindtextdomain", m_bindtextdomain);
bind_member(vm, "textdomain", m_textdomain);
be_return(vm);
}