4

I have a multilingual struts application and recently I upgraded the struts from 2.3.20 to 2.3.29. After upgrading, the Tamil language is not working i.e. even though if we select Tamil language, the texts are shown in English.

I checked the locale setting when we select Tamil language, it is correct i.e. request_locale=ta_IN .

I tried extending I18nInterceptor in my custom interceptor class and then override the getLocaleFromParam() method as below. This also didn't work.

So please let me know if any of you has a solution for this problem.

Tamil language was working fine in Struts 2.3.20

protected Locale getLocaleFromParam(Object requestedLocale) 
{
        Locale locale = null;
        if (requestedLocale != null) {
            locale = (requestedLocale instanceof Locale) ?
                    (Locale) requestedLocale :
                  LocalizedTextUtil.localeFromString   
                                (requestedLocale.toString(), null);
            if (locale != null) {
                logger.debug("applied request locale="+locale);
            }
        }
        return locale;
}
Rish
  • 1,303
  • 9
  • 22
  • The code seems correct, how did you add this interceptor to strust? Did you remove struts default `i18n` interceptor? – Alireza Fattahi Oct 04 '16 at 04:51
  • 1
    Please refer to http://stackoverflow.com/questions/36245849/struts-2-version-2-3-28-only-accepts-registered-locales – Alireza Fattahi Oct 04 '16 at 04:53
  • @AlirezaFattahi I referred that question for overriding getLocaleFromParam() method. How can we use locale which is not registered in JVM. I haven't removed default stack. The new interceptor code is as below. – Ajay A Kumar Oct 04 '16 at 05:01
  • As I read in another thread from struts 2.3.28 onwards, the i18n interceptor only accepts the locales which are registered to jvm, the list which is returned by Locale.getAvailableLocales() method. Is there any way to use locales not registered in JVM. For ex. Tamil (ta_IN) language of India. – Ajay A Kumar Oct 04 '16 at 05:08

1 Answers1

1

You need to change replace your interceptor.

The default stack is defined in struts as below (https://struts.apache.org/docs/struts-defaultxml.html):

<interceptor-stack name="defaultStack">
     <interceptor-ref name="exception"/>
     <interceptor-ref name="alias"/>
     <interceptor-ref name="servletConfig"/>
     <interceptor-ref name="i18n"/>
     <interceptor-ref name="prepare"/>
     <interceptor-ref name="chain"/>
     <interceptor-ref name="scopedModelDriven"/>
     <interceptor-ref name="modelDriven"/>
     <interceptor-ref name="fileUpload"/>
     <interceptor-ref name="checkbox"/>
     <interceptor-ref name="datetime"/>
     <interceptor-ref name="multiselect"/>
     <interceptor-ref name="staticParams"/>
     <interceptor-ref name="actionMappingParams"/>
     <interceptor-ref name="params"/>
     <interceptor-ref name="conversionError"/>
     <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
     </interceptor-ref>
     <interceptor-ref name="workflow">
          <param name="excludeMethods">input,back,cancel,browse</param>
     </interceptor-ref>
     <interceptor-ref name="debugging"/>

You need to define your own interceptor and add it to default stack

    <interceptor name="customi18n"
        class="foo.bar.CustomI18NInterceptor" />

And add it to your own stack:

//Give a new name to your stack
<interceptor-stack name="customDefaultStack">
      <interceptor-ref name="exception"/>
      <interceptor-ref name="alias"/>
      <interceptor-ref name="servletConfig"/>
      //Replace your customi18n interceptor
      <interceptor-ref name="customi18n"/>
     //Same as above
  .....

Make this stack your default

<default-interceptor-ref name="customDefaultStack"/>
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • I tried in the way you have told above. Still didn't work. When I try to print the locale info from session it prints Tamil language locale properly i.e. as 'ta_IN'. Still it renders the page in English. logger.info("Locale : " + session.getAttribute("WW_TRANS_I18N_LOCALE")); – Ajay A Kumar Oct 04 '16 at 06:56
  • What will happen if you set the default locale ins struts `` – Alireza Fattahi Oct 04 '16 at 07:02
  • It works fine if I make ta_IN as default locale in struts.xml. – Ajay A Kumar Oct 04 '16 at 07:21
  • Check your interceptor stacks, may be you are using other stacks too which still use struts default i18n interceptor. Put a break point in struts2 i18n interceptor and you should never reach it. – Alireza Fattahi Oct 04 '16 at 07:38
  • @AjayAKumar Session locale is only placeholder for the current locale, it does nothing with the page is rendered. http://stackoverflow.com/a/18534195/573032 – Roman C Oct 04 '16 at 22:20
  • @AlirezaFattahi Struts default i18n interceptor is never used. I tested as follows, request went through my custom interceptor and I print the locale in custom interceptor and it printed as 'ta_IN'. After that I printed locale as (`httpSession.getAttribute("WW_TRANS_I18N_LOCALE")`) in Action class as well, it also printed as 'ta_IN'. So I believe locale is set properly as ta_IN is not registered in JVM struts is ignoring the locale set and taking English as default locale. – Ajay A Kumar Oct 05 '16 at 03:52