<commit_before>open GettextTypes

module StringMap = Map.Make (String)

let build_string_map mo =
  let map, _ =
    GettextMo.fold_mo Ignore
      (fun translation accu ->
        match translation with
        | Singular (str_id, str) ->
           StringMap.add str_id (str, None) accu
        | Plural (str_id, _, _) ->
           Printf.ksprintf failwith "unsupported: Plural(%S, _, _)" str_id
      ) StringMap.empty mo
  in
  map

let () =
  build_string_map Sys.argv.(1)
  |> StringMap.bindings
  |> List.map
       (fun (str_id, (str, plural)) ->
         match plural with
         | None -> str_id, `List [`String str]
         | Some lst -> str_id, `List [`String str; `List (List.map (fun x -> `String x) lst)])
  |> (fun x -> `Assoc x)
  |> Yojson.Safe.to_channel stdout<commit_msg>mo2json: check compatibility of translated format strings<commit_after>open GettextTypes

module StringMap = Map.Make (String)

let check_format_compatibility str1 str2 =
  let open CamlinternalFormatBasics in
  let open CamlinternalFormat in
  let Fmt_EBB fmt1 = fmt_ebb_of_string str1 in
  ignore (format_of_string_format str2 (Format (fmt1, str1)))

let build_string_map mo =
  let map, _ =
    GettextMo.fold_mo Ignore
      (fun translation accu ->
        match translation with
        | Singular (str_id, str) ->
           if String.contains str_id '%' then
             check_format_compatibility str_id str;
           StringMap.add str_id (str, None) accu
        | Plural (str_id, _, _) ->
           Printf.ksprintf failwith "unsupported: Plural(%S, _, _)" str_id
      ) StringMap.empty mo
  in
  map

let () =
  build_string_map Sys.argv.(1)
  |> StringMap.bindings
  |> List.map
       (fun (str_id, (str, plural)) ->
         match plural with
         | None -> str_id, `List [`String str]
         | Some lst -> str_id, `List [`String str; `List (List.map (fun x -> `String x) lst)])
  |> (fun x -> `Assoc x)
  |> Yojson.Safe.to_channel stdout
{'fraction_non_alphanumeric': 0.11179173047473201, 'fraction_numerical': 0.005104645227156712, 'mean_word_length': 2.596330275229358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '477278', 'n_tokens_mistral': 671, 'n_tokens_neox': 658, 'n_words': 184}