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

Merge pull request #11916 from zmstone/1109-dump-json-file-for-i18n

1109 dump json file for i18n
Zaiming (Stone) Shi 2 лет назад
Родитель
Сommit
5aeb1feada
3 измененных файлов с 62 добавлено и 5 удалено
  1. 18 4
      build
  2. 41 0
      scripts/merge-i18n.escript
  3. 3 1
      scripts/pre-compile.sh

+ 18 - 4
build

@@ -12,6 +12,12 @@ if [ "${DEBUG:-0}" -eq 1 ]; then
     export DIAGNOSTIC=1
 fi
 
+log_red() {
+  local RED='\033[0;31m' # Red
+  local NC='\033[0m' # No Color
+  echo -e "${RED}${1}${NC}"
+}
+
 PROFILE_ARG="$1"
 ARTIFACT="$2"
 
@@ -34,7 +40,7 @@ case "$(is_enterprise "$PROFILE_ARG"),$(is_enterprise "$PROFILE_ENV")" in
         true
         ;;
     *)
-        echo "PROFILE env var is set to '$PROFILE_ENV', but '$0' arg1 is '$PROFILE_ARG'"
+        log_red "PROFILE env var is set to '$PROFILE_ENV', but '$0' arg1 is '$PROFILE_ARG'"
         exit 1
         ;;
 esac
@@ -133,6 +139,14 @@ make_docs() {
     erl -noshell -eval \
         "ok = emqx_conf:dump_schema('$docdir', $SCHEMA_MODULE), \
          halt(0)."
+    local desc="$docdir/desc.en.hocon"
+    if command -v jq &> /dev/null; then
+        log "Generating $desc"
+        scripts/merge-i18n.escript | jq --sort-keys . > "$desc"
+    else
+        # it is not a big deal if we cannot generate the desc
+        log_red "NOT Generated: $desc"
+    fi
 }
 
 ## arg1 is the profile for which the following args (as app names) should be excluded
@@ -149,8 +163,8 @@ assert_no_excluded_deps() {
     for app in "${excluded_apps[@]}"; do
         found="$($FIND "$rel_dir" -maxdepth 1 -type d -name "$app-*")"
         if [ -n "${found}" ]; then
-            echo "ERROR: ${app} should not be included in ${PROFILE}"
-            echo "ERROR: found ${app} in ${rel_dir}"
+            log_red "ERROR: ${app} should not be included in ${PROFILE}"
+            log_red "ERROR: found ${app} in ${rel_dir}"
             exit 1
         fi
     done
@@ -291,7 +305,7 @@ make_tgz() {
     mkdir -p "${tard}/emqx"
     mkdir -p "${pkgpath}"
     if [ ! -f "$src_tarball" ]; then
-        log "ERROR: $src_tarball is not found"
+        log_red "ERROR: $src_tarball is not found"
     fi
     $TAR zxf "${src_tarball}" -C "${tard}/emqx"
     if [ -f "${tard}/emqx/releases/${PKG_VSN}/relup" ]; then

+ 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).

+ 3 - 1
scripts/pre-compile.sh

@@ -25,10 +25,12 @@ cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
 # generate merged config files and English translation of the desc (desc.en.hocon)
 ./scripts/merge-config.escript
 
+I18N_REPO_BRANCH="v$(./pkg-vsn.sh "${PROFILE_STR}" | tr -d '.' | cut -c 1-2)"
+
 # download desc (i18n) translations
 curl -L --silent --show-error \
      --output "apps/emqx_dashboard/priv/desc.zh.hocon" \
-    'https://raw.githubusercontent.com/emqx/emqx-i18n/main/desc.zh.hocon'
+     "https://raw.githubusercontent.com/emqx/emqx-i18n/${I18N_REPO_BRANCH}/desc.zh.hocon"
 
 # TODO
 # make sbom a build artifcat