iosphere / elm-i18n

Localization for Elm apps as a pre-build phase with import and export between elm code and CSV/PO
MIT License
71 stars 6 forks source link

Support run-time language switching #2

Open felixLam opened 7 years ago

felixLam commented 7 years ago

We might consider providing an additional elm writer that creates multilingual modules. This would allow elm-apps to switch at runtime.

Assuming that the following current "modules" exist per language containing one string constant each:

The multilingual mode would create a language definition, e.g.:

module Translation

type Language = De | En

It would then generate the following modules:

The elm app would then use Translation.Main or Translation.ErrorMessages.

Example implementation of helloWorld : Language -> String:

helloWorld : Language -> String
helloWorld language =
    case language of
        De -> Translation.Main.De.helloWorld
        En -> Translation.Main.En.helloWorld
ChristophP commented 7 years ago

@felixLam So in general this sounds a reasonable approach. Just some of my thoughts:

  1. For me (other than supporting JSON) it was not as much about runtime switching of the language as it is about not wanting to have to load and keep aroud lots of builds. I would like to keep it simple in that regard.
  2. I am not sure what you mean by language modules and how the language files you are using are structured. We are using the i18next client to load JSON files. We never really have many modules. We have one language file per language per app. So I don't really understand how that works with the modules or why you need them. One would suffice for the use cases I am familiar with.
  3. Also I don't have any experience with CSV or PO lang files, but it sounds to me like they are flat vs. the hierarchical format of JSON. So if I am correct you have to think about how t deal with nested values. The approach that I used here is to camelize nested values and make longer function names like I did here with the tigersRoar (https://github.com/ChristophP/elm-i18n-module-generator).
tayloredwebsites commented 7 years ago

Please look into using translation tables. I have dropped the idea of putting an Elm front end to a Rails site, because I will have to code, test and maintain four different sets of front end code: English, Bosnian, Croatian, and Serbian. Please look at the simple and effective approach that Rails uses for internationalization. see: http://guides.rubyonrails.org/i18n.html

purohit commented 6 years ago

@tayloredwebsites have you seen https://github.com/michaeljones/elm-message-format-experiment, which uses ICU syntax?