Created
March 24, 2014 13:47
-
-
Save elfenlaid/9740364 to your computer and use it in GitHub Desktop.
first useful script on clojure :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns translate.core | |
(:use clojure-csv.core)) | |
(defonce store (parse-csv (slurp "resources/trans.csv"))) | |
(def lang-shortcuts { "Russian" "ru" | |
"French" "fr" | |
"Italian" "it" | |
"German" "de" | |
"Spanish" "es" | |
"Japanese" "ja" | |
"Portugese" "pt" | |
"Swedish" "sv" | |
"Turkish" "tr" | |
"Chineese Simplified" "zh-Hans" | |
"Chineese Traditional" "zh-Hant" | |
"Arabic" "ar" | |
"Korean" "ko" | |
"Dutch" "nl" }) | |
(defn format-value [o i f] | |
(let [or-title (f o) | |
t-title (f i)] | |
(when (not-empty or-title) | |
(str \" or-title \" " = " \" (if (not-empty t-title) t-title or-title) \"\;)))) | |
(defn extract-translation [orig-index out-index st] | |
(reduce (fn [v f] (if-let [s (format-value orig-index out-index f)] | |
(conj v s) | |
v)) | |
[] | |
st)) | |
(defn mkdir [path] | |
(let [path (str "output/" path)] | |
(.mkdir (java.io.File. path)) | |
path)) | |
(defn spit-translation [lang ts] | |
(if-let [lang-short (lang-shortcuts (clojure.string/trim lang))] | |
(let [dir (mkdir (str lang-short ".lproj")) | |
file "Localizable.strings" | |
path (str dir "/" file) | |
content (clojure.string/join "\n" ts)] | |
(spit path content)) | |
(print "Failed to write translations for: " lang))) | |
(defn localize-store [st] | |
(let [ks (map-indexed vector (first st)) | |
st (rest st)] | |
(doseq [[i k] ks] | |
(when-let [trs (extract-translation 0 i st)] | |
(spit-translation k trs))))) | |
(localize-store store) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment