Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Validate quarter-and-day message values (#85227)

Radek Pietruszewski67e44f25f0c6
diff --git a/bin/build/src/i18n/validation.clj b/bin/build/src/i18n/validation.clj
index 4677d0526bb..f53cda16d61 100644
--- a/bin/build/src/i18n/validation.clj
+++ b/bin/build/src/i18n/validation.clj
@@ -42,8 +42,11 @@
checks; frontend strings get only the format-system-agnostic ones. All applicable types are
collected — no short-circuiting. Single `MessageFormat` parse for backend strings."
[msgstr message backend?]
- (let [count-fn (arg-count-fn backend?)
- format-error (when backend? (validation/message-format-error msgstr))
+ (let [msgid (:id message)
+ count-fn (arg-count-fn backend?)
+ format-error (if backend?
+ (validation/message-format-error msgstr)
+ (validation/regex-message-format-error msgid msgstr))
skip? (if backend?
(when-not format-error (validation/skipped-arg-index? msgstr))
(validation/regex-skipped-arg-index? msgstr))
diff --git a/docs/developers-guide/internationalization.md b/docs/developers-guide/internationalization.md
index c46c9678cef..54407c663bc 100644
--- a/docs/developers-guide/internationalization.md
+++ b/docs/developers-guide/internationalization.md
@@ -55,7 +55,7 @@ The functions `trs` and `tru` create instances of two records, `SiteLocalizedStr
### The maps from source to translated string
-One step in our build process creates an edn file of source to translated string for each locale we support. These are located in `resources/i18n`. If you do not have these files, you can run `bin/build-translation-resources` to generate them.
+One step in our build process creates an edn file of source to translated string for each locale we support. These are located in `resources/i18n`. If you do not have these files, you can run `bin/i18n/build-translation-resources` to generate them.
We have lots of contributors who help us keep a corpus of translated strings into many different languages. We use [Crowdin](https://crowdin.com/project/metabase-i18n) to keep an authoritative list. We export `.po` files from this, which is essentially a dictionary from source to translated string. As part of our build process we format these files as edn files, maps from the source to translated string, for each locale.
diff --git a/src/metabase/util/i18n/validation.clj b/src/metabase/util/i18n/validation.clj
index acb2d44a9ad..59b36f7d732 100644
--- a/src/metabase/util/i18n/validation.clj
+++ b/src/metabase/util/i18n/validation.clj
@@ -9,6 +9,8 @@
- `metabase.util.i18n.validation-test` — regression guard on the built `.edn` artifacts.
Kept free of jgettext and other build-only deps so all three layers can require it."
+ (:require
+ [clojure.string :as str])
(:import
(java.text MessageFormat)))
@@ -93,6 +95,27 @@
(and (seq indices)
(not= (count indices) (inc (apply max indices))))))
+(defn- dayjs-quarter-pattern-error
+ "If `s` is not a valid dayjs format string containing a quarter number, returns an error message"
+ [s needs-yyyy?]
+ (if-let [[_ before _ after] (re-matches #"(.*)\[(.+)\](.*)" s)]
+ (let [pattern (str before after)]
+ (not-empty (str/join ", " (cond-> []
+ (not (str/includes? pattern "Q"))
+ (conj "expected `Q`")
+
+ (and needs-yyyy? (not (str/includes? pattern "YYYY")))
+ (conj "expected `YYYY`")))))
+ "expected `[xx]`"))
+
+(defn regex-message-format-error
+ "If `s` is not a valid pattern for `msgid`, returns an error message, else `nil`.
+ This is used for special cases, like dayjs format strings."
+ [msgid s]
+ (cond
+ (= msgid "[Q]Q YYYY") (dayjs-quarter-pattern-error s true)
+ (= msgid "[Q]Q") (dayjs-quarter-pattern-error s false)))
+
;;; -------------------------------------------------- Cross-string checks -------------------------------------------------
(defn- normalize-acceptable-counts
diff --git a/test/metabase/util/i18n/validation_test.clj b/test/metabase/util/i18n/validation_test.clj
index 1c1973ef89f..982b964a552 100644
--- a/test/metabase/util/i18n/validation_test.clj
+++ b/test/metabase/util/i18n/validation_test.clj
@@ -96,6 +96,26 @@
"" nil
nil nil))
+(deftest ^:parallel regex-message-format-error-test
+ (are [msgid s expected] (= expected (validation/regex-message-format-error msgid s))
+ "[Q]Q YYYY" "[Q]Q YYYY" nil
+ "[Q]Q YYYY" "[T]Q YYYY" nil
+ "[Q]Q YYYY" "Q[Kw.] YYYY" nil
+ "[Q]Q YYYY" "Q YYYY" "expected `[xx]`"
+ "[Q]Q YYYY" "[T]T YYYY" "expected `Q`"
+ "[Q]Q YYYY" "[C]Q yyyy" "expected `YYYY`"
+ "[Q]Q YYYY" "[T]T" "expected `Q`, expected `YYYY`"
+
+ "[Q]Q" "[Q]Q" nil
+ "[Q]Q" "[K]Q" nil
+ "[Q]Q" "Q[T]" nil
+ "[Q]Q" "QQ" "expected `[xx]`"
+ "[Q]Q" "T[T]" "expected `Q`"
+
+ "something-else" nil nil
+ "something-else" "" nil
+ "something-else" "foo" nil))
+
(deftest ^:parallel arg-count-mismatch-test
(testing "string input (macro-time path — parses format string)"
(is (nil? (validation/arg-count-mismatch "{0} {1}" 2))