import="org.springframework.context.i18n.LocaleContextHolder,java.util.Locale"

本文介绍了一种基于Spring框架的网站国际化实现方案,通过检测用户的地理位置自动切换显示语言。主要针对中文(简体、繁体)、英文用户界面的切换进行了详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import="org.springframework.context.i18n.LocaleContextHolder,java.util.Locale"  


<%
Locale locale = LocaleContextHolder.getLocale();
 
  if("CN".equals(locale.getCountry())){
%>
<a href="global?langType=en" ><div class="img"></div></a>
<a  href="global?langType=zh_tw"><div class="img tw"></div></a>
<a  class="active" href="global?langType=zh"  ><div class="img cn"></div></a>
<%}else if("US".equals(locale.getCountry())) {%>
<a class="active" href="global?langType=en"><div class="img"></div></a>
<a href="global?langType=zh_tw"><div class="img tw"></div></a>
<a href="global?langType=zh"><div class="img cn"></div></a>
<%}
else{
%>
<a href="global?langType=en"><div class="img"></div></a>
   <a class="active" href="global?langType=zh_tw"><div class="img tw"></div></a>
   <a href="global?langType=zh"><div class="img cn"></div></a>
<%}
%>
  

package com.sie.common.config; import jakarta.servlet.*; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.apache.commons.lang3.LocaleUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.util.WebUtils; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Locale; @Component public class InitializationLocalFilter implements Filter { // 支持的语言列表(扩展繁体中文和葡萄牙文) private static final List<String> SUPPORTED_LOCALES = Arrays.asList( "zh_CN", "zh_TW", // 中文(简体/繁体) "en_US", // 英文 "pt_PT", "pt_BR" // 葡萄牙文(葡萄牙/巴西) ); private static final String DEFAULT_LOCALE = "zh_CN"; // 默认中文简体 private static final String LOCALE_PARAMETER = "locale"; // 请求参数名 private static final String LOCALE_COOKIE_NAME = "locale_request_change_attr"; // Cookie名 private static final int COOKIE_EXPIRE = 1209600; // Cookie有效期(14天) private static final Logger LOGGER = LoggerFactory.getLogger(InitializationLocalFilter.class); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; // 1. 解析语言:优先取请求参数,其次取Cookie,最后用默认 Locale currentLocale = resolveLocale(httpRequest); // 2. 设置当前线程的语言环境 LocaleContextHolder.setLocale(currentLocale, true); // 3. 将语言存入Cookie(下次请求可复用) addCookie(httpResponse, LOCALE_COOKIE_NAME, currentLocale.toString(), COOKIE_EXPIRE); // 4. 继续执行请求链 chain.doFilter(request, response); // 5. 清理线程变量 LocaleContextHolder.resetLocaleContext(); } /** * 解析请求中的语言(参数 > Cookie > 默认) */ private Locale resolveLocale(HttpServletRequest request) { // 从请求参数获取(如 ?locale=zh_TW 或 ?locale=pt_BR) String paramLocale = request.getParameter(LOCALE_PARAMETER); if (paramLocale != null && SUPPORTED_LOCALES.contains(paramLocale)) { return LocaleUtils.toLocale(paramLocale); } // 从Cookie获取 Cookie localeCookie = WebUtils.getCookie(request, LOCALE_COOKIE_NAME); if (localeCookie != null && SUPPORTED_LOCALES.contains(localeCookie.getValue())) { try { return LocaleUtils.toLocale(localeCookie.getValue()); } catch (IllegalArgumentException e) { LOGGER.warn("Cookie中的语言格式无效: {}", localeCookie.getValue()); } } // 默认返回中文简体 return LocaleUtils.toLocale(DEFAULT_LOCALE); } /** * 添加语言Cookie */ private void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); // 全站有效 cookie.setMaxAge(maxAge); cookie.setHttpOnly(true); // 防止JS读取,增强安全性 response.addCookie(cookie); } }根据这个文件,优化一下你写的代码
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值