Skip to content

Commit 394168b

Browse files
authored
Merge pull request #17 from sagotch/type-t
Optionnaly generate type representation of the language + default default lang.
2 parents dc29126 + 6100d71 commit 394168b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

i18n_generate.mll

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,25 @@ and parse_string_2 buffer = parse
8181

8282
{
8383

84+
let print_type fmt langs =
85+
Format.fprintf fmt
86+
"[%%%%shared type t = %a]\n"
87+
(Format.pp_print_list
88+
~pp_sep:(fun fmt () -> Format.pp_print_string fmt "|")
89+
Format.pp_print_string ) langs
90+
8491
let print_header fmt default_lang =
8592
Format.pp_print_string fmt @@
86-
"[%%server\n\
93+
"[%%shared let default_language = " ^ default_lang ^ "]\n\
94+
[%%server\n\
8795
let _language_ =\n\
8896
Eliom_reference.Volatile.eref\n\
89-
~scope:Eliom_common.default_process_scope " ^ default_lang ^ "\n\
97+
~scope:Eliom_common.default_process_scope default_language\n\
9098
let get_lang () = Eliom_reference.Volatile.get _language_\n\
9199
let set_lang lang = Eliom_reference.Volatile.set _language_ lang\n\
92100
]\n\
93101
[%%client\n\
94-
let _language_ = ref " ^ default_lang ^ "\n\
102+
let _language_ = ref default_language\n\
95103
let get_lang () = !_language_\n\
96104
let set_lang lang = _language_ := lang\n\
97105
]\n\
@@ -167,19 +175,24 @@ let input_file = ref "-"
167175
let output_file = ref "-"
168176
let langs = ref ""
169177
let default_lang = ref ""
178+
let external_type = ref false
170179

171180
let options = Arg.align
172181
[ ( "--langs", Arg.Set_string langs
173182
, " Comma-separated langs (from ocaml sum type) (e.g. Us,Fr). \
174183
Must be ordered as in source TSV file.")
175184
; ( "--default-lang", Arg.Set_string default_lang
176-
, " Set the default lang.")
185+
, " Set the default lang (default is the first one in --langs).")
177186
; ( "--input-file", Arg.Set_string input_file
178187
, " TSV file containing keys and translations. \
179188
If option is omited or set to -, read on stdin.")
180189
; ( "--ouput-file", Arg.Set_string output_file
181190
, " File TSV file containing keys and translations. \
182-
If option is omited or set to -, write on stdout.") ]
191+
If option is omited or set to -, write on stdout.")
192+
; ( "--external-type", Arg.Set external_type
193+
, " Values passed to --langs option come from a predefined type \
194+
(do not generate the type).")
195+
]
183196

184197
let usage = "usage: ocsigen-i18n-generator [options] [< input] [> output]"
185198

@@ -195,11 +208,12 @@ let _ =
195208
| "-" -> stdout
196209
| file -> open_out file in
197210
let langs = Str.split (Str.regexp ",") !langs in
198-
let default_lang = !default_lang in
211+
let default_lang = match !default_lang with "" -> List.hd langs | x -> x in
199212
assert (List.mem default_lang langs) ;
200213
let lexbuf = Lexing.from_channel in_chan in
201214
(try let key_values = parse_lines langs [] lexbuf in
202215
let output = Format.formatter_of_out_channel out_chan in
216+
if not (!external_type) then print_type output langs ;
203217
print_header output default_lang ;
204218
Format.fprintf output "module Tr = struct\n" ;
205219
print_module_body print_expr_html output key_values ;

0 commit comments

Comments
 (0)