taoensso / tower

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

Named interpolations? #15

Closed ifesdjeen closed 11 years ago

ifesdjeen commented 11 years ago

Hi,

What do you think about named interpolations?

For example:

Given that example.greeting == "Hello %{name}, how are you?"

(t :example/greeting {:name "Steve"})
;; => "Hello Steve, how are you?"
ptaoussanis commented 11 years ago

Hi Alex,

Tower uses MessageFormat interpolation by default since it's got great support for localized conversions, pluralizations, etc.

It doesn't have named interpolations, but it does have indexed interpolations: `"Hello {0}, how are you?", etc.

If you really wanted a different interpolation scheme, it'd be very easy to add a wrapper to the unary version of t: the interpolation stuff is all just one line:

([k-or-ks & interpolation-args]
  (when-let [pattern (t k-or-ks)]
    (apply format-msg pattern interpolation-args)) ; You'd want your wrapper to change this
  )

Hope that helps!

Cheers :-)

ifesdjeen commented 11 years ago

Sure sure! I've read through it, and understand that it's a very good solution.

Moreover, we did solve it for our case with around 8 lines of code, so I realize that it's trivial for anyone to extend Tower in a way to get named interpolations.

Actually, we solved it exactly the way you have mentioned here :)

I was mostly wondering if that could be any useful for others. We're using translations in rails-style due to historical reasons, and it would take us a long time to completely get rid of them.

I'll close it)

ptaoussanis commented 11 years ago

Okay, great.

I was mostly wondering if that could be any useful for others.

No problem! And we've got it on record now in case anyone else comes along with the same question, which is nice.

Cheers! :-)