Setting Locale language dynamically initially

1,143 views
Skip to first unread message

Rana Issa

unread,
Jul 4, 2012, 5:05:37 AM7/4/12
to google-we...@googlegroups.com
Hi,

I know that you could initially set a language as a default one using the module file in GWT like this:

<set-property name="locale" value="en"/>

However, I need to display my application initially, in the language the user-agent's is using, i.e. depending on the Geographical IP of the user. Currently I am able to get the language and the country from which the user is connecting.
I thought of changing the locale through a query parameter (in the URL), but I don't like this idea because it reloads the page. Is there any other way to change the locale while launching the application dynamically?

Many thanks in advance
Rana

Jens

unread,
Jul 4, 2012, 5:40:21 AM7/4/12
to google-we...@googlegroups.com
You could build your app's host html page dynamically (jsp, php, whatever) and add a <meta> property to the <head> as described in https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nLocale#LocaleSpecifying

That way your app loads directly the specified language if its available, otherwise it loads the default language.

-- J.

Rana Issa

unread,
Jul 17, 2012, 3:44:43 AM7/17/12
to google-we...@googlegroups.com
Thanks a lot, I will try this
Rana

Thomas Broyer

unread,
Jul 17, 2012, 3:51:35 AM7/17/12
to google-we...@googlegroups.com


On Wednesday, July 4, 2012 11:40:21 AM UTC+2, Jens wrote:
You could build your app's host html page dynamically (jsp, php, whatever) and add a <meta> property to the <head> as described in https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nLocale#LocaleSpecifying

That way your app loads directly the specified language if its available, otherwise it loads the default language.

BTW, it looks like that's exactly what Google is doing for Groups: do a view-source and you'll see that <meta>. 
Message has been deleted

Joseph Lust

unread,
Jul 17, 2012, 11:08:13 PM7/17/12
to google-we...@googlegroups.com
Thomas,

The Groups page source is a fun read.

Question for you, is there a certain compiler flag that can be used to inline the CSS/JS (bootstraping - I see the code in there for injecting <script> tags) into the page like is done on Groups? The CSS adds to the load time, but if it's small enough, must be a win in the long run. Inlining the bootstrap script makes since given that it's a nocache file. I was thinking this would be neat for the compiler/Maven plugin, but assume it is a bespoke internal Google solution.

Another neat bit about the Groups pages are the extra headers added:
  1. x-content-type-options:
    nosniff
  2. x-frame-options:
    SAMEORIGIN
  3. x-ua-compatible:
    chrome=1
  4. x-xss-protection:
    1; mode=block

I'd never used these before, but I'll consider the security features now. The user agent flag makes me think that there is no permutation bootstrapping going on in the page after all, but rather server side, however there is still the bootstrapping code in the page. Odd.


Sincerely,
Joseph

Thomas Broyer

unread,
Jul 18, 2012, 6:23:20 AM7/18/12
to google-we...@googlegroups.com


On Wednesday, July 18, 2012 5:08:13 AM UTC+2, Joseph Lust wrote:
Thomas,

The Groups page source is a fun read.

Question for you, is there a certain compiler flag that can be used to inline the CSS/JS (bootstraping - I see the code in there for injecting <script> tags) into the page like is done on Groups?

You'd do that in a linker (to generate the HTML host page).
 
The CSS adds to the load time, but if it's small enough, must be a win in the long run. Inlining the bootstrap script makes since given that it's a nocache file. I was thinking this would be neat for the compiler/Maven plugin, but assume it is a bespoke internal Google solution.

I've been inlining the *.nocache.js file in my HTML host page with a simple @include JSP directive.
You have to include a special gwt:property meta then to tell the script where to find the permutations' scripts (have a look at the sources for Groups ;-) note the "standalone::" prefix is the name of the module, so that the gwt:property only applies to it –in case you have several modules in the same page–)
 
Another neat bit about the Groups pages are the extra headers added:
  1. x-content-type-options:
    nosniff
  2. x-frame-options:
    SAMEORIGIN
  3. x-ua-compatible:
    chrome=1
  4. x-xss-protection:
    1; mode=block

