Source file gettextTranslate.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
(** Signature of module for translation storage / access.
@author Sylvain Le Gall
*)
open GettextTypes
open GettextUtils
open GettextMo
open GettextFormat
module type TRANSLATE_TYPE = sig
type u
val create : t -> filename -> (string -> string) -> u
(** create t filename recode : Create a translation
table using filename as the mo file and recode as the encoding
converter.
*)
val translate : u -> bool -> string -> (string * int) option -> string
(** translate str (plural_form,number) tbl : translate the string
str using tbl. It is possible that the operation modify tbl,
so it is returned also. It is also possible to get the plural
form of the translated string using plural_form and number.
*)
end
module Dummy : TRANSLATE_TYPE = struct
type u = string -> string
let create _t _filename charset = charset
let translate charset printf_format str plural_form =
match plural_form with
| None -> charset str
| Some (str_plural, x) ->
let check =
if printf_format then check_format Ignore else fun x -> x
in
charset
(get_translated_value Ignore
(check (Plural (str, str_plural, [])))
(germanic_plural x))
end
module Map : TRANSLATE_TYPE = struct
type u = {
dummy : Dummy.u;
map : translation MapString.t;
failsafe : failsafe;
fun_plural_forms : int -> int;
}
let create t filename charset =
let map, fun_plural_forms =
fold_mo t.GettextTypes.failsafe
(fun translation accu ->
match translation with
| Singular (str_id, str) ->
MapString.add str_id (Singular (str_id, charset str)) accu
| Plural (str_id, str_plural, lst) ->
MapString.add str_id
(Plural (str_id, str_plural, List.map charset lst))
accu)
MapString.empty filename
in
{
dummy = Dummy.create t filename charset;
map;
failsafe = t.GettextTypes.failsafe;
fun_plural_forms;
}
let translate u printf_format str plural_form =
try
let plural_number =
u.fun_plural_forms
(match plural_form with Some (_, x) -> x | None -> 0)
in
let check =
if printf_format then check_format u.failsafe else fun x -> x
in
get_translated_value u.failsafe
(check (MapString.find str u.map))
plural_number
with Not_found ->
fail_or_continue u.failsafe (TranslateStringNotFound str)
(Dummy.translate u.dummy printf_format str plural_form)
end
module Hashtbl : TRANSLATE_TYPE = struct
type u = {
dummy : Dummy.u;
hashtbl : (string, translation) Hashtbl.t;
failsafe : failsafe;
fun_plural_forms : int -> int;
}
let create t filename charset =
let hashtbl, fun_plural_forms =
fold_mo t.GettextTypes.failsafe
(fun translation accu ->
match translation with
| Singular (str_id, str) ->
Hashtbl.add accu str_id (Singular (str_id, charset str));
accu
| Plural (str_id, str_plural, lst) ->
Hashtbl.add accu str_id
(Plural (str_id, str_plural, List.map charset lst));
accu)
(Hashtbl.create 32)
filename
in
{
dummy = Dummy.create t filename charset;
hashtbl;
failsafe = t.GettextTypes.failsafe;
fun_plural_forms;
}
let translate u printf_format str plural_form =
try
let plural_number =
u.fun_plural_forms
(match plural_form with Some (_, x) -> x | None -> 0)
in
let check =
if printf_format then check_format u.failsafe else fun x -> x
in
get_translated_value u.failsafe
(check (Hashtbl.find u.hashtbl str))
plural_number
with Not_found ->
fail_or_continue u.failsafe (TranslateStringNotFound str)
(Dummy.translate u.dummy printf_format str plural_form)
end
module Open : TRANSLATE_TYPE = struct
type u = {
dummy : Dummy.u;
filename : filename;
charset : string -> string;
failsafe : failsafe;
fun_plural_forms : int -> int;
number_of_strings : int;
}
let create t filename charset =
let chn = open_in_bin filename in
let = input_mo_header chn in
let informations =
input_mo_informations t.GettextTypes.failsafe chn header
in
close_in chn;
{
dummy = Dummy.create t filename charset;
filename;
charset;
failsafe = t.GettextTypes.failsafe;
fun_plural_forms = informations.GettextTypes.fun_plural_forms;
number_of_strings = Int32.to_int header.GettextTypes.number_of_strings;
}
let translate u printf_format str plural_form =
let chn = open_in_bin u.filename in
let res =
try
let plural_number =
u.fun_plural_forms
(match plural_form with Some (_, x) -> x | None -> 0)
in
let = input_mo_header chn in
let rec find_str_id (start_index, end_index) =
let middle_index = (start_index + end_index) / 2 in
let str_id =
let lst_str_id =
input_mo_untranslated u.failsafe chn header middle_index
in
match lst_str_id with
| str_id :: _ -> str_id
| [] ->
raise Not_found
in
match String.compare str str_id with
| x when x < 0 && start_index <= middle_index - 1 ->
find_str_id (start_index, middle_index - 1)
| x when x > 0 && middle_index + 1 <= end_index ->
find_str_id (middle_index + 1, end_index)
| x when x = 0 -> middle_index
| _ -> raise Not_found
in
let translation =
let translation =
input_mo_translation u.failsafe chn header
(find_str_id (0, u.number_of_strings - 1))
in
match translation with
| Singular (str_id, str) -> Singular (str_id, u.charset str)
| Plural (str_id, str_plural, lst) ->
Plural (str_id, str_plural, List.map u.charset lst)
in
get_translated_value u.failsafe translation plural_number
with Not_found ->
fail_or_continue u.failsafe (TranslateStringNotFound str)
(Dummy.translate u.dummy printf_format str plural_form)
in
close_in chn;
res
end