taoensso / tempura

Simple text localization library for Clojure/Script
https://www.taoensso.com/tempura
Eclipse Public License 1.0
260 stars 16 forks source link

load-resource throws: "Runtime resource loading not possible for cljs dictionaries. See `tempura/load-resource-at-compile-time` as an alternative." #25

Closed abenems closed 2 years ago

abenems commented 5 years ago

Hi Peter, I'm trying to do the following: I added this dependency to my project: [com.taoensso/tempura "1.2.1"]

I added the following to my cljs file: [taoensso.tempura :as tempura :refer [tr]]))

(def my-tempura-dictionary {:en-GB ; Locale {:missing ":en-GB missing text" ; Fallback for missing resources :example ; You can nest ids if you like {:greet "Good day %1!" ; Note Clojure fn-style %1 args }}

:en ; A second locale {:missing ":en missing text" :example {:greet "Hello %1" :farewell "Goodbye %1" :foo "foo" :bar "bar" :bar-copy :en.example/bar ; Can alias entries :baz [:div "This is a Hiccup form"]

 ;; Can use arbitrary fns as resources
 :qux (fn [[arg1 arg2]] (str arg1 " and " arg2))}

:example-copy :en/example ; Can alias entire subtrees

:import-example
{:__load-resource ; Inline edn content loaded from disk/resource
 "resources/i18n.clj"}}})

(def optsT {:dict my-tempura-dictionary}) (def tr1 (partial tr optsT [:en]))

In the REPL I tried this: (require '[ 'mynamespace' :as seg :refer [myfn]]) (seg/tr1 [:example/foo])

I get the following error:

error {:message "Runtime resource loading not possible for cljs dictionaries. See tempura/load-resource-at-compile-time as an alternative.", :data {:rname "resources/i18n/en.edn"}}

If I remove the import-example option it works. It returns 'foo'.

I've also tried this with and without resources in the path, same error: (def local-dict {:de {:load-resource "i18n/de.edn"} :en {:load-resource "i18n/en.edn"}})

(def optsT {:dict local-dict}) (def tr1 (partial tr optsT [:en]))

Lastly, I also tried running the load-resources function in the REPL, same error: (require '[taoensso.tempura.impl :as impl :refer [load-resource]]) (impl/load-resource "i18n/en.edn") or (impl/load-resource "resources/i18n/en.edn")

please help

ptaoussanis commented 5 years ago

Hi Alina,

Only had an opportunity to skim this- but it looks like you're using ClojureScript and trying to use :__load-resource in your dictionary?

As the error you're seeing mentions- "#error {:message "Runtime resource loading not possible for cljs dictionaries. See tempura/load-resource-at-compile-time as an alternative.", :data {:rname "resources/i18n/en.edn"}}"

the use of :__load-resource isn't supported in ClojureScript. You'll need to check the tempura/load-resource-at-compile-time macro to load a dictionary in ClojureScript.

You can find some more info by looking at the docstrings of load-resource-at-runtime:

"Experimental, subject to change.
     Reads and returns an edn resource on classpath, at runtime.
     Supported by: clj only (cljs support not possible).

     A {:my-key {:__load-resource \"my-file.edn\"}} dictionary entry is
     equivalent to {:my-key (load-resource-at-runtime \"my-file.edn\")}.

     See also `load-resource-at-compile-time`."

and load-resource-at-compile-time:

"Experimental, subject to change.
  Reads and inlines an edn resource on classpath, at compile-time.
  Supported by: both clj and cljs.

  See also `load-resource-at-runtime`."

Hope I understood correctly and that that helps some, good luck!