Edit to clarify:
If my Struts 2 site is designed to support two languages, and a user comes to the site with a third (unsupported) language, how does the site determine which localisation to use?
If my project lists
src/main/resources/i18n/messages.properties
# Contains strings in Danishsrc/main/resources/i18n/messages_en.properties
# Contains strings in Englishthen I would expect Struts to serve the page in Danish to a user whose browser requests it in, say, German. But this is not the case! In reality, the page is served using the strings from the English localisation. Why is that, and how do I modify this?
To be clear, yes if the user requests http://localhost:8080/project/index.do?request_locale=de
then that obviously does not magically provide a German translation. All the same, if they use a German OS and browser, then that will be the language requested in the HTTP header. Given that the site is not able to fulfil that request, how does Struts choose which language to serve? It does not, after all, return the raw i18n keys themselves but the string values from some localitation. Which one, and why?
How do I set a 'fallback locale' for a Struts 2 site? It seems to prefer 'en' which is not what I want.
My project has the following files:
src/main/webapp/jsp/index.jsp
src/main/resources/i18n/messages_da.properties
# Contains strings in Danishsrc/main/resources/i18n/messages_en.properties
# Contains strings in Englishsrc/main/resources/struts.xml
# Complete xml file, including:
<constant name="struts.custom.i18n.resources" value="i18n.messages" />
Loading the index page, the preferred language that the user has set in the browser is correctly reflected in the rendered page. The page has links to reload in specific locales (http://localhost:8080/project/index.do?request_locale=en
) and these also work as expected.
However, if coming to the page with an unsupported locale (by way of one's browser settings, or explicitly via http://localhost:8080/project/index.do?request_locale=sw
) then the index page is rendered in English. I would prefer this to be in Danish. How do I set this fallback language?
Even changing the files to:
src/main/resources/i18n/messages.properties
src/main/resources/i18n/messages_en.properties
seems to have no effect. Specifically, setting my browser up to use Swahili only (so English is not even listed), and accessing the index page without specific parameters, the web page is still rendered in English and not using the strings from the 'bare' i18n file.
I'm hoping there is a setting in src/main/resources/struts.xml
or src/main/webapp/WEB-INF/web.xml
that I have missed (as this is my first foray into Struts)?
I came up with the following solution, using a custom Interceptor
. I would have preferred a simple entry in a configuration file, but this at least works.
package my.web.site.i18n;
import java.util.Locale;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LocaleInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;
private static final Locale LOCALE_DA = new Locale("da", "DK");
private static final Locale LOCALE_EN = new Locale("en", "GB");
@Override
public void destroy() {
}
@Override
public void init() {
}
/**
* When a user requests our site in an un-localised language, we want to respond in Danish.
*
* @param invocation the action invocation
* @return the incoming invocation, possibly with adjusted locale
* @throws Exception
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String invocationLanguage = invocation.getInvocationContext().getLocale().getLanguage();
if (!LOCALE_DA.getLanguage().equals(invocationLanguage)
&& !LOCALE_EN.getLanguage().equals(invocationLanguage)
) {
invocation.getInvocationContext().setLocale(LOCALE_DA);
}
return invocation.invoke();
}
}