首页 > 其他分享 >Spring40 - Spring6 i18n

Spring40 - Spring6 i18n

时间:2023-02-09 16:25:33浏览次数:48  
标签:www 国际化 com Spring40 context atguigu i18n import Spring6

Spring6国际化

MessageSource接口

spring中国际化是通过MessageSource这个接口来支持的

常见实现类

ResourceBundleMessageSource

这个是基于Java的ResourceBundle基础类实现,允许仅通过资源名加载国际化资源

ReloadableResourceBundleMessageSource

这个功能和第一个类的功能类似,多了定时刷新功能,允许在不重启系统的情况下,更新资源的信息

StaticMessageSource

它允许通过编程的方式提供国际化信息,一会我们可以通过这个来实现db中存储国际化信息的功能。

使用Spring6国际化

第一步 创建资源文件

国际化文件命名格式:基本名称 _ 语言 _ 国家.properties

{0},{1}这样内容,就是动态参数

(1)创建atguigu_en_US.properties

www.atguigu.com=welcome {0},时间:{1}

(2)创建atguigu_zh_CN.properties

www.atguigu.com=欢迎 {0},时间:{1}

第二步 创建spring配置文件,配置MessageSource

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>atguigu</value>
            </list>
        </property>
        <property name="defaultEncoding">
            <value>utf-8</value>
        </property>
    </bean>
</beans>

第三步 创建测试类

package com.atguigu.spring6.javai18n;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
import java.util.Locale;

public class Demo2 {

    public static void main(String[] args) {
        
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        
        //传递动态参数,使用数组形式对应{0} {1}顺序
        Object[] objs = new Object[]{"atguigu",new Date().toString()};

        //www.atguigu.com为资源文件的key值,
        //objs为资源文件value值所需要的参数,Local.CHINA为国际化为语言
        String str=context.getMessage("www.atguigu.com", objs, Locale.CHINA);
        System.out.println(str);
    }
}

第四步 解决乱码问题

IDEA -> Setting -> file encoding 设置编码格式为 utf-8

标签:www,国际化,com,Spring40,context,atguigu,i18n,import,Spring6
From: https://www.cnblogs.com/Ashen-/p/17105664.html

相关文章

  • vue项目国际化i18n的实现及思考
    国际化是什么?国际化对应的英文单词为Internationalization,又称“i18n”:i为单词的【第一个】字母18为【i和n之间】字母的个数n为单词的【最后一个】字母如果......
  • Spring6 DI 依赖注入--Bean属性赋值
    Spring6基于XML实现Bean管理(属性赋值)IOC和DI有什么区别:DI是IOC中的具体实现,DI表示依赖注入或注入属性,注入属性要在创建对象的基础之上完成依赖注入方法bean属性赋值方......
  • 明解Spring6(一)------邂逅
    一、Spring简介1、Spring介绍Spring是一款主流的JavaEE轻量级开源框架,Spring由“Spring之父”RodJohnson提出并创立,其目的是用于简化Java企业级应用的开发难......
  • Spring6
    Spring6Spring项目的创建打开IDEA,新建一个maven项目在maven项目中引入spring的仓库和依赖<repositories><repository><id>repository.spri......
  • Vue.js框架:引入Element-ui和国际化i18n组件冲突及兼容问题处理
    一、问题如下要使用element-ui的表格时,在main.js中同时全局引入了element和i18n时,页面会报错如下:Errorinrender: TypeError:Cannotreadpropertiesofund......
  • 优维低代码:I18n 国际化
    优维低代码技术专栏,是一个全新的、技术为主的专栏,由优维技术委员会成员执笔,基于优维7年低代码技术研发及运维成果,主要介绍低代码相关的技术原理及架构逻辑,目的是给广大运维......
  • MasaFramework -- i18n (国际化)
    概念作为一个普通开发者,我们负责的项目的使用群体大多数是本国的人民,但不可避免的也有一些做外贸的业务或者给外企做的项目,这个时候就要求我们的项目有服务全球客户......
  • Vite + Vue3导入 vue-i18n 插件
    使用Vite+Vue3导入vue-i18n插件Step1下载vue-i18n插件npminstallvue-i18nStep2新建local文件夹,创建index.jsindex.jsimport{createI18n}from"v......
  • 在项目中定义 i18n 结构化对象的简单思路
    简介本文介绍一种在项目定义i18n文本结构的简单思路,以及定义一些简单的全局函数,用于获取i18n文本。文件结构src├──i18n└──index.js├──zhCN├──i......
  • vue i18n _ctx.$t is not a function
     一、问题Uncaught(inpromise)TypeError:_ctx.$tisnotafunctionatSelect.vue:51:95atrenderFnWithContext(runtime-core.esm-bundler.js:852:21)......