I'd never used these before, but I'll consider the security features now. The user agent flag makes me think that there is no permutation bootstrapping going on in the page after all, but rather server side, however there is still the bootstrapping code in the page. Odd.

Yes, they don't use a client-side selection-script, they use server-side negotiation (they generate a <script> pointing directly to the appropriate permutation script). The CrossSiteIframeLinker (xsiframe) is modular enough to allow it, but it requires a bit of coding (starting with extending the CrossSiteIframeLinker and overriding a few methods). I haven't yet dug deep enough so I don't know exactly what would be required.

Joseph Lust

unread,
Jul 18, 2012, 12:02:42 PM7/18/12
to google-we...@googlegroups.com
Very informative. Thanks Thomas!


Sincerely,
Joseph

Rana

unread,
Jul 31, 2012, 4:24:14 AM7/31/12
to google-we...@googlegroups.com
Hello Jens,

But, what if I don't want to use neither JSP nor PHP. Is there a way of doing it using GWT, or JavaScript?

Many thanks
Rana


On Wednesday, July 4, 2012 11:40:21 AM UTC+2, Jens wrote:

Paul Robinson

unread,
Jul 31, 2012, 4:32:21 AM7/31/12
to google-we...@googlegroups.com

On 31/07/12 09:24, Rana wrote:
Hello Jens,

But, what if I don't want to use neither JSP nor PHP. Is there a way of doing it using GWT, or JavaScript?
That'll be the "whatever" in Jens' email. You can generate a host html page with whatever server-side technology you're using. If you have a java server, then you can create a servlet that handles your host page and generates it.

Rana

unread,
Jul 31, 2012, 4:55:30 AM7/31/12
to google-we...@googlegroups.com
Hi,

But, I don't have a server-side technology, and I don't want to have one. The whole idea is to have only client-side HTML, JavaScript, CSS.... technology and that is it. That is why I need something using JavaScript or GWT to solve the problem.

Many thanks
Rana

Jens

unread,
Jul 31, 2012, 7:05:49 AM7/31/12
to google-we...@googlegroups.com
But, I don't have a server-side technology, and I don't want to have one. The whole idea is to have only client-side HTML, JavaScript, CSS.... technology and that is it. That is why I need something using JavaScript or GWT to solve the problem.

Ok take a look at com.google.gwt.i18n.I18N.gwt.xml

Seems like there are 5 ways to provide the locale:

1.) using a query param named "locale". To use this method you can let your web server send a redirect from app.example.com to app.example.com/?locale=<locale> after determining the locale on your web server if possible or you do the redirect from within your app, e.g. in your onModuleLoad() you use Window.Location.assign(<current url> + <locale query param>). You can change the name of the query param by setting a different value to "locale.queryparam".

2.) using a cookie. To use this you have to define the cookie name by setting "locale.cookie" to any value as in I18N.gwt.xml no default cookie name is defined.

3.) using meta tags. As already described you can include a gwt:property meta tag in a dynamic host page.

4.) using the user agent information. To use this you have to activate it by setting "locale.useragent" to "Y" as its disabled by default in I18N.gwt.xml.

5.) create your own property provider and use JavaScript to fill the "locale" property value yourself. Here you are completely free how to obtain the value.

GWT's default search order is "query param, cookie, meta, useragent" but cookie and useragent will be skipped if you don't configure/activate them. You could also modify the search order by setting "locale.searchorder" in your gwt.xml.

Now choose one solution ... 

-- J.

Rana

unread,
Aug 1, 2012, 5:47:07 AM8/1/12
to google-we...@googlegroups.com
Hi Jen,

Thanks a lot for the information.

I am in fact using/used the ways you have described, but not number 5. They are working fine, but I wanted to get initially a locale of what the user is using on his computer (from browser, etc), without having to reload the page. The issue is that the "locale.useragent"  parameter doesn't support all the languages our application does.

I will use the number 5 suggestion, it sounds very promising.

Many thanks
Rana
Reply all
Reply to author
Forward
0 new messages