-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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() { }
}
Asked: 2019-02-24 21:14:41 +0800
Seen: 5 times
Last updated: Feb 26 '19
You saved the day again, thank you cor3000
jgomer2001 ( 2019-02-27 00:07:54 +0800 )edityou're welcome
cor3000 ( 2019-02-27 15:20:10 +0800 )edit