-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Is there a way to determine what is the current language/locale from ZK Client Engine?
If not, the best I could come up was this: Implement a custom org.zkoss.zk.ui.metainfo.MessageLoader to append a small javascript snippet to zul.lang.wpd, which would store the current language in a global JS variable.
Or is there a better way?
The answer is yes... and no!
You may use the javascript function window.navigator.language or window.navigator.userLanguage to get the language of your browser and send it back to the server to store it for future use. Here is a simple example:
<zk>
<window id="winMain" title="Locale" border="none"
xmlns:n="native" xmlns:w="client"
apply="snippets.json.LocaleController">
<vlayout>
<button w:onClick="sendLocale();">Determine my locale</button>
Your language is: <textbox id="txtLang" />
</vlayout>
</window>
<script type="text/javascript"><![CDATA[
/*
* Get the browser's language and send it back to server
*/
function sendLocale() {
var lang = window.navigator.userLanguage || window.navigator.language;
zAu.send(new zk.Event(zk.Widget.$('$winMain'), 'onSendLanguage', lang));
}
]]></script>
</zk>
and a simple controller:
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Textbox;
public class LocaleController extends SelectorComposer<Component> {
private static final long serialVersionUID = 1L;
@Wire private Textbox txtLang;
@Listen("onSendLanguage = window")
public void onSendLanguage(Event e) {
String language = (String) e.getData();
txtLang.setValue( language );
}
}
But be aware that the returned language may differ and does not reflect the actual regional settings of the user. If you are using Internet Explorer then this function returns the language defined in the windows regional settings control panel. In all other browsers (chrome/ff/safari) returns the preferred language defined in the browser settings. Even if i am a user in Greece it doesn't mean i have changed the default language of my browser. So in most cases you will see 'en-US'.
Hope that helps
Costas
Asked: 2014-11-28 13:48:11 +0800
Seen: 23 times
Last updated: Nov 28 '14
How to get web application attribute in javascript? [closed]
How to stop combining commands in an ajax call ?
zk.auSend not called on composer
Font Awesome Problem with IE8 when security option "font download" is disabled
Send a client event with JSON data
How to capture the Browser Window Resize/Size Event
Toggled Popup - Close ONLY on toggling, not on focus lost