internationalization(i18n)

  • 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。
  • 它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。
  • 换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。
  • 开发这样的程序的过程,就称为国际化。

数据国际化

  • * 关键的思路是从请求作用域获取locale,然后查询对应的数据*

中文语言数据页面:只有中文数据

英文语言数据页面:只有英文数据

浏览器切换中文,英文

User

@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String locale;//通过此字段查询对应的数据

InitApplicationListener

package com.jege.spring.boot.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component; import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:spring的事件监听器的处理机制:在启动服务器的时候,插入默认数据
*/
@Component
public class InitApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
User user;
for (int i = 1; i < 21; i++) {
if (i % 2 == 0) {
user = new User("je哥" + i, 25 + i);
user.setLocale("zh");
} else {
user = new User("je-ge" + i, 25 + i);
user.setLocale("en");
}
userRepository.save(user);
}
} }

UserController

// 从user.jsp列表页面由easyui-datagrid发出ajax请求获取json数据
@RequestMapping("/json")
@ResponseBody
public Map<String, Object> json(@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "rows", defaultValue = "10") int rows, final String q, HttpServletRequest request) {
// 按照id降序
Sort sort = new Sort(Sort.Direction.DESC, "id");
// 封装分页查询条件
Pageable pageable = new PageRequest(page - 1, rows, sort);
// 拼接查询条件
Specification<User> specification = new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
if (!StringUtils.isEmpty(q)) {
list.add(cb.like(root.get("name").as(String.class), "%" + q + "%"));
}
if (request.getLocale().toString().contains("en")) {
list.add(cb.like(root.get("locale").as(String.class), "%en%"));
} else {
list.add(cb.like(root.get("locale").as(String.class), "%zh%"));
}
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
};
return findEasyUIData(userRepository.findAll(specification, pageable));
}

其他关联项目

源码地址

https://github.com/je-ge/spring-boot

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!



Spring Boot 系列教程16-数据国际化的更多相关文章

  1. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  2. Spring Boot 系列教程17-Cache-缓存

    缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...

  3. Spring Boot 系列教程14-动态修改定时任务cron参数

    动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...

  4. Spring Boot 系列教程11-html页面解析-jsoup

    需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...

  5. Spring Boot 系列教程10-freemarker导出word下载

    freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个 ...

  6. Spring Boot 系列教程9-swagger-前后端分离后的标准

    前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...

  7. Spring Boot 系列教程8-EasyUI-edatagrid扩展

    edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...

  8. Spring Boot 系列教程7-EasyUI-datagrid

    jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...

  9. Spring Boot 系列教程19-后台验证-Hibernate Validation

    后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...

随机推荐

  1. 浙江大学Pat 1036 题解

    1036. Boys vs Girls (25) This time you are asked to tell the difference between the lowest grade of ...

  2. Infix to postfix without '(' and ')'

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  3. Struts对输入数据的校验

    当我们在登录或者是注册时需要对用户输入的数据验证,以前都是浏览器传送数据到后台,后台对数据进行校验,如果有错误就带着错误信息转发带登录或者注册页面, struts可以简便的对输入数据进行校验 首先我们 ...

  4. 第二次冲刺spring会议(第一次会议)

    [例会时间]2014/5/4  21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇 内部测试版发布时间5月11日 ...

  5. C - 小Y上学记——认识新同学

    C - 小Y上学记——认识新同学 Time Limit: 4000/2000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) ...

  6. io外挂

    c++里最快的io方式是什么呢? 详见这里. 同时给出一个比较常用的方式,就是用fread.然后自己解析文本,而不是用cin或者scanf,见这里: //fast io test #include & ...

  7. Android:OpenFire 相关API (持续更新)

    基于XMPP协议的聊天服务器.最近会一直更新相关的API. 需要的软件:OpenFire(服务器),Spark(客户端--测试用),Asmack(Jar包) 1.连接服务器的代码 private vo ...

  8. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  9. C#用Zlib压缩或解压缩字节数组

    /// <summary> /// 复制流 /// </summary> /// <param name="input">原始流</par ...

  10. NEU OJ 1644 Median I

    优先级队列 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...