diff options
Diffstat (limited to 'perl_checker.src/parser_helper.ml')
-rw-r--r-- | perl_checker.src/parser_helper.ml | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/perl_checker.src/parser_helper.ml b/perl_checker.src/parser_helper.ml index 7f723a6..e562d15 100644 --- a/perl_checker.src/parser_helper.ml +++ b/perl_checker.src/parser_helper.ml @@ -3,6 +3,9 @@ open Common open Printf let bpos = -1, -1 +let pot_strings = ref [] +let pot_strings_and_file = Hashtbl.create 16 +let po_comments = ref [] let raw_pos2pos(a, b) = !Info.current_file, a, b let pos_range (_, (_, (a1, b1))) (_, (_, (a2, b2))) = raw_pos2pos((if a1 = -1 then a2 else a1), (if b2 = -1 then b1 else b2)) @@ -25,6 +28,7 @@ let is_parenthesized = function let un_parenthesize = function | List[List[e]] -> e + | List[e] -> e | _ -> internal_error "un_parenthesize" let rec un_parenthesize_full = function @@ -484,7 +488,15 @@ let call_func is_a_func (e, para) = | "N" | "N_" -> (match para with - | [ List(String([ _s, List [] ], _) :: _) ] -> None + | [ List(String([ s, List [] ], (file, _, _)) :: _) ] -> + if !Flags.generate_pot then ( + lpush pot_strings (s, !po_comments) ; + po_comments := [] ; + Hashtbl.add pot_strings_and_file s file ; + ) ; + (*if String.contains s '\t' then warn_rule "tabulation in translated string must be written \\\\t";*) + (*if count_matching_char s '\n' > 10 then warn_rule "long string";*) + None | [ List(String _ :: _) ] -> die_rule "don't use interpolated translated string, use %s or %d instead" | _ -> die_rule (sprintf "%s() must be used with a string" f)) @@ -595,3 +607,30 @@ let from_PATTERN_SUBST parse ((s1, s2, opts), (_, pos)) = [ String(parse_interpolated parse s1, raw_pos2pos pos) ; String(parse_interpolated parse s2, raw_pos2pos pos) ; Raw_string(opts, raw_pos2pos pos) ] + +let po_comment (s, _) = lpush po_comments s + +let generate_pot file = + let fd = open_out file in + let rec print_formatted_char = function + | '"' -> output_char fd '\\'; output_char fd '"' + | '\t' -> output_char fd '\\'; output_char fd 't' + | '\\' -> output_char fd '\\'; output_char fd '\\' + | '\n' -> output_string fd "\\n\"\n\"" + | c -> output_char fd c + in + List.iter (fun (s, po_comments) -> + match Hashtbl.find_all pot_strings_and_file s with + | [] -> () + | l -> + List.iter (fun po_comment -> output_string fd ("#. " ^ po_comment ^ "\n")) po_comments; + + List.iter (fun _ -> Hashtbl.remove pot_strings_and_file s) l ; + fprintf fd "#: %s\n" (String.concat " " l) ; + + output_string fd (if String.contains s '\n' then "msgid \"\"\n\"" else "msgid \"") ; + String.iter print_formatted_char s ; + output_string fd "\"\n" ; + output_string fd "msgstr \"\"\n\n" + ) !pot_strings ; + close_out fd |