go-playground / locales

:earth_americas: a set of locales generated from the CLDR Project which can be used independently or within an i18n package; these were built for use with, but not exclusive to https://github.com/go-playground/universal-translator
MIT License
268 stars 55 forks source link

Dynamic loading of locales #2

Closed nkev closed 7 years ago

nkev commented 7 years ago

Nice work, however when dealing with unknown locales, say based on the browser settings of users in a Web application, it would make sense to dynamically load locale data from a Json files right?

deankarn commented 7 years ago

Hello @nkev

I think I understand what you asking about dynamically loading locales, but don't really see any benefit loading from JSON, it's just an extra step.

eg. without JSON local.New() --> use locale

vs with JSON Define struct(with exposed fields!) --> Unmarshalled json into struct --> use locale

I used to have a map auto generated with a mapping of locale string to the New() function which would probably be what you're looking for without the JSON, I could add it back in if you'd like, but there are some down sides:

eg. using the map, because it references all the locales, causes all locales to be compiled into your application whether you will ever use them or not and increases compile time by a bit.

locales is really just a helper package though and is intended to be wrapped by either the user or another package as it provides all of the information for building an i18n package, but doesn't actually provide functionality for storing translations. I created an i18n package universal-translator that you regsiter all your applications supported locales, avoiding unnecessary locales from being compiled in, and even a fallback locale for unsupported ones and it has functions for selecting the locale based on ones you've extracted from the browser's headers(I may add another helper function for extracting the headers for the user soon).

Sorry for being so long winded, please let me know if this addresses you question about dynamically loading locales.