• Spring Boot 系列教程16-数据国际化


    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

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

  • 相关阅读:
    hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
    HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
    Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
    Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
    Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
    Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
    HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
    axios 简介与安装
    serializer 功能
    APIview 整理
  • 原文地址:https://www.cnblogs.com/je-ge/p/6135676.html
Copyright © 2020-2023  润新知