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

如何写好网站文案站长之家

如何写好网站文案,站长之家,网站域名试用期,css响应式布局国际化实现步骤 Spring Boot 3 提供了强大的国际化支持,使得应用程序可以根据用户的语言和区域偏好适配不同的语言和地区需求。 添加国际化资源文件: 国际化资源文件通常放在 src/main/resources 目录下,并按照不同的语言和地区命名&#xf…

国际化实现步骤

Spring Boot 3 提供了强大的国际化支持,使得应用程序可以根据用户的语言和区域偏好适配不同的语言和地区需求。

  1. 添加国际化资源文件: 国际化资源文件通常放在 src/main/resources 目录下,并按照不同的语言和地区命名,例如:

    • messages.properties:默认语言(如英文)
    • messages_zh_CN.properties:中文简体
    • messages_fr.properties:法语
  2. 配置 MessageSource Bean: 可以通过在 application.propertiesapplication.yml 中进行简单配置来加载国际化资源文件:

    spring:messages:basename: messagesencoding: UTF-8

    或者在配置类中定义 MessageSource Bean:

    @Configuration
    public class MessageConfig {@Beanpublic MessageSource messageSource() {ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:messages");messageSource.setDefaultEncoding("UTF-8");return messageSource;}
    }
  3. 使用国际化资源: 在代码中可以通过 MessageSource 来获取国际化消息。例如,在控制器中根据请求参数确定语言环境并获取对应的消息。

  4. 模板中的国际化: 如果使用 Thymeleaf 作为模板引擎,可以在模板中直接使用国际化消息。需要确保在 application.properties 中启用了国际化支持,并且在模板中使用 #{} 表达式引用消息键。

  5. 自动检测客户端语言: Spring Boot 提供了 LocaleResolver 来自动检测和设置客户端的语言环境。可以使用 AcceptHeaderLocaleResolver 或自定义的 LocaleResolver

  6. 缓存本地语言设置: 若要将本地语言设置缓存,可以在自己的配置类中增加 LocaleChangeInterceptor 拦截器和实现 LocaleResolver 方法。比如使用 CookieLocaleResolver 将语言设置存储在 Cookie 中。

  7. 与 Spring Security 结合: 在使用 Spring Security 时,可以通过在资源文件中添加相应的消息并在 Spring Security 配置中使用这些消息来实现登录页面和错误信息的多语言支持。

示例

配置国际化yaml

spring:messages:encoding: UTF-8basename: i18n/messagesprofiles:active: zh_CN
#-Dspring.profiles.active=en_US

英文

server:port: 8000
spring:jackson:date-format: MM-dd-yyyy

中文

spring:jackson:date-format: yyyy-MM-dd
server:port: 8000

国际化配置

package com.cokerlk.language;import com.cokerlk.language.service.EnUSProductService;
import com.cokerlk.language.service.IProductService;
import com.cokerlk.language.service.ZhCNProductService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;@Configuration
@Data
public class I18NConfiguration {@Value("${spring.profiles.active}")private String locale;@Profile("zh_CN")@Beanpublic IProductService zhCNBussService(){return new ZhCNProductService();}@Profile("en_US")@Beanpublic IProductService enUSBussService(){return new EnUSProductService();}
}

产品接口

package com.cokerlk.language.service;import java.util.Map;public interface IProductService {Map<String,String> getProduct();
}

中文产品

package com.cokerlk.language.service;import com.cokerlk.language.I18NConfiguration;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;@Slf4j
public class ZhCNProductService implements IProductService {@ResourceI18NConfiguration i18NConfiguration;@ResourceMessageSource messageSource;@Overridepublic Map getProduct() {log.info("中文");Map result = new HashMap();result.put("create-date", new Date());result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale())));return result;}
}

英文产品

package com.cokerlk.language.service;import com.cokerlk.language.I18NConfiguration;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;@Slf4j
public class EnUSProductService implements IProductService {@ResourceI18NConfiguration i18NConfiguration;@ResourceMessageSource messageSource;@Overridepublic Map<String,String> getProduct() {log.info("英文");Map result = new HashMap();result.put("create-date", new Date());result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale())));return result;}
}

message配置

#messages.properties
product_name=huawei mate 70
#messages_en_US.properties
product_name=Hua wei mate 70
#messages_zh_CN.properties
product_name=华为mate70

测试结果

http://www.dt0577.cn/news/15609.html

相关文章:

  • 中央纪委网站 举报 要这么做才有效代运营网店公司
  • 做天猫网站价格表五个常用的搜索引擎
  • 网站与平台的区别天津网站seo设计
  • 政府基层网站建设问题ppt今天《新闻联播》回放
  • 中国网站备案信息查询如何做网站
  • 南京做网站找哪家好常州seo收费
  • 网站二级目录网站建设规划书
  • 南通网站建设top广州seo服务
  • 网站优化公司价格如何计算郑州seo团队
  • 深圳家居网站建设公司企业网络营销的模式有哪些
  • 常德做网站公司哪家好制作网站的软件有哪些
  • jsp网站加载慢域名注册价格及续费
  • 德州有做网站的seo关键词优化排名推广
  • 网站分页样式百度数据分析
  • 佛山电子商务网站建设站长工具忘忧草社区
  • 遵义门户网站樱桃bt磁力天堂
  • 内江做网站多少钱怎么让百度搜索靠前
  • 安徽省招标投标信息网官方网站网络营销渠道策略研究
  • 中山做app网站公司哪家好单页面seo搜索引擎优化
  • 网站开发的意义吉安seo网站快速排名
  • 西红门网站建设公司今日热搜前十名
  • 徐州网站建设公司哪个好常用的关键词优化策略有哪些
  • 哪里做网站比较稳定竞价排名采用什么计费方式
  • 手机怎样建立网站百度推广官网登录
  • 外贸移动端网站模板正规seo关键词排名网络公司
  • 百度商桥怎么绑定网站营销型网站更受用户欢迎的原因是
  • 做项目接任务的网站网络营销案例视频
  • 东莞网站推广公司怎么自己建立网站
  • 手机端网站关键词排名天津百度搜索排名优化
  • 好用的在线客服系统抖音seo排名