forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port code for building i18n resources to Clojure build scripts (metab…
…ase#15189) * Port code for building i18n resources to Clojure build scripts; remove dep on gettext * Use Metabase classloader * Test fixes 🔧 * Bump adoptopenjdk version * Delete gettext dependency from Dockerfile * Update developers-guide.md * Address PR feedback * Fix frontend singular msgstr format Co-authored-by: Luis Paolini <[email protected]>
- Loading branch information
1 parent
80f984c
commit aa211a5
Showing
27 changed files
with
408 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,9 @@ | ||
#!/bin/sh | ||
#! /usr/bin/env bash | ||
|
||
set -eu | ||
set -euo pipefail | ||
|
||
# gettext installed via homebrew is "keg-only", add it to the PATH | ||
if [ -d "/usr/local/opt/gettext/bin" ]; then | ||
export PATH="/usr/local/opt/gettext/bin:$PATH" | ||
fi | ||
source "./bin/check-clojure-cli.sh" | ||
check_clojure_cli | ||
|
||
POT_NAME="locales/metabase.pot" | ||
LOCALES=$(find locales -type f -name "*.po" -exec basename {} .po \;) | ||
|
||
if [ -z "$LOCALES" ]; then | ||
LOCALES_QUOTED="" | ||
else | ||
LOCALES_QUOTED=" $(echo "$LOCALES" | awk '{ printf "\"%s\" ", $0 }')" | ||
fi | ||
|
||
FRONTEND_LANG_DIR="resources/frontend_client/app/locales" | ||
|
||
# backend | ||
# NOTE: include "en" even though we don't have a .po file for it because it's the default? | ||
cat << EOF > "resources/locales.clj" | ||
{ | ||
:locales #{"en"$LOCALES_QUOTED} | ||
:packages ["metabase"] | ||
:bundle "metabase.Messages" | ||
} | ||
EOF | ||
|
||
mkdir -p "$FRONTEND_LANG_DIR" | ||
|
||
for LOCALE in $LOCALES; do | ||
LOCALE_FILE="locales/$LOCALE.po" | ||
LOCALE_WITH_UNDERSCORE=$(echo "$LOCALE" | tr '-' '_') | ||
# frontend | ||
# NOTE: just copy these for now, but eventially precompile from .po to .json | ||
./bin/i18n/build-translation-frontend-resource \ | ||
"$LOCALE_FILE" \ | ||
"$FRONTEND_LANG_DIR/$LOCALE_WITH_UNDERSCORE.json" | ||
|
||
# backend | ||
msgfmt \ | ||
--java2 \ | ||
-d "resources" \ | ||
-r "metabase.Messages" \ | ||
-l "$LOCALE_WITH_UNDERSCORE" \ | ||
"$LOCALE_FILE" | ||
done | ||
cd bin/i18n | ||
clojure -M -m i18n.create-artifacts $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{:paths ["src"] | ||
|
||
:deps | ||
{common/common {:local/root "../common"} | ||
cheshire/cheshire {:mvn/version "5.8.1"} | ||
clj-http/clj-http {:mvn/version "3.9.1"} | ||
org.fedorahosted.tennera/jgettext {:mvn/version "0.15.1"}} | ||
|
||
:aliases | ||
{:test {:extra-paths ["test"] | ||
:extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git" | ||
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}} | ||
:main-opts ["-m" "cognitect.test-runner"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(ns i18n.common | ||
(:require [clojure.java.io :as io] | ||
[clojure.string :as str] | ||
[metabuild-common.core :as u]) | ||
(:import [org.fedorahosted.tennera.jgettext Catalog HeaderFields Message PoParser])) | ||
|
||
(defn locales | ||
"Set of all locales for which we have i18n bundles. | ||
(locales) ; -> #{\"nl\" \"pt\" \"zh\" \"tr\" \"it\" \"fa\" ...}" | ||
[] | ||
(set (for [^java.io.File file (.listFiles (io/file (u/filename u/project-root-directory "locales"))) | ||
:let [file-name (.getName file)] | ||
:when (str/ends-with? file-name ".po")] | ||
(str/replace file-name #"\.po$" "")))) | ||
|
||
(defn locale-source-po-filename [locale] | ||
(u/filename u/project-root-directory "locales" (format "%s.po" locale))) | ||
|
||
;; see https://github.com/zanata/jgettext/tree/master/src/main/java/org/fedorahosted/tennera/jgettext | ||
|
||
(defn- catalog ^Catalog [locale] | ||
(let [parser (PoParser.)] | ||
(.parseCatalog parser (io/file (locale-source-po-filename "es"))))) | ||
|
||
(defn po-headers [locale] | ||
(when-let [^Message message (.locateHeader (catalog locale))] | ||
(let [header-fields (HeaderFields/wrap (.getMsgstr message))] | ||
(into {} (for [^String k (.getKeys header-fields)] | ||
[k (.getValue header-fields k)]))))) | ||
|
||
(defn po-messages-seq [locale] | ||
(for [^Message message (iterator-seq (.iterator (catalog locale))) | ||
;; remove any empty translations | ||
:when (not (str/blank? (.getMsgid message)))] | ||
{:id (.getMsgid message) | ||
:id-plural (.getMsgidPlural message) | ||
:str (.getMsgstr message) | ||
:str-plural (seq (remove str/blank? (.getMsgstrPlural message))) | ||
:fuzzy? (.isFuzzy message) | ||
:plural? (.isPlural message) | ||
:source-references (seq (remove str/blank? (.getSourceReferences message))) | ||
:comment (.getMsgctxt message)})) | ||
|
||
(defn po-contents [locale] | ||
{:headers (po-headers locale) | ||
:messages (po-messages-seq locale)}) | ||
|
||
(defn print-message-count-xform [rf] | ||
(let [num-messages (volatile! 0)] | ||
(fn | ||
([] | ||
(rf)) | ||
([result] | ||
(u/announce "Wrote %d messages." @num-messages) | ||
(rf result)) | ||
([result message] | ||
(vswap! num-messages inc) | ||
(rf result message))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns i18n.create-artifacts | ||
(:require [clojure.pprint :as pprint] | ||
[i18n.common :as i18n] | ||
[i18n.create-artifacts.backend :as backend] | ||
[i18n.create-artifacts.frontend :as frontend] | ||
[metabuild-common.core :as u])) | ||
|
||
;; TODO -- shouldn't this be `locales.edn`? | ||
(defn- locales-dot-clj [] | ||
{:locales (conj (i18n/locales) "en") | ||
:packages ["metabase"] | ||
:bundle "metabase.Messages"}) | ||
|
||
(defn- generate-locales-dot-clj! [] | ||
(u/step "Create resources/locales.clj" | ||
(let [file (u/filename u/project-root-directory "resources" "locales.clj")] | ||
(u/delete-file-if-exists! file) | ||
(spit file (with-out-str (pprint/pprint (locales-dot-clj)))) | ||
(u/assert-file-exists file)))) | ||
|
||
(defn- create-artifacts-for-locale! [locale] | ||
(u/step (format "Create artifacts for locale %s" (pr-str locale)) | ||
(frontend/create-artifact-for-locale! locale) | ||
(backend/create-artifact-for-locale! locale) | ||
(u/announce "Artifacts for locale %s created successfully." (pr-str locale)))) | ||
|
||
(defn- create-artifacts-for-all-locales! [] | ||
(doseq [locale (i18n/locales)] | ||
(create-artifacts-for-locale! locale))) | ||
|
||
(defn create-all-artifacts! [] | ||
(u/step "Create i18n artifacts" | ||
(generate-locales-dot-clj!) | ||
(create-artifacts-for-all-locales!) | ||
(u/announce "Translation resources built successfully."))) | ||
|
||
(defn -main [] | ||
(create-all-artifacts!)) |
Oops, something went wrong.