ToB企服应用市场:ToB评测及商务社交产业平台
标题:
Spring i18n国际化
[打印本页]
作者:
滴水恩情
时间:
2024-10-24 22:49
标题:
Spring i18n国际化
从源码MessageSource的三个实现出发实战spring·i18n国际化 - 简熵 - 博客园
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import java.util.Locale;
import java.util.ResourceBundle;
@SpringBootApplication
public class HelloWorldSpringbootApplication {
// ReloadableResourceBundleMessageSource可以加载指定项目以外的国际化文件
private static String zhPath =
"file:D:/temp/content";
public static void main(String[] args) {
// SpringApplication.run(HelloWorldSpringbootApplication.class, args);
getSpringContent();
}
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames(zhPath, "classpath:content");
return messageSource;
}
public static void getSpringContent() {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldSpringbootApplication.class);
MessageSource source = ctx.getBean(MessageSource.class);
Locale localeZh = new Locale("zh","CN");
String hello = source.getMessage("hello", null, localeZh);
System.out.println("hello = " + hello);
}
public static void getContent() {
Locale localeEn = new Locale("en","US");
Locale localeZh = new Locale("zh","CN");
ResourceBundle res = ResourceBundle.getBundle("content", localeZh);
String hello = res.getString("hello");
System.out.println("hello = " + hello);
}
}
复制代码
再加三个文件:
content.properties
复制代码
hello=123
复制代码
content_en_US.properties
复制代码
hello=hello_world
复制代码
content_zh_CN.properties
hello=456
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4