如何登陆工商局网站做变更百度seo优化包含哪几项
一、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。