0

How to constrain to a set of locales?

asked 2019-02-24 21:14:41 +0800

jgomer2001 gravatar image jgomer2001
80 4

Hi,

I have a webapp that uses internationalization labels, eg.:

zk-label_de.properties

zk-label_fr.properties

zk-label.properties

I don't need/want to set the locale declaratively (xml) neither in code. I feel comfortable with how ZK chooses the locale based on browser setting (servletRequest.getLocale)

In default zk-label.properties file I have labels in English. Thus, if a users visits the app using a browser in Spanish (a language I have no translation texts), the content will be displayed in English

My problem is that certain components like MessageBox, Grid (paging), Calendar,etc. still show certain labels in Spanish. I would like to attach an image or link to better exemplify, but this form does not allow me. Hopefully my description is good enough

I noticed the labels found at zul-8.6.0.1.jar\metainfo\mesg\msgzul*.properties are used in this case

I would like to avoid this mix of languages. is there any way to workaround this?

For instance, can I say "hey ZK, this my set of supported locales: fr/de. If the browser doesn't match, simply go with default" ?

Any other idea is welcome. I simply want to avoid language mix

delete flag offensive retag edit

Comments

You saved the day again, thank you cor3000

jgomer2001 ( 2019-02-27 00:07:54 +0800 )edit

you're welcome

cor3000 ( 2019-02-27 15:20:10 +0800 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-02-26 18:14:36 +0800

cor3000 gravatar image cor3000
6280 2 7

The mechanism how ZK determines the current locale are described here:

https://www.zkoss.org/wiki/ZK%20Developer's%20Reference/Internationalization/Locale

You can choose any of the options described there, but as I understood you'd prefer a different solution a a lower level.

Obviously you could remove the language files directly from the jars - however that's not really nice.

At a lower level you can implement a ServletFilter and override the getLocale method using a ServletRequestWrapper

This allows you to limit the potential values returned by request.getLocale().

Here some untested code that how the ServletRequestWrapper can be used:

package zk.support.potential;

import javax.servlet.*;
import java.io.IOException;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class RestrictLocaleFilter implements Filter {
    Set<Locale> allowedLocales = new HashSet<>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //do some init here, e.g. read system properties or 
        //filter config parameter from web.xml
        allowedLocales.add(Locale.getDefault());
        allowedLocales.add(Locale.GERMAN);
        allowedLocales.add(Locale.FRENCH);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
            FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(new ServletRequestWrapper(servletRequest) {
            @Override
            public Locale getLocale() {
                Locale requestedLocale = super.getLocale();
                if(allowedLocales.contains(requestedLocale)) {
                    return requestedLocale;
                } else {
                    return Locale.getDefault();
                }
            }
        }, servletResponse);
    }

    @Override
    public void destroy() { }
}
link publish delete flag offensive 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

Follow
2 followers

RSS

Stats

Asked: 2019-02-24 21:14:41 +0800

Seen: 5 times

Last updated: Feb 26 '19

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