Skip to content

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

fix: handle more header names in translation csv (#62241)

Edward Pagetc9777da390f6
diff --git a/enterprise/backend/src/metabase_enterprise/content_translation/dictionary.clj b/enterprise/backend/src/metabase_enterprise/content_translation/dictionary.clj
index 5202fe1b032..6c5f8dea578 100644
--- a/enterprise/backend/src/metabase_enterprise/content_translation/dictionary.clj
+++ b/enterprise/backend/src/metabase_enterprise/content_translation/dictionary.clj
@@ -3,6 +3,7 @@
(:require
[clojure.data.csv :as csv]
[clojure.java.io :as io]
+ [clojure.set :as set]
[clojure.string :as str]
[metabase.premium-features.core :as premium-features]
[metabase.util :as u]
@@ -51,7 +52,12 @@
(defn- format-row
"Formats a row to be inserted into the content translation table. Locales are standardized, and all fields are trimmed. Extra fields are included as well."
[headers row]
- (let [{locale :language msgid :string msgstr :translation} (zipmap headers row)
+ (let [row-with-headers (zipmap headers row)
+ locale (or (get row-with-headers "language")
+ (get row-with-headers "locale")
+ (get row-with-headers "locale code"))
+ msgid (get row-with-headers "string")
+ msgstr (get row-with-headers "translation")
normalized-locale (i18n/normalized-locale-string (str/trim locale))
formatted-locale (if (nil? normalized-locale)
(str/trim locale)
@@ -72,20 +78,33 @@
(adjust-index index)))
(defn- header?
- "Checks if a row is likely a header by seeing if it has the set of values we expect in the header"
+ "Checks if a row is likely a header by seeing if has a subset of the values we expect the header
+ to have.
+
+ We can only ~kinda~ guess when a row is header, but since it unlikely for two or more of these
+ values to ever appear in a translation row we can say this is a header. "
[row]
- (and (vector? row)
- (= #{"language" "string" "translation"} (->> row
- (map u/lower-case-en)
- set))))
+ (let [normalized-values (->> row
+ (map u/lower-case-en)
+ set)]
+ (>= (count (set/intersection #{"language" "locale code" "locale" "string" "translation"} normalized-values))
+ 2)))
-(def ^:private default-headers [:language :string :translation])
+(def ^:private default-headers ["language" "string" "translation"])
(defn- parse-header
[header-row]
- (->> (map u/lower-case-en header-row)
- (map keyword)
- vec))
+ (let [parsed (mapv u/lower-case-en header-row)
+ errors (cond-> []
+ (not= 3 (count parsed)) (conj "Header must have only three columns")
+ (empty? (filter #(contains? #{"language" "locale" "locale code"} %) parsed)) (conj "Header must have one of: Language, Locale, or Locale Code")
+ (nil? (some #{"string"} parsed)) (conj "Header must have String")
+ (nil? (some #{"translation"} parsed)) (conj "Header must have translation"))]
+ (when (seq errors)
+ (throw (ex-info "This file could not be uploaded due to the following error(s):"
+ {:status-code http-status-unprocessable
+ :errors errors})))
+ parsed))
(defn- process-rows
"Format, validate, and process rows from a CSV. Takes a collection of vectors and returns a map with the shape
diff --git a/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj b/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
index 254b0fe1ff7..347634ccc81 100644
--- a/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
@@ -34,7 +34,7 @@
translations (if (empty? translations)
sample-translations
translations)
- csv-data (cons ["Language" "String" "Translation"]
+ csv-data (cons ["Locale Code" "String" "Translation"]
(map (fn [{:keys [locale msgid msgstr]}]
[locale msgid msgstr])
translations))]
diff --git a/enterprise/backend/test/metabase_enterprise/content_translation/dictionary_test.clj b/enterprise/backend/test/metabase_enterprise/content_translation/dictionary_test.clj
index b69c6e509ef..a3cec5b27df 100644
--- a/enterprise/backend/test/metabase_enterprise/content_translation/dictionary_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/content_translation/dictionary_test.clj
@@ -114,6 +114,11 @@
(is (thrown-with-msg?
Exception #"Row 1.*CSV error.*unexpected character.*X"
(dictionary/read-and-import-csv! file)))))
+ (testing "Reads CSV with invalid header name and throws informative error"
+ (let [file (.getBytes "Loca,String,Translation\nes-mx,hello,hola")] ; bad header
+ (is (thrown-with-msg?
+ Exception #"This file could not be uploaded due to the following error\(s\):"
+ (dictionary/read-and-import-csv! file)))))
(testing "Reads CSV with invalid data row and throws informative error"
(let [file (.getBytes "Language,String,Translation\nde,Title,Titel\nde,Vendor,\"Anbieter\"X")] ; character outside quotation marks
(is (thrown-with-msg?
@@ -152,24 +157,103 @@
(is (some #(and (= (:locale %) "es") (= (:msgid %) "Hello") (= (:msgstr %) "Hola")) translations))
(is (some #(and (= (:locale %) "fr") (= (:msgid %) "Goodbye") (= (:msgstr %) "Au revoir")) translations))))))))
-(deftest ^:parallel format-row-test
+(deftest read-and-import-csv-different-locale-headers-test
+ (ct-utils/with-clean-translations!
+ (testing "CSV with 'locale' header is imported correctly"
+ (mt/with-premium-features #{:content-translation}
+ (let [csv-with-locale "locale,string,translation\nes,Hello,Hola\nfr,Goodbye,Au revoir"
+ file (.getBytes csv-with-locale)]
+ (dictionary/read-and-import-csv! file)
+ (is (= 2 (count-translations)))
+ (let [translations (get-translations)]
+ (is (some #(and (= (:locale %) "es") (= (:msgid %) "Hello") (= (:msgstr %) "Hola")) translations))
+ (is (some #(and (= (:locale %) "fr") (= (:msgid %) "Goodbye") (= (:msgstr %) "Au revoir")) translations))))))
+
+ (testing "CSV with 'locale code' header is imported correctly"
+ (mt/with-premium-features #{:content-translation}
+ (let [csv-with-locale-code "locale code,string,translation\nde,Thank you,Danke\nit,Good morning,Buongiorno"
+ file (.getBytes csv-with-locale-code)]
+ (dictionary/read-and-import-csv! file)
+ (is (= 2 (count-translations)))
+ (let [translations (get-translations)]
+ (is (some #(and (= (:locale %) "de") (= (:msgid %) "Thank you") (= (:msgstr %) "Danke")) translations))
+ (is (some #(and (= (:locale %) "it") (= (:msgid %) "Good morning") (= (:msgstr %) "Buongiorno")) translations))))))
+
+ (testing "CSV with 'language' header is imported correctly"
+ (mt/with-premium-features #{:content-translation}
+ (let [csv-with-language "language,string,translation\npt_BR,Welcome,Bem-vindo\nzh_CN,Hello,你好"
+ file (.getBytes csv-with-language)]
+ (dictionary/read-and-import-csv! file)
+ (is (= 2 (count-translations)))
+ (let [translations (get-translations)]
+ (is (some #(and (= (:locale %) "pt_BR") (= (:msgid %) "Welcome") (= (:msgstr %) "Bem-vindo")) translations))
+ (is (some #(and (= (:locale %) "zh_CN") (= (:msgid %) "Hello") (= (:msgstr %) "你好")) translations))))))))
+
+(deftest ^:parallel format-row-locale-standardization-test
(testing "Format function standardizes locale"
(is (=
["pt_BR" "msgid" "msgstr"]
- (#'dictionary/format-row [:language :string :translation] ["pt-br" "msgid" "msgstr"])))
+ (#'dictionary/format-row ["language" "string" "translation"] ["pt-br" "msgid" "msgstr"])))
(is (=
["pt_BR" "msgid" "msgstr"]
- (#'dictionary/format-row [:language :string :translation] ["Pt-bR" "msgid" "msgstr"])))
+ (#'dictionary/format-row ["language" "string" "translation"] ["Pt-bR" "msgid" "msgstr"])))
(is (=
["pt_BR" "msgid" "msgstr"]
- (#'dictionary/format-row [:language :string :translation] ["pt_br" "msgid" "msgstr"])))
+ (#'dictionary/format-row ["language" "string" "translation"] ["pt_br" "msgid" "msgstr"])))
(is (=
["zh_CN" "msgid" "msgstr"]
- (#'dictionary/format-row [:language :string :translation] ["ZH-cn" "msgid" "msgstr"]))))
+ (#'dictionary/format-row ["language" "string" "translation"] ["ZH-cn" "msgid" "msgstr"])))))
+
+(deftest ^:parallel format-row-trimming-test
(testing "Format function trims all fields"
(is (=
["pt_BR" "msgid" "msgstr"]
- (#'dictionary/format-row [:language :string :translation] [" pt-BR " "msgid " " msgstr"])))))
+ (#'dictionary/format-row ["language" "string" "translation"] [" pt-BR " "msgid " " msgstr"])))))
+
+(deftest ^:parallel format-row-header-names-test
+ (testing "Format function works with different locale header names"
+ (is (=
+ ["pt_BR" "msgid" "msgstr"]
+ (#'dictionary/format-row ["locale" "string" "translation"] ["pt-br" "msgid" "msgstr"])))
+ (is (=
+ ["pt_BR" "msgid" "msgstr"]
+ (#'dictionary/format-row ["locale code" "string" "translation"] ["pt-br" "msgid" "msgstr"])))
+ (is (=
+ ["pt_BR" "msgid" "msgstr"]
+ (#'dictionary/format-row ["language" "string" "translation"] ["pt-br" "msgid" "msgstr"])))))
+
+(deftest ^:parallel format-row-field-order-test
+ (testing "Format function extracts fields in correct order regardless of input order"
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["language" "string" "translation"] ["es" "Hello" "Hola"])))
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["string" "language" "translation"] ["Hello" "es" "Hola"])))
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["translation" "string" "language"] ["Hola" "Hello" "es"])))
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["string" "translation" "language"] ["Hello" "Hola" "es"])))
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["language" "translation" "string"] ["es" "Hola" "Hello"])))
+ (is (=
+ ["es" "Hello" "Hola"]
+ (#'dictionary/format-row ["translation" "language" "string"] ["Hola" "es" "Hello"])))))
+
+(deftest ^:parallel format-row-mixed-headers-order-test
+ (testing "Format function works with different locale header names in different orders"
+ (is (=
+ ["fr" "Goodbye" "Au revoir"]
+ (#'dictionary/format-row ["locale" "string" "translation"] ["fr" "Goodbye" "Au revoir"])))
+ (is (=
+ ["fr" "Goodbye" "Au revoir"]
+ (#'dictionary/format-row ["string" "locale" "translation"] ["Goodbye" "fr" "Au revoir"])))
+ (is (=
+ ["fr" "Goodbye" "Au revoir"]
+ (#'dictionary/format-row ["locale code" "translation" "string"] ["fr" "Au revoir" "Goodbye"])))))
(deftest import-translations-success-test
(ct-utils/with-clean-translations!