简体   繁体   English

Spring Boot i18n问题

[英]Spring Boot i18n issue

I am learning spring Boot framework right now, I am trying to apply i18n concept on my simple application. 我现在正在学习spring Boot框架,我正在尝试在简单的应用程序中应用i18n概念。 But whenever I run the application, the following error returned: "No message found under code 'label.welcomeMessage' for locale 'en_US'." 但是,每当我运行该应用程序时,都会返回以下错误: "No message found under code 'label.welcomeMessage' for locale 'en_US'." . I have read about this issue and tried a lot before I getting here to ask but noting have worked with me. 在到达这里询问之前,我已经阅读了有关此问题的内容并尝试了很多,但注意到已经与我合作了。

Here is my AppClass configuration: 这是我的AppClass配置:

package com.abed.main.configuration;


import java.util.Locale;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class AppConfig implements WebMvcConfigurer{



    @Bean
    public LocaleResolver localeResolver()
    {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr ; 
    }

    @Bean
    public LocaleChangeInterceptor LocaleChangeInterceptor()
    {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci ;
    }
    {

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(LocaleChangeInterceptor());


    }


    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource msgSrc = new ReloadableResourceBundleMessageSource();
        msgSrc.setBasename("classpath:messages/ticket");
        msgSrc.setDefaultEncoding("UTF-8");
        return msgSrc;
    }

}

Here is the welcome JSP: 这是受欢迎的JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
     <%@ taglib prefix = "spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><spring:message code="label.welcomePageTitle"></spring:message></title>
</head>
<body>

<h1><spring:message code="label.welcomeMessage"></spring:message></h1>
<form action="ticket" method="GET">
<spring:message code="label.ticketId"></spring:message><input type="text" name="Student_Id">
<input type="submit" value="<spring:message code="label.search"></spring:message>">
</form>

<a href="createTicket"><spring:message code="label.createTicketSubmit"></spring:message></a>
</body>
</html>

and here is the hierarchy of my application: 这是我的应用程序的层次结构:

应用程序层次结构

Any Help Please , Thanks in advance 任何帮助请,在此先感谢

You need to configuration a bean named messageSource 您需要配置一个名为messageSourcebean
here is the code, add it to a java code file, make sure @Configuration can be scan: 这是代码,将其添加到java代码文件中,确保可以扫描@Configuration

@Configuration
public class MessageSourceConfig {

    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageSource() throws Exception {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        resourceBundleMessageSource.setBasenames("i18n/messages");
        return resourceBundleMessageSource;
    }
}


The point is : resourceBundleMessageSource.setBasenames("i18n/messages"); 关键是: resourceBundleMessageSource.setBasenames("i18n/messages"); remember the base path is classpath , so the i18n properties resource should set in i18n/messages . 请记住,基本路径是classpath ,因此i18n属性资源应在i18n/messages设置。 In order not to crash problem, I suggest you create the properties by using IDEA . 为了避免崩溃,建议您使用IDEA创建属性。

Quote Spring official doc: 引用Spring官方文档:

Strategy interface for resolving messages, with support for the parameterization and internationalization of such messages. 用于解析消息的策略接口,并支持此类消息的参数化和国际化。 Spring provides two out-of-the-box implementations for production: ResourceBundleMessageSource , built on top of the standard ResourceBundle ReloadableResourceBundleMessageSource , being able to reload message definitions without restarting the VM. Spring为生产提供了两种开箱即用的实现: ResourceBundleMessageSource ,它是在标准ResourceBundle ReloadableResourceBundleMessageSource之上构建的,能够在不重新启动VM的情况下重新加载消息定义。

This is the reason we need to configure a bean named messageSource . 这就是我们需要配置一个名为messageSource的bean的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM