Announcement Announcement Module
Collapse
No announcement yet.
AcceptHeaderLocaleResolver and multiple locales Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AcceptHeaderLocaleResolver and multiple locales

    The AcceptHeaderLocaleResolver appears to only care about the first language in the HTTP accept-language header element. It would be nice if the locale resolver would use the entire set of locales and not just the first one.

    Does anybody have an implementation of a LocaleResolver that attempts to resolve using the entire list?

    Thanks,

    -Fred

  • #2
    Hi Fred,

    Do you find any solution to resolve entire list?

    Thanks,
    Yannick.

    Comment


    • #3
      workaround that i have wrote :

      Code:
      package com.sarbacane.forms.localResolver;
      
      import java.io.File;
      import java.util.ArrayList;
      import java.util.Enumeration;
      import java.util.List;
      import java.util.Locale;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.springframework.web.servlet.LocaleResolver;
      import org.springframework.web.servlet.i18n.AbstractLocaleResolver;
      
      /**
       * Personal Implementation of LocaleResolver that is like AcceptHeaderLocaleResolver but accepts setLocal.
       * So this LocaleResolver works fine with a LocaleChangeInterceptor.
       * This AcceptHeaderLocaleResolver consider also all elements of locales request list.
       *
       * @author Yannick Legros
       * @since 05.12.2012
       */
      public class AcceptHeaderLocaleResolver extends AbstractLocaleResolver implements LocaleResolver {
      	
      	private final String urllocalAttributeName = "urllocal"; 
      	private final String propertiesFilesPrefix = "messages_";
      	private final String propertiesFilesSuffix = ".properties";
      	private final String propertiesPath = "/WEB-INF/classes";
      	
      	private List<String> definedLanguage = null;
      
      	public Locale resolveLocale(HttpServletRequest request) {  
      
      		if(request.getAttribute(urllocalAttributeName)!=null)
      			return (Locale) request.getAttribute(urllocalAttributeName);
      		
      		return getFirstExistingLocale(request);
      	}
      	
      	private Locale getFirstExistingLocale(HttpServletRequest request) {
      		
      		if(definedLanguage==null)
      			definedLanguage = getDefinedLanguages(request);
      		
      		@SuppressWarnings("unchecked")
      		Enumeration<Locale> requestLocales = request.getLocales();
      		while(requestLocales.hasMoreElements()){
      			Locale locale = requestLocales.nextElement();
      			if(definedLanguage.contains(locale.getLanguage()))
      				return locale;
      		}
      		
      		//if no one request locals is define => return first locale of request
      		return request.getLocale();
      		
      	}
      
      	private List<String> getDefinedLanguages(HttpServletRequest request){
      		
      		List<String> languages = new ArrayList<String>();  
      		String languagesFolderPath = request.getSession().getServletContext().getRealPath(propertiesPath);  
      		File folder = new File(languagesFolderPath);  
      		File[] listOfFiles = folder.listFiles();  
      
      		for (int i = 0; i < listOfFiles.length; i++){  
      		   String fileName = listOfFiles[i].getName();
      		   if (fileName.startsWith(propertiesFilesPrefix) && fileName.endsWith(propertiesFilesSuffix)){  
      		      // Extract the language code, which is between the underscore and the .properties extension  
      		      String language = fileName.substring(9, fileName.indexOf(propertiesFilesSuffix));  
      		      languages.add(language);  
      		   }  
      		}
      		
      		return languages;
      		
      	}
      
      	public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
      		request.setAttribute(urllocalAttributeName, locale);
      	}
      
      }
      if someone have better or cleaner things to do i will be happy to get it.

      Yannick.

      Comment

      Working...
      X