当前位置: 首页 > news >正文

i18n-demo

一、demo

1、资源文件准备

如我需要对menu、logMsg内容做国际化。

resources下放各个语言文件,直接放resources下都行。我是新建了一个myi18n文件夹,

(1)然后在myi18n上点击New--Resource Bundle

(2)在弹框中输入base name:

(3)点击+号添加多个区域,这里以添加zh和en为例

最后点击OK,可以看到自动生成了几个文件

(4)自定义内容:

① menu.properties:

menu.user=user
menu.role=role

②  menu_en_US.properties:

menu.user=user
menu.role=role

③  menu_zh_CN.properties:

menu.user=\u7528\u6237
menu.role=\u89d2\u8272

(5)同样的方法生成logMsg:

action.add=Add
action.delete=Delete
action.update=Update
action.enable=Enable
action.disable=Disable

  logMsg_zh_CN.properties:

#Unicode
action.add=æ·»å 
action.delete=å é¤
action.update=æ´æ°
action.enable=å¯ç¨
action.disable=ç¦ç¨
2、资源文件引入

需要引入才能生效,两种方法

(1)配置文件法

在application.properties文件中配置(如我选择了这种方法)

spring.messages.basename=myi18n/menu,myi18n/logMsg
spring.messages.encoding=UTF-8
(2)代码引入

或者新建配置文件,

    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();messageSource.setBasenames("myi18n/menu");messageSource.setDefaultEncoding("UTF-8");
3、util编写
package org.example.util;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;import java.util.Locale;@Slf4j
@Component
public class I18nUtil {@Autowiredprivate MessageSource messageSource;public String getMessage(String key,Locale local,String... params) {if(local == null){//local = Locale.getDefault();local = LocaleContextHolder.getLocale();}String msg = messageSource.getMessage(key,null,local);log.info("msg={}",msg);return msg;}
}
4、单元测试
package com.test;import lombok.extern.slf4j.Slf4j;
import org.example.I18nApplication;
import org.example.util.I18nUtil;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.test.context.junit4.SpringRunner;import java.util.Locale;@SpringBootTest(classes = {I18nApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class Test {@Autowiredprivate I18nUtil i18nUtil;@org.junit.Testpublic void test() {//logMsgString logMsgKey = "action.delete";//menuString menuKey = "menu.user";//Locale local = Locale.CHINA;String msg = i18nUtil.getMessage(menuKey,local);System.out.println(msg);}
}
5、测试
5.1、menu
 Locale local = Locale.CHINA;String msg = i18nUtil.getMessage(menuKey,local);

执行输出中文:用户

如果改成

Locale local = Locale.ENGLISH;

执行输出英文:user

5.2、logMsg
Locale local = Locale.ENGLISH;String msg = i18nUtil.getMessage(logMsgKey,local);

执行输出英文:delete

改成

Locale local = Locale.CHINA;String msg = i18nUtil.getMessage(logMsgKey,local);

执行输出:删除

5.3、默认Local

如我没有配置CANADA,

Locale local = Locale.CANADA;
String msg = i18nUtil.getMessage(logMsgKey,local);

执行输出英文:Delete。

5.4、{baseName}.properties

http://www.lryc.cn/news/362373.html

相关文章:

  • [Leetcode] 0-1背包和完全背包
  • 自定义类型:联合体和枚举
  • 【Cityengine】Cityengine生产带纹理的建筑模型导入UE4/UE5(下)
  • 详解51种企业应用架构模式
  • 【十年java搬砖路】Jumpserver docker版安装及配置Ldap登陆认证
  • C\C++内存管理(未完结)
  • 一个小时搞定JAVA面向对象(5)——抽象与接口
  • 图像关键特征描述方法-小目标
  • 【qt15】windeployqt 安装依赖
  • DETR论文重点
  • slf4j等多个jar包冲突绑定的排查方法使用IDEA的maven help解决
  • MySQL主从的延迟怎么解决呢?
  • 【一百】【算法分析与设计】N皇后问题常规解法+位运算解法
  • GPT-4:人工智能领域的新里程碑
  • mysql inset bug
  • oracle查看序列
  • flask-slqalchemy使用详解
  • Scala学习笔记8: 包
  • 分享一份糟糕透顶的简历,看看跟你写的一样不
  • VMware 三种网络模式
  • 红绿二分查找
  • C51单片机 串口打印printf重定向
  • PieCloudDB Database Flink Connector:让数据流动起来
  • 主机CPU访问PCIe设备内存空间和PCIe设备访问主机内存空间
  • 在家AIAA(美国航空航天学会)文献如何查找下载
  • dnf手游版游玩感悟
  • 安卓如何书写注册和登录界面
  • 黄仁勋的AI时代:英伟达GPU革命的狂欢与挑战
  • Linux云计算架构师涨薪班课程内容包含哪些?
  • c语言:自定义类型(枚举、联合体)