Просмотр исходного кода

build: generate desc.en.hocon file for i18n

Zaiming (Stone) Shi 2 лет назад
Родитель
Сommit
5990f9835f
2 измененных файлов с 44 добавлено и 0 удалено
  1. 3 0
      build
  2. 41 0
      scripts/merge-i18n.escript

+ 3 - 0
build

@@ -133,6 +133,9 @@ make_docs() {
     erl -noshell -eval \
         "ok = emqx_conf:dump_schema('$docdir', $SCHEMA_MODULE), \
          halt(0)."
+    local desc="$docdir/desc.en.hocon"
+    log "Generating $desc"
+    scripts/merge-i18n.escript | jq --sort-keys . > "$desc"
 }
 
 ## arg1 is the profile for which the following args (as app names) should be excluded

+ 41 - 0
scripts/merge-i18n.escript

@@ -0,0 +1,41 @@
+#!/usr/bin/env escript
+
+%% This script is only used at build time to generate the merged desc.en.hocon in JSON format
+%% but NOT the file generated to _build/$PROFILE/lib/emqx_dashboard/priv (which is HOCON format).
+%%
+%% The generated JSON file is used as the source of truth when translating to other languages.
+
+-mode(compile).
+
+-define(RED, "\e[31m").
+-define(RESET, "\e[39m").
+
+main(_) ->
+    try
+        _ = hocon:module_info()
+    catch
+        _:_ ->
+            fail("hocon module not found, please make sure the project is compiled")
+    end,
+    %% wildcard all .hocon files in rel/i18n
+    Files = filelib:wildcard("rel/i18n/*.hocon"),
+    case Files of
+        [_ | _] ->
+            ok;
+        [] ->
+            fail("No .hocon files found in rel/i18n")
+    end,
+    case hocon:files(Files) of
+        {ok, Map} ->
+            JSON = jiffy:encode(Map),
+            io:format("~s~n", [JSON]);
+        {error, Reason} ->
+            fail("~p~n", [Reason])
+    end.
+
+fail(Str) ->
+    fail(Str, []).
+
+fail(Str, Args) ->
+    io:format(standard_error, ?RED ++ "ERROR: " ++ Str ++ ?RESET ++ "~n", Args),
+    halt(1).