Dynamically generating meta-tag for specifying locale

657 views
Skip to first unread message

rashmi

unread,
Nov 3, 2008, 5:36:06 PM11/3/08
to Google Web Toolkit
Hi all,
I'm new to GWT and I'm trying to implement GWT static string
internationalization
I understand that in order to specify a locale at run time you need to
use meta tag or rewrite URL.

If I add "locale=xx" in the meta tag or in the URL as below, I can see
the string internationalization working in my application.
<meta name="gwt:property" content="locale=de">
or
http://www.example.org/myapp.html?locale=de

But, I would like to dynamically generate the meta tag to set locale
at run time based on the client's browser locale. How to I change the
meta tag dynamically to use client's locale settings? Most browsers
send an Accept-Language header - is there any way in GWT to use that
information to dynamically generate the meta-tag?

I would really appreciate any reply.

Regards,
Rashmi

Ian Petersen

unread,
Nov 3, 2008, 10:19:14 PM11/3/08
to Google-We...@googlegroups.com
On Mon, Nov 3, 2008 at 5:36 PM, rashmi <rash...@gmail.com> wrote:
> But, I would like to dynamically generate the meta tag to set locale
> at run time based on the client's browser locale. How to I change the
> meta tag dynamically to use client's locale settings? Most browsers
> send an Accept-Language header - is there any way in GWT to use that
> information to dynamically generate the meta-tag?

Yes. Instead of serving a static host page, serve a dynamic host page
using your favourite server-side approach. On the server, read the
Accept-Language header and write an appropriate value into a meta tag
for consumption by GWT.

Ian

prody

unread,
Nov 4, 2008, 12:27:14 AM11/4/08
to Google Web Toolkit

> Yes.  Instead of serving a static host page, serve a dynamic host page
> using your favourite server-side approach.  On the server, read the
> Accept-Language header and write an appropriate value into a meta tag
> for consumption by GWT.
>
Hi Ian,
Thanks for your response. Could you please tell me how to generate
meta tag dynamically using GWT?
I know it's pretty simple using JSP but I have not been able to figure
out how to generate meta tag dynamically using GWT code. Thanks in
advance!

Ian Petersen

unread,
Nov 4, 2008, 12:38:23 AM11/4/08
to Google-We...@googlegroups.com
On Tue, Nov 4, 2008 at 12:27 AM, prody <prod...@gmail.com> wrote:
> Thanks for your response. Could you please tell me how to generate
> meta tag dynamically using GWT?
> I know it's pretty simple using JSP but I have not been able to figure
> out how to generate meta tag dynamically using GWT code. Thanks in
> advance!

I don't think it can be done. By the time you're executing GWT code,
it's too late to change the meta tag.

The host page (which is the page that needs to contain a meta tag)
executes the GWT bootstrap script. It's the bootstrap script that
looks at, amongst other things, the chosen locale, and then downloads
a script specific to the execution environment. It's that
environment-specific script that is your GWT app and, by the time it's
executing, the locale has already been chosen.

Ian

prody

unread,
Nov 4, 2008, 1:10:01 AM11/4/08
to Google Web Toolkit

>
> I don't think it can be done.  By the time you're executing GWT code,
> it's too late to change the meta tag.
>
> The host page (which is the page that needs to contain a meta tag)
> executes the GWT bootstrap script.  It's the bootstrap script that
> looks at, amongst other things, the chosen locale, and then downloads
> a script specific to the execution environment.  It's that
> environment-specific script that is your GWT app and, by the time it's
> executing, the locale has already been chosen.
>

I followed the approach given in Google Plats project.
http://code.google.com/p/narorumo/wiki/Plats

If I put the following lines in my application main html page, the
locale changes to that of my browser.

<head>
<script src="http://plats.appspot.com/put_variable.js?
available=fr,jp,es,en">
</script>
</head>

In the above code snippet, available parameter represents the
languages supported by my application.
put_variable.js script parses the Accept-Language header sent by my
browser, and puts the
<meta name="gwt:property" content="xxx">
tag in the application main html page.

xxx represents the locale matching the Accept-Language header.

Can I do something similar with Java code, since my backend is in
java?

Thanks

Thomas Broyer

unread,
Nov 4, 2008, 5:53:45 AM11/4/08
to Google Web Toolkit


On 4 nov, 07:10, prody <prody...@gmail.com> wrote:
> > I don't think it can be done.  By the time you're executing GWT code,
> > it's too late to change the meta tag.
>
> > The host page (which is the page that needs to contain a meta tag)
> > executes the GWT bootstrap script.  It's the bootstrap script that
> > looks at, amongst other things, the chosen locale, and then downloads
> > a script specific to the execution environment.  It's that
> > environment-specific script that is your GWT app and, by the time it's
> > executing, the locale has already been chosen.
>
> I followed the approach given in Google Plats project.http://code.google.com/p/narorumo/wiki/Plats
>
> If I put the following lines in my application main html page, the
> locale changes to that of my browser.
>
>   <head>
>     <script src="http://plats.appspot.com/put_variable.js?
> available=fr,jp,es,en">
>     </script>
>   </head>
>
> In the above code snippet, available parameter represents the
> languages supported by my application.
> put_variable.js script parses the Accept-Language header sent by my
> browser, and puts the
> <meta name="gwt:property" content="xxx">
> tag in the application main html page.

Well, actually, it's set_metatag.js (put_variable.js just sets a
global JavaScript variable)

> Can I do something similar with Java code, since my backend is in
> java?

Of course, and you can use some code from the GWT-Incubator to help
you. See http://groups.google.fr/group/Google-Web-Toolkit/t/84c0ad3907e603cd
(which appears to be, uh, no more than 5 days ago !!!)

ston...@gmail.com

unread,
Nov 4, 2008, 8:53:41 AM11/4/08
to Google Web Toolkit
I think that jsp is the better way. Just rename gwt html file to a jsp
file in which add some java code. And on client you should access the
jsp file.

prody

unread,
Nov 4, 2008, 2:29:32 PM11/4/08
to Google Web Toolkit

Thank you all for your reply.
I'm trying to use GWT incubator code. I could not find a working
example.

The documentation says-

// Per-request code for the host html page
String locale =
localeMatcher.findBestMatch(request.getHeader("Accept-language"));
// Store the selected locale in the host html page to be served, via
// <meta name='gwt:property' content='locale=<<locale>>'>
// Be sure to include a Vary: Accept-language header in the
response.

Just by adding <meta name='gwt:property' content='locale=<<locale>>'>
into html host page and then setting locale ="es" on server side
doesn't seem to work.

Has anyone been able to resolve this issue? Could you please provide a
working example(using jsp in host page or gwt incubator??

Thanks !
> > Rashmi- Hide quoted text -
>
> - Show quoted text -

Martin Trummer

unread,
Nov 5, 2008, 3:49:39 AM11/5/08
to Google Web Toolkit
here's what we do:
very simple java script (in the header section of the .html file)
of course the accept-language approach is much better and I also
consider implementing that approach: some day when I have some spare
time :)

...
<script type="text/javascript">
var lang="en";
if (navigator.language.indexOf("en") > -1) {
lang="en";
} else if (navigator.language.indexOf("de") > -1) {
lang="de";
}
document.write("<meta name='gwt:property' content='locale="+lang
+"'>");
</script>
<script type="text/javascript" language="javascript"
src="com.tfact.TFAct.nocache.js"></script>
</head>
Reply all
Reply to author
Forward
0 new messages