Reputation: 253
My question is the following:
I got one application running with GWT. At the startup of the application, one GWTClientModule which extends AbstractGinModule or another GWTClientModule is loaded.
Based on that switch I would like to set the local. Is there a way to set the local not directly in .html by doing as follow:
<meta name="gwt:property" content="locale=fr">
but directly in one of the selected GWTClientModule?
Thank you very much for your kind assistance ;-)
Upvotes: 1
Views: 891
Reputation: 1101
We used a workaround here based on this to set the locale according to the client browsers language.
Simply add the following Javascript to your .html :
<script type="text/javascript">
var search = location.search;
if(search.indexOf("locale") == -1){
var lang = navigator.language!= null ? navigator.language : navigator.browserLanguage;
lang = lang.replace("-", "_");
var metaTag = document.createElement('meta');
metaTag.name = "gwt:property";
metaTag.content = "locale=" + lang;
document.getElementsByTagName('head')[0].appendChild(metaTag);
}
</script>
It looks if the meta tag corresponding to the locale isn't set, if not it looks up the browser language and add the proper meta tag, EG :
<meta name="gwt:property" content="locale=fr">
Upvotes: 0
Reputation: 12692
You cannot do that if you use static String internationalization, since it uses deferred binding: the GWT compiler generates one JavaScript file per locale, and, when your page is loaded, the GWT bootstrap script looks for the "locale" property inside the page (or in the URL), before loading the appropriate version of the code.
Choosing the locale programmatically (inside your GWTClientModule) is impossible, since the localized version of your code is already loaded once that code executes...
If you really need to choose the locale at runtime, you might want to look into dynamic String internationalization, but it is less performant and you lose static type checking.
Upvotes: 1