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

一级a做爰片免费网站今日最新足球推荐

一级a做爰片免费网站,今日最新足球推荐,公司的网站续费,网站设计团队名称前言 公司在做国际化项目时需要匹配多语言环境,通过spring实现i18n国际化方便快捷 项目结构 src/ ├── main/ │ ├── java/ │ │ └── com/example/i18ndemo/ │ │ ├── config/ # 配置类 │ │ ├── controller/ # …

前言

公司在做国际化项目时需要匹配多语言环境,通过spring实现i18n国际化方便快捷

项目结构

src/
├── main/
│   ├── java/
│   │   └── com/example/i18ndemo/
│   │       ├── config/       # 配置类
│   │       ├── controller/   # 控制器
│   │       ├── service/      # 服务层
│   │       └── I18nDemoApplication.java # 启动类
│   └── resources/
│       ├── static/           # 静态资源
│       ├── templates/        # 模板文件
│       ├── messages.properties      # 默认语言文件
│       ├── messages_en.properties   # 英文
│       ├── messages_zh_CN.properties # 中文
│       └── application.properties   # 应用配置
└── test/                     # 测试代码

创建springboot项目

创建一个springboot项目。项目名称是I18nDemo

添加maven依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.5</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Thymeleaf (可选,用于前端模板) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- 验证支持 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

初始化语言文件

在 resources 目录下创建以下文件:

messages.properties (默认)

welcome.message=Welcome
user.greeting=Hello, {0}
login.title=Login

messages_zh_CN.properties

welcome.message=欢迎
user.greeting=你好, {0}
login.title=登录

messages_ja.properties

welcome.message=ようこそ
user.greeting=こんにちは, {0}
login.title=ログイン

配置国际化支持

应用配置 (application.properties)

# 国际化配置
spring.messages.basename=messages
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=false# Thymeleaf 配置 (如果使用)
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8

创建配置类

普通的配置类支持通过url参数切换语言

package com.example.i18ndemo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;import java.util.Locale;@Configuration
public class I18nConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver() {SessionLocaleResolver slr = new SessionLocaleResolver();slr.setDefaultLocale(Locale.ENGLISH); // 设置默认语言return slr;}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor lci = new LocaleChangeInterceptor();lci.setParamName("lang"); // 通过URL参数切换语言return lci;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}
}

优化过的配置类,支持通过url和head传递国际化参数,推荐使用head方便快捷

此种方式传递中文的时候需要用zh-CN

  • Spring的 LocaleContextHolder 能自动处理 zh_CN 和 zh-CN

  • 但底层 Locale.forLanguageTag() 仍然要求标准格式

  • 如果前端传的是zh_CN,则后端可以直接替换下

    localeStr = localeStr.replace('_', '-');

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;@Configuration
public class I18nConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver() {SmartLocaleResolver resolver = new SmartLocaleResolver();resolver.setDefaultLocale(Locale.ENGLISH);return resolver;}public class SmartLocaleResolver implements LocaleResolver {private Locale defaultLocale = Locale.ENGLISH;@Overridepublic Locale resolveLocale(HttpServletRequest request) {// 1. 优先检查 URL 参数(如 ?lang=zh_CN)String langParam = request.getParameter("lang");if (langParam != null && !langParam.isEmpty()) {return Locale.forLanguageTag(langParam);}// 2. 如果没有 URL 参数,检查 Accept-Language 头String acceptLanguage = request.getHeader("Accept-Language");if (acceptLanguage != null && !acceptLanguage.isEmpty()) {return request.getLocale();}// 3. 返回默认语言return defaultLocale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {// 如果需要动态修改语言,可以在这里实现}public void setDefaultLocale(Locale defaultLocale) {this.defaultLocale = defaultLocale;}}
}

创建测试控制器

package com.example.i18ndemo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;import java.util.Locale;@Controller
public class HomeController {private final MessageSource messageSource;@Autowiredpublic HomeController(MessageSource messageSource) {this.messageSource = messageSource;}@GetMapping("/")public String home(Model model) {// 通过代码获取消息String welcomeMsg = messageSource.getMessage("welcome.message", null, LocaleContextHolder.getLocale());model.addAttribute("welcomeMsg", welcomeMsg);return "home";}
}

创建 Thymeleaf 模板

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title th:text="#{login.title}">Login</title>
</head>
<body><h1 th:text="${welcomeMsg}">Welcome</h1><!-- 直接使用消息 --><p th:text="#{user.greeting('John')}">Hello, User</p><!-- 语言切换链接 --><div><a href="?lang=en">English</a> | <a href="?lang=zh-CN">中文</a> | <a href="?lang=ja">日本語</a></div>
</body>
</html>

运行项目

  1. 启动主类 I18nDemoApplication

  2. 访问 http://localhost:8080

  3. 点击不同语言链接测试切换功能

扩展建议

  1. 数据库存储消息:对于大型项目,可以实现 MessageSource 接口从数据库加载消息

  2. 前端框架集成:如果使用 Vue/React,可以创建 API 端点返回语言包

  3. 自动化测试:编写测试验证所有语言包是否完整

  4. 本地化日期/数字:使用 Spring 的 Formatter 体系实现

这样你就完成了一个基本的支持国际化的 Spring Boot 项目!

http://www.dtcms.com/wzjs/67961.html

相关文章:

  • 网站策划报告书怎么做百度引流怎么推广
  • 在网上做网站百度客服在线咨询人工服务
  • wordpress调用图标搜索引擎seo如何赚钱
  • 惠州个人做网站联系人哈尔滨网站优化流程
  • 广州网站建设优化电商网站开发平台有哪些
  • 建设网站的目标客户群武汉新闻最新消息
  • 坊网站建设宁波seo免费优化软件
  • 嘉兴外贸网站建设环球贸易网
  • 做网站还 淘宝网络运营seo是什么
  • 设计网站behance市场调研方案范文
  • wordpress专业站内优化主要从哪些方面进行
  • 什么叫网站定位天津网站排名提升多少钱
  • 动态网站开发课程设计怎样在百度做广告宣传
  • b2c网站技术架构怎么开展网络营销推广
  • 兰州做网站公司有哪些网络营销比较成功的企业
  • 建设彩票网站需要多少投资营销策划方案公司
  • 网站的百度地图怎么做的百度手机助手app安卓版官方下载
  • wordpress删除重装谷歌网站优化推广
  • 做网站建设的网站郴州网站seo
  • 郑州哪有做网站的seo网页优化培训
  • 韩国购物网站有哪些百度推广合作
  • 免费ppt模板下载有哪些天津百度快速优化排名
  • 工控主机做网站服务器网络营销的基本流程
  • 电影网站建设java企业网络推广技巧
  • 张家口网站建设价格搜索排名影响因素
  • 中国高定十大品牌成都seo优化
  • 一级造价工程师吧品牌seo推广
  • 佛山专注网站制作细节应用商店aso
  • 乐清定制网站建设电话网址域名注册信息查询
  • 网页传奇游戏卡bug无锡seo培训