merge-i18n.escript 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env escript
  2. %% This script is only used at build time to generate the merged desc.en.hocon in JSON format
  3. %% but NOT the file generated to _build/$PROFILE/lib/emqx_dashboard/priv (which is HOCON format).
  4. %%
  5. %% The generated JSON file is used as the source of truth when translating to other languages.
  6. -mode(compile).
  7. -define(RED, "\e[31m").
  8. -define(RESET, "\e[39m").
  9. main(_) ->
  10. try
  11. _ = hocon:module_info()
  12. catch
  13. _:_ ->
  14. fail("hocon module not found, please make sure the project is compiled")
  15. end,
  16. %% wildcard all .hocon files in rel/i18n
  17. Files = filelib:wildcard("rel/i18n/*.hocon"),
  18. case Files of
  19. [_ | _] ->
  20. ok;
  21. [] ->
  22. fail("No .hocon files found in rel/i18n")
  23. end,
  24. case hocon:files(Files) of
  25. {ok, Map} ->
  26. JSON = jiffy:encode(Map),
  27. io:format("~s~n", [JSON]);
  28. {error, Reason} ->
  29. fail("~p~n", [Reason])
  30. end.
  31. fail(Str) ->
  32. fail(Str, []).
  33. fail(Str, Args) ->
  34. io:format(standard_error, ?RED ++ "ERROR: " ++ Str ++ ?RESET ++ "~n", Args),
  35. halt(1).