Determine current language/locale from Javascript

asked 2014-11-28 13:48:11 +0800

gediminas gravatar image gediminas
39 3

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?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-11-28 14:36:36 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

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:

<window id="winMain" title="Locale" border="none" 
        xmlns:n="native" xmlns:w="client" 

        <button w:onClick="sendLocale();">Determine my locale</button> 
        Your language is: <textbox id="txtLang" />


<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));

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


link publish delete flag offensive edit


I was looking for Locales.getCurrent() equivalent in JS (i.e. current ZK application locale(language), not the one from browser's settings). I guess I'lll have to stick to my custom solution then.

gediminas ( 2014-12-01 07:58:38 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2014-11-28 13:48:11 +0800

Seen: 22 times

Last updated: Nov 28 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More