Skip to content

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

3.9 KB
Raw Blame
(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)))

(set! *warn-on-reflection* true)

(defn locales
  "Set of all locales with a catalog in `directory`.

    (locales directory) ; -> #{\"nl\" \"pt\" \"zh\" \"tr\" \"it\" \"fa\" ...}"
  [directory]
  (into
   (sorted-set)
   (for [^java.io.File file (.listFiles (io/file directory))
         :let               [file-name (.getName file)]
         :when              (and (str/ends-with? file-name ".po")
                                 (not (str/starts-with? file-name "metabase")))]
     (str/replace file-name #"\.po$" ""))))

(defn locale-source-po-filename
  "Path to the catalog for `locale` in `directory`."
  [locale directory]
  (str (io/file directory (format "%s.po" locale))))

;; see https://github.com/zanata/jgettext/tree/master/src/main/java/org/fedorahosted/tennera/jgettext

(defn- catalog ^Catalog [locale directory]
  (let [parser (PoParser.)]
    (.parseCatalog parser (io/file (locale-source-po-filename locale directory)))))

(defn- po-headers [locale directory]
  (when-let [^Message message (.locateHeader (catalog locale directory))]
    (let [header-fields (HeaderFields/wrap (.getMsgstr message))]
      (into {} (for [^String k (.getKeys header-fields)]
                 [k (if (= k "Language")
                      locale
                      (.getValue header-fields k))])))))

(defn- po-messages-seq [locale directory]
  (for [^Message message (iterator-seq (.iterator (catalog locale directory)))
        ;; remove any empty translations
        :when            (not (str/blank? (.getMsgid message)))]
    {:id                (.getMsgid message)
     :id-plural         (.getMsgidPlural message)
     :str               (.getMsgstr message)
     :str-plural        (seq (.getMsgstrPlural message))
     :fuzzy?            (.isFuzzy message)
     :plural?           (.isPlural message)
     :source-references (seq (remove str/blank? (.getSourceReferences message)))
     ;; the `msgctxt` a message was extracted with (nil when it declares none). Part of a
     ;; message's identity: the same `msgid` can appear under several contexts.
     :context           (.getMsgctxt message)}))

(defn po-contents
  "Contents of the PO file for a `locale`."
  [locale directory]
  {:headers  (po-headers locale directory)
   :messages (po-messages-seq locale directory)})

(defn backend-message?
  "True if `message` (the shape returned by `po-messages-seq`) originates from a Clojure source file
  — i.e. one of its `:source-references` ends in `.clj` or `.cljc`. This is the same filter used by
  `i18n.create-artifacts.backend` when writing the backend `.edn` artifacts, and by `i18n.validation`
  when deciding which translations to sanity-check as `java.text.MessageFormat` patterns. Frontend
  strings use a different format system (`#, javascript-format` in the `.po`) and their own
  validation rules, so this scanner skips them."
  [{:keys [source-references]}]
  (boolean
   (let [paths (eduction
                ;; Sometimes 2 paths exist in a single string, space separated
                (mapcat #(str/split % #" "))
                ;; Strip off the line number at the end of some paths
                (map #(str/split % #":"))
                (map first)
                source-references)]
     (some (fn [path]
             (some
              (fn [suffix]
                (str/ends-with? path suffix))
              [".clj" ".cljc"]))
           paths))))

(defn print-message-count-xform
  "Transducer that prints a count of how many translation strings we process/write."
  [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)))))