简体   繁体   中英

Grails: how to set language dynamically in gsp or using js

I want to set language value of a gsp page dynamically . Currently I am just doing it using basic hardcoded value . I did find something with JS Onload event described here .

But I wanted to find something that is GSP driven . Is there any way ?

My current code looks like <html lang="en">

I think maybe you are thinking of this in a more complex way than it actuall is.

In grails you have your layouts/main.gsp which is your sitemesh.

The tag <html lang='en' is declared at the very top of this

If you simply edit this page and add the following:

<g:set var="locale" value="${session?.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'?:java.util.Locale.UK}"/>
<html lang="${locale?.language?:'en'}" class="no-js">

Then when I visit my site: localhost:8080/?lang=ja_JP view source shows: <html lang="ja" class="no-js">

You need to do that for each sitemesh that requires to do this - having a read about this property it seems it doesn't do much for the browser but may help non human things such as search engines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM