Announcement Announcement Module
Collapse
No announcement yet.
spring internationalization message Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring internationalization message

    Hi

    I need to show spring security messages like 'Bad Credentials' in several languages. I still didn´t try experts stuffs like change languages and so, I´m just with the basis.

    In doc you can see:


    Shipping in the spring-security-core-xx.jar you will find an
    org.springframework.security package that in turn contains a messages.properties file. This should be referred to by your ApplicationContext, as Spring Security classes implement Spring's MessageSourceAware interface and expect the message resolver to be dependency injected at application context startup time. Usually all you need to do is register a bean inside your application context to refer to the messages. An example is shown below:
    Code:
    <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="org/springframework/security/messages"/>
    </bean>
    (...) If you do not register a message source, Spring Security will still work correctly and fallback to hard-coded English versions of the messages.

    If you wish to customize the messages.properties file, or support other languages, you should copy the file, rename it accordingly, and register it inside the above bean definition. (...)
    So, this is what I did, and didn´t work:

    1* Copy the message.properties inside the jar and move to my project:
    myProject/src/resources/messages_es_ES.properties

    In order to be sure, I renamed this file to messages.properties and messages_en.properties, all of then content spanish text. No matter the locale of my browser, the messages should be spanish.


    2* Put this code above in my app-context-security.

    Code:
    <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    	<beans:property name="basename" value="resources.messages"/>
    </beans:bean>
    3* In my login.js a show this message:
    Code:
    <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
    And it is still English!!




    Thanks

  • #2
    There is no property file named resources.messages.properties. There is a messages.properties which resides in resources (not sure what your packaging is).

    Comment


    • #3
      Thanks Marten

      You are right, I think I didn´t explain acuratly.

      In org.springframework.security there are several files messages.properties, each for one language (cs_CZ, de, es, etc..).

      What I did was to create a folder named resources in /src directory, and move messages_es_ES.properties inside.
      I though, thats would be enough, and it would work, but didn´t.

      Just to make some trys, I renamed this file (the spanish one) with the enlgish sufix (I Just wanted to discart that the problem was that the system was using the english locale), but the same outcome.

      I tried to specify the paht, instead with dots (resources.messages) withl '/', but didn´t work neiter (resources/messages) :

      Code:
      <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      	<beans:property name="basename" value="resources/messages"/>
      </beans:bean>
      I think I missing son conffiguration more, but I don´t know.

      Thanks again for your time
      Regards

      Comment


      • #4
        Make sure the file(s) are actually there and that they are at the root of the classpath (ie in /WEB-INF/classes/resources). If they aren't is isn't going to be found. If it is somewhere else you will need to add the approprate prefix (file:/classpath:/http.

        Comment


        • #5
          Solved!!!

          Thank you very much

          As always, the solution was the easiest one:

          Code:
          <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
          	<beans:property name="basename" value="classpath:/resources/messages"/>
          </beans:bean>
          Thanks

          (This is a very very very usefull forum )

          Comment


          • #6
            Hi again

            Once I finally got internazionalize spring security messages, a new problem arise.
            ¿How can I change the appliation locale an show messages in several languages?

            I was checking some post like this.

            The idea would be share struts locale (Globals.LOCALE_KEY) among spring. The user can change struts locale and it works fine, but spring doesn´t use it.

            So, I read that the solution is to have my own locale resolver and override the getDefaultLocale or resolveLocale, but its never called.


            This is my structure:
            Web.xml
            Code:
            	<listener>
            	   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
            	</listener>

            appContext (app-context-security)
            Code:
            <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
                    <beans:property name="basename" value="classpath:/resources/errores"/>
            	</beans:bean>		
            
            	<!-- declare the resolver -->
            	<beans:bean id="localeResolver" class="com.myapp.myLocaleResolver"/>

            And myLocaleResolver extends from SessionLocaleResolver.

            What was I missing?


            Thanks

            Comment


            • #7
              Write a StrutsRequestContextListener that looks up the Locale from session and then places sets it on the LocaleContextHolder. Ensure to call resetLocaleContext after the request is destroyed. You can view the source of RequestContextListener for an example (the only difference is you will obtain the locale from session).

              Comment


              • #8
                Hi

                I forgot thanks you for your answer.

                Finally I solved my problem as you said:
                1- Create my own RequestContextListener.
                Code:
                public class MyRequestContextListener extends RequestContextListener {
                    
                	@Override
                    public void requestInitialized(ServletRequestEvent requestEvent) {
                		  
                		  super.requestInitialized(requestEvent);
                
                		  //Obtener el idioma de la aplicaicón de la sesión
                          HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
                         
                          HttpSession sesion = request.getSession(false);
                          if (sesion != null){
                        	  Locale idioma = (Locale)sesion.getAttribute(Globals.LOCALE_KEY);          
                        	  LocaleContextHolder.setLocale(idioma);
                          }
                    }
                }
                2- Define the listener in web.xml:
                Code:
                <listener>  
                         <listener-class>xx.xxx.xxx.MyRequestContextListener</listener-class>  
                    </listener>

                Regards and thanks again

                Comment


                • #9
                  I would have created a LocaleResolver instead of registering an extra listener, that would have been easier. Basically the code in your Listener could have been in the resolver.

                  Code:
                  public class StrutsSessionLocaleResolver extends AbstractLocaleResolver {
                  
                  
                  	public Locale resolveLocale(HttpServletRequest request) {
                  		Locale locale = (Locale) WebUtils.getSessionAttribute(request, Globals.LOCALE_KEY);
                  		if (locale == null) {
                  			locale = determineDefaultLocale(request);
                  		}
                  		return locale;
                  	}
                  
                  	/**
                  	 * Determine the default locale for the given request,
                  	 * Called if no locale session attribute has been found.
                  	 * <p>The default implementation returns the specified default locale,
                  	 * if any, else falls back to the request's accept-header locale.
                  	 * @param request the request to resolve the locale for
                  	 * @return the default locale (never <code>null</code>)
                  	 * @see #setDefaultLocale
                  	 * @see javax.servlet.http.HttpServletRequest#getLocale()
                  	 */
                  	protected Locale determineDefaultLocale(HttpServletRequest request) {
                  		Locale defaultLocale = getDefaultLocale();
                  		if (defaultLocale == null) {
                  			defaultLocale = request.getLocale();
                  		}
                  		return defaultLocale;
                  	}
                  
                  	public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
                  		WebUtils.setSessionAttribute(request, Globals.LOCALE_KEY, locale);
                  	}
                  
                  }
                  But if it works, it works . that i the most important basically in the end, at least for the end user.

                  Comment

                  Working...
                  X