0

For example, suppose my app have some localization texts. My question is, should I load appropriate locale at start:

var locale=userPrefs.getLocale();
localeStringMap=getLocaleTextFromFile("locale_string_"+locale+".json");

//some other code
//.
//.
//.

var message=localeStringMap["welcome_to_use_the_app"];

or get the locale preference when accessing localized string each time:

var localeStringMap={
  "en":getLocaleTextFromFile("locale_string_en.json"),
  "es":getLocaleTextFromFile("locale_string_es.json"),

};
//some other code
//.
//.
//.

var locale=userPrefs.getLocale();
var message=localeStringMap[locale]["welcome_to_use_the_app"];

?

  • How often do you expect a user to change locales? – 1201ProgramAlarm Apr 13 '18 at 3:51
2

There are three approaches you could use:

Pre-load

Load any and all locale files immediately. All locale information is therefore retained in memory and requires no further loading at runtime.

Advantage: Fast. You suffer a slightly worse startup in exchange for having unbeatable load times whenever you need it.

Disadvantage: Memory usage. Depending on how many locales you intend to support, this may not be ideal. If the user does not often change locales (something I would expect), then you suffer the memory usage penalty without the benefits.

Lazy-load

Load locale files only when needed. If you think you might need the locale file on startup, load a default locale. Use a map/dictionary to keep track of which user is using which locale, and have only one copy loaded per active locale. When user logs out, if no other user is using that locale, you may choose to abandon it or not, depending on the importance of memory.

Advantage: Low memory usage.

Disadvantage: Slight load delays when unloaded locale is requested.

Dynamic

For any given request made by the user, load once using active locale. Any potential changes are made immediately evident and don't require explicit reloading.

Advantage: Changes are immediate. Fastest to implement. Minimal memory.

Disadvantage: High load delays. If load times are low enough, this may not even be a big deal for you.

Conclusion

If you'll notice, there is a sort of scale here. The tradeoff is speed/memory and as you move from top approach to bottom, you improve one and worsen the other. Which one is best? That depends on what you want to optimize. If you want speed, preload. If you want memory, make it dynamic. If both carry equal importance, consider lazy-loading as it is the best of both worlds in my humble opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.