Source file gettext.ml
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
open GettextTypes
open GettextCompat
open GettextUtils
open GettextModules
open Lexing
type global_type = { t : t option; realize : t -> t'; t' : t' option }
let dummy_realize _t _printf_format _textdomain str _str_plural _category = str
let default_realize = dummy_realize
let global_lock = ref (fun () -> ())
let global_unlock = ref (fun () -> ())
let global = ref { t = None; realize = default_realize; t' = None }
let get_global_t () =
let t =
!global_lock ();
!global.t
in
!global_unlock ();
match t with Some t -> t | None -> raise GettextUninitialized
let set_global_t t =
let () =
!global_lock ();
global := { !global with t = Some t; t' = None }
in
!global_unlock ()
let set_global_realize realize =
let () =
!global_lock ();
global := { !global with realize; t' = None }
in
!global_unlock ()
let get_global_t' () =
let t' =
!global_lock ();
match !global.t' with
| None ->
let t =
match !global.t with
| Some t -> t
| None -> raise GettextUninitialized
in
let t' = !global.realize t in
global := { !global with t' = Some t' };
t'
| Some t' -> t'
in
!global_unlock ();
t'
module Library (Init : INIT_TYPE) = struct
let init = (Init.textdomain, Init.codeset, Init.dir) :: Init.dependencies
let s_ str = dgettext (get_global_t' ()) Init.textdomain str
let f_ str = fdgettext (get_global_t' ()) Init.textdomain str
let sn_ str = dngettext (get_global_t' ()) Init.textdomain str
let fn_ str = fdngettext (get_global_t' ()) Init.textdomain str
end
module GettextGettext = Library (struct
let textdomain = "ocaml-gettext"
let codeset = None
let dir = None
let dependencies = []
end)
let init = GettextGettext.init
let string_of_exception exc =
let f_ x = GettextGettext.f_ x in
let s_ x = GettextGettext.s_ x in
let spf x = Printf.sprintf x in
let string_of_pos lexbuf =
let char_pos = lexbuf.lex_curr_p.pos_cnum - lexbuf.lex_curr_p.pos_bol in
let line_pos = lexbuf.lex_curr_p.pos_lnum in
spf (f_ "line %d character %d") line_pos char_pos
in
match exc with
| CompileProblemReadingFile (fln, error) ->
spf (f_ "Problem reading file %s: %s.") fln error
| CompileExtractionFailed (fln, cmd, status) ->
spf
(f_ "Problem while extracting %s: command %S exits with code %d.")
fln cmd status
| CompileExtractionInterrupted (fln, cmd, signal) ->
spf
(f_ "Problem while extracting %s: command %S killed by signal %d.")
fln cmd signal
| DomainFileDoesntExist lst ->
spf
(f_ "Cannot find an appropriate ocaml-gettext compiled file (%s).")
(string_of_list lst)
| GettextUninitialized -> s_ "Ocaml-gettext library is not initialized"
| MoInvalidOptions (lexbuf, text) ->
spf
(f_ "Error while processing parsing of options at %s: %S.")
(string_of_pos lexbuf) text
| MoInvalidPlurals (lexbuf, text) ->
spf
(f_ "Error while processing parsing of plural at %s: %S.")
(string_of_pos lexbuf) text
| MoInvalidContentType (lexbuf, text) ->
spf
(f_ "Error while processing parsing of content-type at %s: %S.")
(string_of_pos lexbuf) text
| MoInvalidFile ->
s_ "MO file provided is not encoded following ocaml-gettext convention."
| MoInvalidTranslationSingular (str, x) ->
spf (f_ "Trying to fetch the plural form %d of a singular form %S.") x str
| MoInvalidTranslationPlural (lst, x) ->
spf
(f_ "Trying to fetch the plural form %d of plural form %s.")
x (string_of_list lst)
| MoJunk (id, lst) ->
spf
(f_ "Junk at the end of the plural form id %S: %s.")
id (string_of_list lst)
| MoEmptyEntry -> s_ "An empty entry has been encounter."
| MoInvalidHeaderNegativeStrings -> s_ "Number of strings is negative."
| MoInvalidHeaderTableStringOutOfBound ((b1, e1), (b2, e2)) ->
spf
(f_
"Offset of string table is out of bound ([%ld,%ld] should be in \
[%ld,%ld]).")
b1 e1 b2 e2
| MoInvalidHeaderTableTranslationOutOfBound ((b1, e1), (b2, e2)) ->
spf
(f_
"Offset of translation table is out of bound ([%ld,%ld] should be \
in [%ld,%ld]).")
b1 e1 b2 e2
| MoInvalidHeaderTableTranslationStringOverlap ((b1, e1), (b2, e2)) ->
spf
(f_
"Translation table and string table overlap ([%ld,%ld] and \
[%ld,%ld] have a non empty intersection).")
b1 e1 b2 e2
| MoInvalidStringOutOfBound (max, cur) ->
spf
(f_ "Out of bound access when trying to find a string (%d < %d).")
max cur
| MoInvalidTranslationOutOfBound (max, cur) ->
spf
(f_ "Out of bound access when trying to find a translation (%d < %d).")
max cur
| MoCannotOpenFile fln -> spf (f_ "Could not open file %s.") fln
| PoInvalidFile (s, lexbuf, _chn) ->
spf
(f_ "Error while processing parsing of PO file: %S at %s.")
s (string_of_pos lexbuf)
| PoFileInvalidIndex (id, i) ->
spf
(f_
"Error while processing parsing of PO file, in msgid %S, %d index \
is out of bound.")
id i
| PoFileDoesntExist fl ->
spf (f_ "Error while trying to load PO file %s, file doesn't exist.") fl
| PoInconsistentMerge (str1, str2) ->
spf
(f_ "Error while merging two PO files: %S and %S cannot be merged.")
str1 str2
| TranslateStringNotFound str -> spf (f_ "Cannot find string %S.") str
| LocalePosixUnparseable str ->
spf (f_ "Unable to parse the POSIX language environment variable %s") str
| _ -> Printexc.to_string exc
module Program (Init : INIT_TYPE) (Realize : REALIZE_TYPE) = struct
let textdomain = Init.textdomain
let dependencies =
(Init.textdomain, Init.codeset, Init.dir) :: Init.dependencies
let init =
let f_ x = GettextGettext.f_ x in
let s_ x = GettextGettext.s_ x in
let spf x = Printf.sprintf x in
let () = set_global_t (GettextModules.create textdomain) in
let () =
set_global_t
(List.fold_left
(fun t (textdomain, codeset_opt, dir_opt) ->
upgrade_textdomain t textdomain (codeset_opt, dir_opt))
(get_global_t ()) dependencies)
in
let () = set_global_realize Realize.realize in
( [
( "--gettext-failsafe",
Arg.Symbol
( [ "ignore"; "inform-stderr"; "raise-exception" ],
fun x ->
match x with
| "ignore" ->
set_global_t { (get_global_t ()) with failsafe = Ignore }
| "inform-stderr" ->
set_global_t
{
(get_global_t ()) with
failsafe = InformStderr string_of_exception;
}
| "raise-exception" ->
set_global_t
{ (get_global_t ()) with failsafe = RaiseException }
| _ -> () ),
spf
(f_ " Choose how to handle failure in ocaml-gettext. Default: %s.")
(match (get_global_t ()).failsafe with
| Ignore -> "ignore"
| InformStderr _ -> "inform-stderr"
| RaiseException -> "raise-exception") );
( "--gettext-disable",
Arg.Unit (fun () -> set_global_realize dummy_realize),
s_
" Disable the translation perform by ocaml-gettext. Default: \
enable." );
( "--gettext-domain-dir",
(let current_textdomain = ref textdomain in
Arg.Tuple
[
Arg.String (fun textdomain -> current_textdomain := textdomain);
Arg.String
(fun dir ->
set_global_t
(bindtextdomain !current_textdomain dir (get_global_t ())));
]),
spf
(f_
"textdomain dir Set a dir to search ocaml-gettext files for the \
specified domain. Default: %s.")
(string_of_list
(MapTextdomain.fold
(fun textdomain (_, dir_opt) lst ->
match dir_opt with
| Some dir -> spf "%s: %s" textdomain dir :: lst
| None -> lst)
(get_global_t ()).textdomains [])) );
( "--gettext-dir",
Arg.String
(fun s ->
set_global_t
{ (get_global_t ()) with path = s :: (get_global_t ()).path }),
spf
(f_ "dir Add a search dir for ocaml-gettext files. Default: %s.")
(string_of_list (get_global_t ()).path) );
( "--gettext-language",
Arg.String
(fun s -> set_global_t { (get_global_t ()) with language = Some s }),
spf
(f_
"language Set the default language for ocaml-gettext. Default: \
%s.")
(match (get_global_t ()).language with
| Some s -> s
| None -> "(none)") );
( "--gettext-codeset",
Arg.String
(fun s -> set_global_t { (get_global_t ()) with codeset = s }),
spf
(f_
"codeset Set the default codeset for outputting string with \
ocaml-gettext. Default: %s.")
(get_global_t ()).codeset );
],
GettextConfig.copyright )
let s_ str = dgettext (get_global_t' ()) textdomain str
let f_ str = fdgettext (get_global_t' ()) textdomain str
let sn_ str = dngettext (get_global_t' ()) textdomain str
let fn_ str = fdngettext (get_global_t' ()) textdomain str
end