taoensso / tower

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

Parent scopes are no longer searched for missing translations in 2.0.0 #36

Closed ystael closed 10 years ago

ystael commented 10 years ago

I just upgraded my project to tower 2.0.0 from 1.2.0, and it seems that parent scopes are no longer searched for missing translations. If I have the dictionary:

{:en
 {:missing "<Translation missing: %s>"
  :error
  {:not-found "The requested resource %s was not found."}}}

and I call

(with-tscope :error
  (t :en my-tconfig :conflict conflicting-id))

then I get a NullPointerException because the key :error/missing is not found. I have to add another :missing translation in the :error subdictionary for this to work.

Is this behavior intentional? The comment at https://github.com/ptaoussanis/tower/blob/master/src/taoensso/tower.clj#L438 seems to indicate that parent scopes should be searched for missing translation messages.

Here are traces of the behavior with and without the extra missing translation key:

user> (tower/with-tscope :error
       (tower/t :en
                {:fallback-locale :en
                 :dictionary {:en
                              {:missing "<Translation missing: %s>"
                               :error
                               {:not-found "The requested resource %s was not found."}}}}
                :conflict
                1))
TRACE t8492: (taoensso.tower/t :en {:fallback-locale :en, :dictionary {:en {:error {:not-found "The requested resource %s was not found."}, :missing "<Translation missing: %s>"}}} :conflict 1)
TRACE t8493: | (taoensso.tower/translate :en {:fallback-locale :en, :dictionary {:en {:error {:not-found "The requested resource %s was not found."}, :missing "<Translation missing: %s>"}}} :taoensso.tower/scope-var :conflict 1)
TRACE t8494: | | (taoensso.tower/fmt-str :en nil 1)
NullPointerException   java.util.regex.Matcher.getTextLength (Matcher.java:1234)
user> (tower/with-tscope :error
       (tower/t :en
                {:fallback-locale :en
                 :dictionary {:en
                              {:missing "<Translation missing: %s>"
                               :error
                               {:not-found "The requested resource %s was not found."
                                :missing "<Translation missing: %s>"}}}}
                :conflict
                1))
TRACE t8499: (taoensso.tower/t :en {:fallback-locale :en, :dictionary {:en {:error {:not-found "The requested resource %s was not found.", :missing "<Translation missing: %s>"}, :missing "<Translation missing: %s>"}}} :conflict 1)
TRACE t8500: | (taoensso.tower/translate :en {:fallback-locale :en, :dictionary {:en {:error {:not-found "The requested resource %s was not found.", :missing "<Translation missing: %s>"}, :missing "<Translation missing: %s>"}}} :taoensso.tower/scope-var :conflict 1)
TRACE t8501: | | (taoensso.tower/fmt-str :en "&lt;Translation missing: %s&gt;" ":en" ":error" "[:conflict]")
TRACE t8501: | | => "&lt;Translation missing: :en&gt;"
TRACE t8502: | | (taoensso.tower/fmt-str :en "&lt;Translation missing: :en&gt;" 1)
TRACE t8502: | | => "&lt;Translation missing: :en&gt;"
TRACE t8500: | => "&lt;Translation missing: :en&gt;"
TRACE t8499: => "&lt;Translation missing: :en&gt;"
"&lt;Translation missing: :en&gt;"
ptaoussanis commented 10 years ago

Hi Chris, thanks for the detailed report - that was helpful.

As you suspected, this is a bug. Have just pushed v2.0.1 which should correct the problem. In case you're curious, the issue was that the get-tr fn uses the dynamic scope - not something we want for the :missing key fallback. The unit tests were failing to catch this because it only surfaces when combined with a with-tscope as in your case.

ystael commented 10 years ago

This is great -- thank you very much for the quick fix!

On Mon, Nov 25, 2013 at 12:43 PM, Peter Taoussanis <notifications@github.com

wrote:

Hi Chris, thanks for the detailed report - that was helpful.

As you suspected, this is a bug. Have just pushed v2.0.1 which should correct the problem. In case you're curious, the issue was that the get-trfn uses the dynamic scope - not something we want for the :missing key fallback. The unit tests were missing this because it only surfaces when combined with a with-tscope as in your case.

— Reply to this email directly or view it on GitHubhttps://github.com/ptaoussanis/tower/issues/36#issuecomment-29223287 .

ptaoussanis commented 10 years ago

Np, cheers :-)