Spring i18n国际化

打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

从源码MessageSource的三个实现出发实战spring·i18n国际化 - 简熵 - 博客园
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.context.MessageSource;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.support.ReloadableResourceBundleMessageSource;
  7. import java.util.Locale;
  8. import java.util.ResourceBundle;
  9. @SpringBootApplication
  10. public class HelloWorldSpringbootApplication {
  11.     // ReloadableResourceBundleMessageSource可以加载指定项目以外的国际化文件
  12.     private static String zhPath =
  13.             "file:D:/temp/content";
  14.     public static void main(String[] args) {
  15.         // SpringApplication.run(HelloWorldSpringbootApplication.class, args);
  16.         getSpringContent();
  17.     }
  18.     @Bean
  19.     public ReloadableResourceBundleMessageSource messageSource() {
  20.         ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  21.         messageSource.setBasenames(zhPath, "classpath:content");
  22.         return messageSource;
  23.     }
  24.     public static void getSpringContent() {
  25.         AnnotationConfigApplicationContext ctx =
  26.                 new AnnotationConfigApplicationContext(HelloWorldSpringbootApplication.class);
  27.         MessageSource source = ctx.getBean(MessageSource.class);
  28.         Locale localeZh = new Locale("zh","CN");
  29.         String hello = source.getMessage("hello", null, localeZh);
  30.         System.out.println("hello = " + hello);
  31.     }
  32.     public static void getContent() {
  33.         Locale localeEn = new Locale("en","US");
  34.         Locale localeZh = new Locale("zh","CN");
  35.         ResourceBundle res = ResourceBundle.getBundle("content", localeZh);
  36.         String hello = res.getString("hello");
  37.         System.out.println("hello = " + hello);
  38.     }
  39. }
复制代码
再加三个文件:
  1. content.properties
复制代码
  1. hello=123
复制代码
  1. content_en_US.properties
复制代码
  1. hello=hello_world
复制代码
content_zh_CN.properties
  1. hello=456
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

滴水恩情

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表