0

I'm working with the Tower library for i18n and l10n in my web application.

I have the problem of having to localize "just-in-time", i.e with an unanticipated dictionary, and in particular unanticipated supported locales.

I receive HTTP request, from which I can extract the accepted locales in preference order, e.g :

[:de :en-UK :en :fr-FR :fr]

On the other hand, I have to localize from micro-dictionaries which are fetched from a database, in the form : {:fr "En Français" :en "In English" :en-Uk "In English from UK"}

What I would like to do in my program is to find the best matching translations from these 2 elements (in the example, it is :en-UK; note that I may not know in advance that :de is unavailable).

I haven't found a way to do that with the Tower library (and I'd like to avoid reinventing the wheel).

Does anyone know how I might proceed?

Thanks in advance!

Valentin Waeselynck
  • 5,950
  • 26
  • 43

2 Answers2

1

Just added native support for arbitrary locale fallbacks in Tower v2.1.0-SNAPSHOT, as per discussion here: https://github.com/ptaoussanis/tower/issues/43#issuecomment-42014418

So it'll now be possible to request a translation like (t [:fr-FR :en-US] :example/foo).

Cheers! :-)

0

You could get a sequence of keys from the map, get all the values that are common to both sequences, and then choose whatever one is preferred based on whatever logic fits your requirements.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • Unfortunately, what you're suggesting is more complicated than it sounds because of the hierarchy of locales. For instance, if my accepted-languages are `[:fr-FR, :en]` and my available locales are `#{:fr, :en}`, then intersecting both sequences will rule out `:fr`, although it is the right choice. This is the kind of complicated logic that makes me not want to bypass the Tower library. – Valentin Waeselynck May 01 '14 at 14:04
  • Can you not normalize your data first? – Jeremy May 01 '14 at 21:05
  • What do you mean by "normalize"? – Valentin Waeselynck May 01 '14 at 21:20
  • Make the data look like how you want it to look. – Jeremy May 06 '14 at 06:30