Hi Ute
sorry for the late reply. Thanks for sharing your implementation, I
appreciate it!
I am pretty sure that I will go for the Apache rewrite approach, but
it's good to have this solution as a fallback ;-).
Kind regards,
Florian
On 1/20/26 11:26 AM, Ute Kaiser wrote:
Hi Florian,
my Interceptor to get language from URL https://myURL/myApp/<de/en>/myAction.xxx
package whatever.your.project;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ActionContext;
import org.apache.struts2.ActionInvocation;
import org.apache.struts2.interceptor.Interceptor;
import org.apache.struts2.ServletActionContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.util.Locale;
/**
* Extract language (de or en) from request und set struts constant
WW_TRANS_I18N_LOCALE.
* @param ActionInvocation invocation
* @return String invocation.invoke()
*/
public class LocaleInterceptor implements Interceptor
{
private Log log = LogFactory.getLog ( getClass () );
@Override
public void init() {}
@Override
public void destroy() {}
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
//extract language from URL /en/... or /de/... and set Locale.
Default englisch
String uri = request.getRequestURI();
Locale myLocale = uri.contains("/de/") ? Locale.GERMANY :
Locale.US;
session.setAttribute("WW_TRANS_I18N_LOCALE", myLocale);
// additionally for problems with a night batch job
ActionContext.getContext().withLocale(myLocale);
log.debug("Executing my LanguageGetterSetter: myLocale= " +
myLocale.toString());
return invocation.invoke();
}
}
struts.xml:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<package name="myApp" extends="struts-default">
...
<interceptors>
<interceptor name="LocaleInterceptor"
class="whatever.your.project.LocaleInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="LocaleInterceptor" />
<interceptor-ref name="defaultStack">
...
<action name="{lang}/MyAction" class="whatever.your.project.MyAction"
method="execute">
...
MyAction.java:
@Namespace("/{lang}")
public final class MyAction extends ...
That was enough to get the language specific resource properties.
It was my first struts2/7 application, migrating from struts1, so there might
be better solutions.
It worked only with the namespace.
In some jsp, I needed explicitly the language:
<s:set var="sprache" value="#attr['locale'].language.toUpperCase()"/>
//sprache=German for language
And to add more complexity, I needed some language specific fields stored in
database (e.g. descriptionDE or descriptionEN) instead of resource properties.
<s:myproperty value="something.description" ...
public class MypropertyTag extends PropertyTag {
public void setValue(String value) {
String sprache =
ActionContext.getContext().getLocale().getLanguage().toUpperCase();
super.setValue(value + sprache);
}
}
I copied struts-tags.tld into my project and added the tag myproperty (mostly
as a copy of the tag property).
Kind regards Ute
(inconsistency may be caused by hiding my application names)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]