This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-22
Channels
- # adventofcode (26)
- # aleph (34)
- # announcements (10)
- # babashka (71)
- # beginners (80)
- # biff (7)
- # calva (1)
- # cider (4)
- # cljdoc (12)
- # clojure (8)
- # clojure-belgium (1)
- # clojure-europe (11)
- # clojure-nl (3)
- # clojure-norway (18)
- # clojure-sg (3)
- # clojure-sweden (2)
- # clojurescript (18)
- # clojutre (4)
- # conjure (1)
- # core-logic (4)
- # datahike (1)
- # datascript (3)
- # emacs (27)
- # exercism (1)
- # gratitude (12)
- # introduce-yourself (4)
- # joyride (1)
- # lsp (46)
- # malli (3)
- # membrane (2)
- # nbb (1)
- # off-topic (3)
- # other-languages (7)
- # pedestal (4)
- # portal (3)
- # practicalli (1)
- # rdf (33)
- # re-frame (11)
- # releases (1)
- # ring (1)
- # scittle (34)
- # shadow-cljs (10)
- # squint (12)
- # tools-deps (89)
- # tree-sitter (2)
- # xtdb (14)
We are using re-frame and tempura for internationalization. Since re-frame 1.3.0 we are getting a warning "core.cljs:3953 re-frame: Subscribe was called outside of a reactive context." We are indeed subscribing to the language choice in the function that creates a partial of tempura/tr. Is there an approach that would allow us to do this without having to subscribe to / pass around the language choice in/to all components?
Of course, you'd use that sub only in views. And you can wrap it in a function so that it's easier to use, something like:
(defn <tr []
@(rf/subscribe [:tr]))
So there is a difference between returning the fn from a subscription and having a plain fn use the subscription to build the tr?
Only if you have code like
(def tr @(rf/subscribe [:tr]))
The only difference is where you use subscriptions. It's either in a reactive context (views, reactions - the intended usage) or not (the usage that produces that warning).OK, but then I don't see how I can avoid subscribing in every component or at the top lvl and passing tr to sub-components? Or are you saying build a component that calls defn ? Sorry if I am being dense.
Right now I am doing a require [dict :refer tr] in all view files and just use that whenever I render text. Ideally I would like to preserve that somehow.
> I don't see how I can avoid subscribing in every component Why would you want to avoid it? Just do it. :) Actually, this is roughly what I'm doing in one of my projects (not exactly what I proposed above, but similar):
(ns my-app.i18n
(:require [re-frame.core :as rf]))
(rf/reg-sub :tr
(fn [db [_ key]]
... call some actual `tr` function on the `key` ...)
(defn tr [key]
@(rf/subscribe [:tr key]))
(ns my-app.views
(:require [my-app.i18n :refer [tr]]))
(defn button
[:button {:on-click #()}
(tr [:the-button-label])])
Ah, I think I got it. Use the subscription to perform the actual translation. And have the sub look up the key and the current language.