Results 1 to 9 of 9

Thread: spring internationalization message

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    There is no property file named resources.messages.properties. There is a messages.properties which resides in resources (not sure what your packaging is).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default 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 )

  6. #6

    Default

    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

  7. #7
    Join Date
    Jan 2008
    Posts
    1,826

    Default

    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).
    Rob Winch - @rob_winch
    Spring Security Lead
    Pivotal

  8. #8

    Thumbs up

    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

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •