The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns i18n.pseudo-locale
"Generates the `en-ZZ` pseudo-locale `.po` file used by E2E tests.
This file is the build-time companion to the pseudo-locale strategy described in UXW-3460. Every English source
string from the pinned `metabase.po` is transformed into `[zz] {English}`, and the resulting `target/i18n/en-ZZ.po`
is picked up by the normal i18n build pipeline (same as every other real locale).
E2E tests can then switch the UI to `en-ZZ` and assert on the predictable `[zz] ...` prefix, without depending on
fragile real-language strings that Crowdin may update at any time.
The file is never committed — it is regenerated on every build from the pinned `metabase.po`, so it never
drifts. Crowdin does not know about `en-ZZ` (it's not in `locales/crowdin.yml`) so the translation-update workflow
leaves it alone.
See `src/metabase/util/i18n/impl.clj:available-locale?` for the backend allowlist that makes `en_ZZ` a valid
settable locale, and `src/metabase/util/i18n.clj:available-locales-with-names` for the dev/test-only visibility
filter that hides it from production language pickers."
(:require
[clojure.java.io :as io]
[metabuild-common.core :as u])
(:import
(org.fedorahosted.tennera.jgettext Catalog Message PoParser PoWriter)))
(set! *warn-on-reflection* true)
(def pseudo-locale-code
"The on-disk filename stem for the pseudo-locale. Hyphen form to match the existing convention
(`pt-BR.po`, `zh-CN.po`, ...)."
"en-ZZ")
(def ^:private msgstr-prefix "[zz] ")
(def ^:private pseudo-header-msgstr
(str
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: en-ZZ\n"
"Language-Team: E2E Testing Pseudo-Locale\n"
"X-Metabase-Pseudo-Locale: true\n"))
(defn- prefix [s]
(str msgstr-prefix s))
(defn- build-pseudo-header ^Message []
(let [header (Message.)]
(.setMsgid header "")
(.setMsgstr header pseudo-header-msgstr)
(.addComment header " Pseudo-locale auto-generated by bin/build/src/i18n/pseudo_locale.clj. DO NOT EDIT.")
(.addComment header " See UXW-3460 and the namespace docstring for details.")
header))
(defn- build-pseudo-message
"Given a `Message` from `metabase.po`, return a new `Message` whose `msgstr` (and plural `msgstr`s) are the original
`msgid` prefixed with `[zz] `. All other fields (msgctxt, source references, comments, formats) are copied as-is."
^Message [^Message src]
(let [dst (Message.)]
(when-let [domain (.getDomain src)]
(.setDomain dst domain))
(when-let [msgctxt (.getMsgctxt src)]
(.setMsgctxt dst msgctxt))
(.setMsgid dst (.getMsgid src))
(if (.isPlural src)
(let [msgid-plural (.getMsgidPlural src)
plurals (.getMsgstrPlural src)]
(.setMsgidPlural dst msgid-plural)
;; Index 0 corresponds to the singular form, indices >= 1 to the plural form(s).
;; We don't model language-specific plural rules beyond Metabase's default `nplurals=2`.
(dotimes [i (max 2 (count plurals))]
(.addMsgstrPlural dst
(if (zero? i)
(prefix (.getMsgid src))
(prefix msgid-plural))
i)))
(.setMsgstr dst (prefix (.getMsgid src))))
(doseq [ref (.getSourceReferences src)]
(.addSourceReference dst ref))
(doseq [fmt (.getFormats src)]
(.addFormat dst fmt))
(doseq [comment (.getExtractedComments src)]
(.addExtractedComment dst comment))
dst))
(defn- source-catalog ^Catalog [source-directory]
(let [parser (PoParser.)
source-file (io/file source-directory "metabase.po")]
(.parseCatalog parser source-file)))
(defn generate-pseudo-locale-po!
"Read `metabase.po` from `source-directory` and write `en-ZZ.po` to `target-directory` with every msgstr prefixed
with `[zz] `. Idempotent: running this repeatedly produces the same output (modulo jgettext's internal ordering)."
[source-directory target-directory]
(u/step (format "Generate pseudo-locale %s.po" pseudo-locale-code)
(let [src (source-catalog source-directory)
dst (Catalog.)
target (io/file target-directory (str pseudo-locale-code ".po"))
msg-count (volatile! 0)]
(.addMessage dst (build-pseudo-header))
(doseq [^Message msg (iterator-seq (.iterator src))
:when (not (.isHeader msg))]
(.addMessage dst (build-pseudo-message msg))
(vswap! msg-count inc))
(u/create-directory-unless-exists! (str target-directory))
(u/delete-file-if-exists! (.getAbsolutePath target))
(.write (PoWriter.) dst target)
(u/announce "Wrote %d pseudo-locale messages to %s." @msg-count (.getAbsolutePath target)))))