Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用正确的语言印刷国家的春天内化_Java_Spring - Fatal编程技术网

Java 用正确的语言印刷国家的春天内化

Java 用正确的语言印刷国家的春天内化,java,spring,Java,Spring,当我想访问地址为?language=en或language=fr的页面时,我使用以下控制器: public class AddressController { private static Logger logger = Logger.getLogger(AddressController.class); @RequestMapping(value="/address",method=RequestMethod.GET) public ModelAndView init

当我想访问地址为?language=en或language=fr的页面时,我使用以下控制器:

public class AddressController {
    private static Logger logger = Logger.getLogger(AddressController.class);

    @RequestMapping(value="/address",method=RequestMethod.GET)
    public ModelAndView init(HttpServletRequest r){
            ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/spring-servlet.xml");
            SessionLocaleResolver resolver = (SessionLocaleResolver) context.getBean("localeResolver");

            String[] locales = resolver.resolveLocale(r).getISOCountries();
            //String[] locales = Locale.getISOCountries();
            Map<String,String> countryList = new HashMap<String,String>();

            for(String countryCode : locales){
                 Locale loc = new Locale(resolver.resolveLocale(r).getLanguage(),countryCode);
                 countryList.put(loc.getDisplayCountry(), loc.getDisplayCountry());
                 logger.info(loc.getDisplayCountry());
             }

             Map<String,String> tree = new TreeMap<String,String>(countryList);
             ModelAndView modelAndView = new ModelAndView("address");
             modelAndView.addObject("address",new Address());
             modelAndView.addObject("countriesList", tree);

             return modelAndView;
     }
}
spring-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.application.myGoogleAppEngine.controller" />
    <!-- <mvc:annotation-driven /> -->

    <!-- Register the messageBundle.properties -->
    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
       <property value="classpath:MessageBundle,classpath:Messages" name="basenames" />
       <property value="UTF-8" name="defaultEncoding" />
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="fr" />
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
          <property name="paramName" value="language" />
    </bean>

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
         <property name="interceptors">
               <ref bean="localeChangeInterceptor" />
         </property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property value="org.springframework.web.servlet.view.JstlView" name="viewClass" />
         <property name="prefix">
              <value>/WEB-INF/pages/</value>
         </property>
         <property name="suffix">
             <value>.jsp</value>
         </property>
    </bean>

</beans>
当我通过调用/address?language=en或/address?language=fr访问控制器时,我注意到循环“forString countryCode:locales”中的记录器显示每个国家的名称都是用法语打印的,而不是用英语打印的


您有什么解决方案吗?

为什么不使用请求参数“语言”来创建国家/地区列表?只要会话保持活动状态,SessionLocalProvider将始终返回相同的语言

以下代码示例使用该参数构建本地化国家/地区名称的列表:

@Controller
public class CountriesController {

    @RequestMapping(value="/countries", method=RequestMethod.GET)
    public ModelAndView getCountries(@RequestParam("language") String language){

        List<String> countries = new ArrayList<>();

        Locale locale = new Locale(language);
        String[] isoCountries = Locale.getISOCountries();
        for (String isoCountry : isoCountries) {

            Locale countryLoc = new Locale(language, isoCountry);
            String name = countryLoc.getDisplayCountry(locale);

            if (!"".equals(name)) {
               countries.add(name);
            }

         }

         ModelAndView model = new ModelAndView("countries");
         model.addObject("model", countries);

        return model;
   }
}