gpt4 book ai didi

java - Spring Boot i18n 问题

转载 作者:行者123 更新时间:2023-12-02 10:59:44 25 4
gpt4 key购买 nike

我现在正在学习 spring Boot 框架,我正在尝试在我的简单应用程序中应用 i18n 概念。但每当我运行该应用程序时,都会返回以下错误:“在语言环境“en_US”的代码“label.welcomeMessage”下找不到消息。”。在我来这里询问之前,我已经读过有关这个问题的文章并尝试了很多,但注意到对我有用。

这是我的应用程序类配置:

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;
}

}

这是欢迎 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>

这是我的应用程序的层次结构:

Application hierarchy

请提供任何帮助,提前致谢

最佳答案

您需要配置一个名为messageSourcebean
这是代码,将其添加到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;
}
}


重点是:resourceBundleMessageSource.setBasenames("i18n/messages");请记住基本路径是classpath,因此i18n属性资源应在i18n/messages中设置。为了避免崩溃问题,我建议您使用IDEA创建属性。

引用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.

这就是我们需要配置名为 messageSource 的 bean 的原因。

关于java - Spring Boot i18n 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51424451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com