htmljspstruts2xwork

How to set xml:lang value at runtime?


How to change xml:language in runtime?

The situation is that person has let's say 'EN' selected as the browser language. Some other user has e.g 'FI' in the very same language property. For the first guy everything is shown in English, for the second one in Finnish accordingly. The two persons use Google Chrome and because both have xml:lang set to default 'en-US' on the code, the Finnish text is treated as in English. Now what I want is that the Finnish text is in the runtime recognized as Finnish also in the xml:lang setting level. So, xml:lang value cannot be hard coded as it is now. I also don't want to make 7 different pages for seven languages just because I cannot modify one field in runtime.

My setting is one of having struts2 and xwork for i18n. Setup is a jsp file having html tag with variable xml:lang and that setup should be altered on runtime.


Solution

  • I assume you want to set this value at the <html> level.

    Example Current Code:

    <html lang="en" xml:lang="en">
    

    Example Revised Code:

    <html lang="${action.language}" xml:lang="${action.language}">
    

    Then, add a getLanguage() method on your Struts2 actions (just put it on a base action that everything inherits from), such as:

    public String getLanguage() {
        // return the appropriate language based on the user's choice
    }