taoensso / tower

i18n & L10n library for Clojure/Script
https://www.taoensso.com/tower
Eclipse Public License 1.0
278 stars 24 forks source link

How to use with clojurescript #49

Closed roboli closed 9 years ago

roboli commented 10 years ago

Hello Peter, I was wondering if you could please point to some examples/resources to better understand how to use tower in cljs. The part I don't get in the README.md example is:

;; Inlined (macro) dict => this ns needs rebuild for dict changes to reflect:
   :compiled-dictionary (tower-macros/dict-compile "my-dict.clj")

And i'm using it like this:

(ns tower-example.core
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [taoensso.tower :as tower :refer-macros (with-tscope)]
            [tower-example.brepl]))

(enable-console-print!)

(def ^:private tconfig
  {:fallback-locale :en
   ;; Inlined (macro) dict => this ns needs rebuild for dict changes to reflect:
   :compiled-dictionary (tower/dict-compile "i18n/dictionary.clj")})

(def t (tower/make-t tconfig)) ; Create translation fn

(def app-state (atom {:text "Hello world!"
                      :text2 (t :es tconfig :default/greeting)}))

(om/root
  (fn [app owner]
    (dom/h1 nil (:text app))
    (dom/h3 nil (:text2 app)))
  app-state
  {:target (. js/document (getElementById "app"))})

When I load the page in the browser, I get in the console:

TypeError: taoensso.tower.dict_compile is undefined

I'm sorry if I'm ignoring too much, but I've been googling for it but I don't find any answers.

Thank you for your time.

ptaoussanis commented 10 years ago

Hi Roberto, sorry for the delay replying - have been travelling.

Did you find the info you were looking for?

roboli commented 10 years ago

Hello Peter. No I didn't. But I found kawasima/tower-cljs, but I don't know what limitations I will find. For the moment I just need to translate the front end and if later I need translations on the server (restful api) I guess I could make a crossover on the dictionary. I think that can work... But it will be one more dependency. Could I have your opinion?

Thank you for your response.

ptaoussanis commented 10 years ago

Not at all familiar with kawasima/tower-cljs (first I've heard of it), so can't comment there.

The error you're seeing above looks like it's being caused by an error in the README example. Have updated here: https://github.com/ptaoussanis/tower/tree/dev#clojurescript-translation-support-still-experimental

Please try that and let me know how it goes?

I guess I could make a crossover on the dictionary

Not necessary. You can use the same .clj dictionary between the server/client if you like, or split it into client/server files - whatever you find more convenient. Tower's ClojureScript support works by compiling the Tower dictionary into your JavaScript at compile time.

Does that help / make sense?

roboli commented 10 years ago

Yeah make sense. I just was trying to say that with the other library (tower-cljs) I have to do the crossover because it doesn't compiles anything.

Anyway, I updated my code just like the example but, I just can't make it work... Now cljsbuild complains:

WARNING: No such namespace: tower-macros at line 13 src/cljs/tower_example/core.cljs

And here is the code again:

(ns tower-example.core
  (:require-macros [taoensso.tower :as tower-macros :refer (with-tscope)])
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [tower-example.brepl]
            [taoensso.tower :as tower]))

(enable-console-print!)

(def ^:private tconfig
  {:fallback-locale :en
   ;; Inlined (macro) dict => this ns needs rebuild for dict changes to reflect:
   :compiled-dictionary (tower-macros/dict-compile "i18n/dictionary.clj")})

(def t (tower/make-t tconfig)) ; Create translation fn

...

And the dependencies are (if it helps):

(defproject tower-example "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [org.clojure/clojurescript "0.0-2173"]
                 [compojure "1.1.6"]
                 [com.taoensso/tower "2.1.0-RC1"]
                 [om "0.5.0"]
                 [com.cemerick/piggieback "0.1.3"]]
...

Again, thanks.

ptaoussanis commented 10 years ago

Hi Roberto,

Sorry for taking so long to reply - was on vacation! Are you still having trouble with this? I'd suggest checking that all your dependencies (incl. Tower) are up-to-date, then calling lein clean; lein cljsbuild clean and trying again. That often helps if the Cljs build artefacts have somehow gotten into an inconsistent state.

Cheers! :-)

roboli commented 9 years ago

Hi Peter! No worries, I understand... Well, actually I left things working as I told you, using tower on the server and tower-cljs on the client. But I'm not comfortable with that approach but I'm on another thing right now, but once I get done with it I will try again and if I'm in trouble I'll get back to you. Many thanks.

ptaoussanis commented 9 years ago

Okay, great. Closing for now - please feel free to reopen later if you try again and are still running into issues!

Cheers :-)