|
@@ -1,9 +1,11 @@
|
|
|
(ns logseq.tasks.lang
|
|
|
"Tasks related to language translations"
|
|
|
(:require [clojure.set :as set]
|
|
|
+ [clojure.string :as string]
|
|
|
[frontend.dicts :as dicts]
|
|
|
[frontend.modules.shortcut.dicts :as shortcut-dicts]
|
|
|
- [logseq.tasks.util :as task-util]))
|
|
|
+ [logseq.tasks.util :as task-util]
|
|
|
+ [babashka.process :refer [shell]]))
|
|
|
|
|
|
(defn- get-dicts
|
|
|
[]
|
|
@@ -69,8 +71,11 @@
|
|
|
(sort-by (juxt :file :translation-key))
|
|
|
task-util/print-table))))
|
|
|
|
|
|
-(defn invalid-translations
|
|
|
- "Lists translation that don't exist in English"
|
|
|
+(defn- validate-non-default-languages
|
|
|
+ "This validation finds any translation keys that don't exist in the default
|
|
|
+ language English. Logseq needs to work out of the box with its default
|
|
|
+ language. This catches mistakes where another language has accidentally typoed
|
|
|
+ keys or added ones without updating :en"
|
|
|
[]
|
|
|
(let [dicts (get-all-dicts)
|
|
|
;; For now defined as :en but clj-kondo analysis could be more thorough
|
|
@@ -83,12 +88,68 @@
|
|
|
(set/difference (set (keys get-dicts))
|
|
|
valid-keys)))))]
|
|
|
(if (empty? invalid-dicts)
|
|
|
- (println "All translations have valid keys!")
|
|
|
+ (println "All non-default translations have valid keys!")
|
|
|
(do
|
|
|
- (println "Invalid translation keys found:")
|
|
|
+ (println "\nThese translation keys are invalid because they don't exist in English:")
|
|
|
(task-util/print-table invalid-dicts)
|
|
|
(System/exit 1)))))
|
|
|
|
|
|
+;; Command to check for manual entries:
|
|
|
+;; grep -E -oh '\(t [^ ):]+' -r src/main
|
|
|
+(def manual-ui-dicts
|
|
|
+ "Manual list of ui translations because they are dynamic i.e. keyword isn't
|
|
|
+ first arg. Only map values are used in linter as keys are for easily scanning
|
|
|
+ grep result."
|
|
|
+
|
|
|
+ {"(t (shortcut-helper/decorate-namespace" [] ;; shortcuts related so can ignore
|
|
|
+ "(t (keyword" [:color/yellow :color/red :color/pink :color/green :color/blue
|
|
|
+ :color/purple :color/gray]
|
|
|
+ ;; from 3 files
|
|
|
+ "(t (if" [:asset/show-in-folder :asset/open-in-browser
|
|
|
+ :search-item/whiteboard :search-item/page
|
|
|
+ :page/make-private :page/make-public]
|
|
|
+ "(t (name" [] ;; shortcuts related
|
|
|
+ "(t (dh/decorate-namespace" [] ;; shortcuts related
|
|
|
+ "(t prompt-key" [:select/default-prompt :select.graph/prompt]
|
|
|
+ ;; All args to ui/make-confirm-modal are not keywords
|
|
|
+ "(t title" []
|
|
|
+ "(t subtitle" [:asset/physical-delete]})
|
|
|
+
|
|
|
+(defn- validate-ui-translations-are-used
|
|
|
+ "This validation checks to see that translations done by (t ...) are equal to
|
|
|
+ the ones defined for the default :en lang. This catches translations that have
|
|
|
+ been added in UI but don't have an entry or translations no longer used in the UI"
|
|
|
+ []
|
|
|
+ (let [actual-dicts (->> (shell {:out :string}
|
|
|
+ ;; This currently assumes all ui translations
|
|
|
+ ;; use (t and src/main. This can easily be
|
|
|
+ ;; tweaked as needed
|
|
|
+ "grep -E -oh '\\(t :[^ )]+' -r src/main")
|
|
|
+ :out
|
|
|
+ string/split-lines
|
|
|
+ (map #(keyword (subs % 4)))
|
|
|
+ (concat (mapcat val manual-ui-dicts))
|
|
|
+ set)
|
|
|
+ expected-dicts (set (keys (:en (get-dicts))))
|
|
|
+ actual-only (set/difference actual-dicts expected-dicts)
|
|
|
+ expected-only (set/difference expected-dicts actual-dicts)]
|
|
|
+ (if (and (empty? actual-only) (empty? expected-only))
|
|
|
+ (println "All defined :en translation keys match the ones that are used!")
|
|
|
+ (do
|
|
|
+ (when (seq actual-only)
|
|
|
+ (println "\nThese translation keys are invalid because they are used in the UI but not defined:")
|
|
|
+ (task-util/print-table (map #(hash-map :invalid-key %) actual-only)))
|
|
|
+ (when (seq expected-only)
|
|
|
+ (println "\nThese translation keys are invalid because they are not used in the UI:")
|
|
|
+ (task-util/print-table (map #(hash-map :invalid-key %) expected-only)))
|
|
|
+ (System/exit 1)))))
|
|
|
+
|
|
|
+(defn validate-translations
|
|
|
+ "Runs multiple translation validations that fail fast if one of them is invalid"
|
|
|
+ []
|
|
|
+ (validate-non-default-languages)
|
|
|
+ (validate-ui-translations-are-used))
|
|
|
+
|
|
|
(defn list-duplicates
|
|
|
"Lists translations that are the same as the one in English"
|
|
|
[& args]
|