springcloud微服务国际化

这篇具有很好参考价值的文章主要介绍了springcloud微服务国际化。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、初探

单体应用完成国际化还是比较简单的,可以看下面的示例代码。
引入必要的依赖

<!-- SpringBoot Web -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Validator -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<!-- i18n -->
<dependency>
   <groupId>org.webjars.bower</groupId>
   <artifactId>jquery-i18n-properties</artifactId>
   <version>1.2.7</version>
</dependency>

创建一个拦截器

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LocaleInterceptor extends LocaleChangeInterceptor {
    private static final String LOCALE = "Accept-Language";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String newLocale = request.getHeader(LOCALE);
        if (newLocale != null) {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            if (localeResolver == null) {
                throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
            }
            try {
                localeResolver.setLocale(request, response, parseLocaleValue(newLocale));
            } catch (IllegalArgumentException ignore) {
            }
        }
        return true;
    }
}

创建一个配置类

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

@Configuration
public class LocaleConfig implements WebMvcConfigurer{
    /**
     *	默认解析器 其中locale表示默认语言,当请求中未包含语种信息,则设置默认语种
     *	当前默认为简体中文,zh_CN
     */
    @Bean
    public SessionLocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return localeResolver;
    }

    /**
     *  默认拦截器
     *  拦截请求,获取请求头中包含的语种信息并重新注册语种信息
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LocaleInterceptor());
            }
        };
    }

    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        // 设置消息源
        bean.setValidationMessageSource(resourceBundleMessageSource());
        return bean;
    }

    @Bean
    public MessageSource resourceBundleMessageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
        // 多语言文件地址
        messageSource.addBasenames("i18n/message");
        return messageSource;
    }

    @Bean
    public MethodValidationPostProcessor validationPostProcessor() {
        MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
        processor.setValidator(localValidatorFactoryBean().getValidator());
        return processor;
    }

    @Override
    public Validator getValidator() {
        return localValidatorFactoryBean();
    }
}

然后在resource下创建i18n目录,选中右键 New =>Resource Bundle
springcloud微服务国际化
填入base name,选择Project locales,再Add All,确定即可。
springcloud微服务国际化
打开配置文件,填写对应的中英文数据
springcloud微服务国际化
配置一下application.yml

spring:
  messages:
    basename: i18n.message
    cache-duration: 3600
    encoding: UTF-8

这样基本上就好了,使用也很简单,看下图
springcloud微服务国际化

二、深入

对于微服务来讲,每个模块单独配置国际化还是很繁琐的事情。所以一般是将国际化存入数据库进行统一管理。而本地缓存使用Redis替换,从而更新国际化之后,相应的模块能同步。

先把原来的LocaleConfigLocaleInterceptor抽离到公共服务,同时增加一个自定义MessageSource

import com.xxx.common.core.domain.RpcResult;
import com.xxx.common.redis.service.RedisService;
import com.xxx.system.api.RemoteLocaleMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;

@Slf4j
@Component("messageSource")
public class CustomMessageSource extends AbstractMessageSource {
    public static final String REDIS_LOCALE_MESSAGE_KEY = "i18n_message";

    @Value("${spring.application.name}")
    private String appName;

	/**
	* 这里使用的是dubbo,也可以用open feign
	* 需要引入dubbo的依赖包 spring-cloud-starter-dubbo
	*/
    @DubboReference(version = "1.0.0")
    private RemoteLocaleMessageService i18nMessageMapper;

    @Autowired
    private RedisService redisService;

    @PostConstruct
    public void init() {
        log.info("init i18n message...");
        redisService.deleteObject(REDIS_LOCALE_MESSAGE_KEY + ":" + appName);
        this.reload();
    }

    /**
     * 重新加载消息到该类的Map缓存中
     */
    public Map<String, Map<String, String>> reload() {
        Map<String, Map<String, String>> localeMsgMap = redisService.getCacheMap(REDIS_LOCALE_MESSAGE_KEY + ":" + appName);
        if (localeMsgMap == null || localeMsgMap.isEmpty()) {
            // 加载所有的国际化资源
            localeMsgMap = this.loadAllMessageResources();
            // 缓存到redis
            if (localeMsgMap != null && !localeMsgMap.isEmpty()) {
                redisService.setCacheMap(REDIS_LOCALE_MESSAGE_KEY + ":" + appName, localeMsgMap);
            }
        }
        return localeMsgMap;
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String msg = this.getSourceFromCacheMap(code, locale);
        return new MessageFormat(msg, locale);
    }

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        return this.getSourceFromCacheMap(code, locale);
    }

    /**
     * 加载所有的国际化消息资源
     *
     * @return
     */
    private Map<String, Map<String, String>> loadAllMessageResources() {
        // 从数据库中查询所有的国际化资源
        RpcResult<Map<String, Map<String, String>>> rpcResult = i18nMessageMapper.getAllLocaleMessage(appName);
        return rpcResult.getCode() == 200 ? rpcResult.getData() : null;
    }

    /**
     * 缓存Map中加载国际化资源
     *
     * @param code
     * @param locale
     * @return
     */
    private String getSourceFromCacheMap(String code, Locale locale) {
        // 判断如果没有值则会去重新加载数据
        Map<String, Map<String, String>> localeMsgMap = this.reload();
        String language = ObjectUtils.isEmpty(locale) ? LocaleContextHolder.getLocale().getLanguage() : locale.getLanguage();
        // 获取缓存中对应语言的所有数据项
        Map<String, String> propMap = localeMsgMap.get(language);
        if (!ObjectUtils.isEmpty(propMap) && propMap.containsKey(code)) {
            // 如果对应语言中能匹配到数据项,那么直接返回
            return propMap.get(code);
        }
        // 如果找不到国际化消息,就直接返回code
        return code;
    }
}

并对LocaleConfig进行改造

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

@Configuration
public class LocaleConfig implements WebMvcConfigurer {

     @Autowired
     private CustomMessageSource customMessageSource;

    /**
     *	默认解析器 其中locale表示默认语言,当请求中未包含语种信息,则设置默认语种
     *	当前默认为SIMPLIFIED_CHINESE,zh_CN
     */
    @Bean
    public SessionLocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return localeResolver;
    }

    /**
     *  默认拦截器
     *  拦截请求,获取请求头中包含的语种信息并重新注册语种信息
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LocaleInterceptor());
            }
        };
    }

    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
        bean.setMessageInterpolator(interpolatorFactory.getObject());
        // 设置消息源
        bean.setValidationMessageSource(resourceBundleMessageSource());
        return bean;
    }

    @Bean
    public MessageSource resourceBundleMessageSource() {
        return customMessageSource;
    }

    @Bean
    public MethodValidationPostProcessor validationPostProcessor() {
        MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
        processor.setValidator(localValidatorFactoryBean().getValidator());
        return processor;
    }

    @Override
    public Validator getValidator() {
        return localValidatorFactoryBean();
    }
}

在需要的模块中引入即可。

Dubbo provider的实现

import com.xxx.common.core.domain.RpcResult;
import com.xxx.system.api.RemoteLocaleMessageService;
import com.xxx.system.entity.SysLocaleMessage;
import com.xxx.system.service.ISysLocaleMessageService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService(version = "1.0.0")
public class DubboLocaleMessageService implements RemoteLocaleMessageService {
    @Autowired
    private ISysLocaleMessageService localeMessageService;

    private final String COMMON_CONFIG = "common";

	// 这里不能返回List,因为无法序列化,会导致Dubbo异常,所以使用Map
    @Override
    public RpcResult<Map<String, Map<String, String>>> getAllLocaleMessage(String module) {
        // 每个module对应的配置
        SysLocaleMessage localeMessage = new SysLocaleMessage();
        localeMessage.setModule(module);
        List<SysLocaleMessage> list = localeMessageService.queryByParam(localeMessage);
        if (list == null) {
            list = new ArrayList<>();
        }
        // 公共配置
        localeMessage = new SysLocaleMessage();
        localeMessage.setModule(COMMON_CONFIG);
        list.addAll(localeMessageService.queryByParam(localeMessage));
        if (CollectionUtils.isEmpty(list)) {
            return RpcResult.fail("no data!");
        }

        Map<String, Map<String, String>> localeMsgMap = list.stream().collect(Collectors.groupingBy(
                // 根据国家地区分组
                SysLocaleMessage::getLocale,
                // 收集为Map,key为code,msg为信息
                Collectors.toMap(SysLocaleMessage::getCode, SysLocaleMessage::getMsg)
        ));
        return RpcResult.success(localeMsgMap);
    }
}

将国际化的增删改查功能集成到系统管理,就可以通过数据库进行管理了。
springcloud微服务国际化
对国际化进行增删改后,需要对Redis缓存进行更新

/**
 * 清理Redis所有前缀匹配的缓存
 */
private void clearCache() {
    Collection<String> keys = redisService.keys(CustomMessageSource.REDIS_LOCALE_MESSAGE_KEY + "*");
    keys.stream().forEach(key -> {
        redisService.deleteObject(key);
    });
}

/**
 * 按key清理Redis缓存
 */
private void clearCache(String key) {
    redisService.deleteObject(CustomMessageSource.REDIS_LOCALE_MESSAGE_KEY + ":" + key);
}

之前创建的resouce/i18n目录则可以删除。使用也是和单体应用一样的。
springcloud微服务国际化文章来源地址https://www.toymoban.com/news/detail-405812.html

到了这里,关于springcloud微服务国际化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot——国际化

    优质博文:IT-BLOG-CN 【1】编写国际化配置文件; 【2】使用 ResourceBundleMessageSource 管理国际化资源文件; 【3】在页面使用 ftp:message 取出国际化内容; 【1】创建 i18n 目录,并创建 login.properties 国际化默认配置文件,同时创建 login_zh_CN.properties 系统就会自动识别到是配置国际化

    2024年02月05日
    浏览(46)
  • hyperf 十四 国际化

    官方网址:Hyperf 文件结构:         /storage/languages/en/messages.php         /storage/languages/zh_CH/messages.php 创建文件 /config/autoload/translation.php。 多语言的调用从注入开始,即HyperfTranslationTranslator::__construct(TranslatorLoaderInterface $loader, string $locale)方法。根据配置文件Translato

    2024年02月11日
    浏览(46)
  • SpringBoot复习:(36)国际化

    一、Resources目录下建立一个目录(比如international)来存储资源文件 message.properties 空的,但不能没有 message_zh_CN.properties message_en_us.properties 二、自动配置类MessageSourceAutoConfiguration 常量MESSAGE_SOURCE_BEAN_NAME为messageSource,也就是有这个名字的bean,则自动配置失效。 因为有@Conditional(R

    2024年02月13日
    浏览(39)
  • 微信小程序国际化

    参考文件: 国际化(微信小程序+TS 微信小程序国际化 https://github.com/wechat-miniprogram/miniprogram-i18n 注意:一定要注意项目目录结构,新建文件夹miniprogram,并把前面新建的文件移到这个目录中 在NEW-FAN-CLOCK1 中安装根目录依赖 在NEW-FAN-CLOCK1 / minprogram 中安装依赖 1、初始化仓库: 一

    2023年04月26日
    浏览(31)
  • vue2+element-ui使用vue-i18n进行国际化的多语言/国际化

    注意:vue2.0要用8版本的,使用9版本的会报错 在src目录下,创建新的文件夹,命名为i18n zh.js en.js index.js main.js 使用方式一 效果图 使用方式二 效果图 使用方式三,在 效果图 ` 注意:这种方式存在更新this.$i18n.locale的值时无法自动切换的问题,需要刷新页面才能切换语言。解

    2024年02月07日
    浏览(55)
  • Spring MVC(三) 国际化

    随着全球化的加速发展,Web应用的多语言支持变得越来越重要。对于开发者来说,如何实现应用的国际化成为了一个不可忽视的问题。作为Java Web开发的重要框架,Spring MVC在处理国际化方面有着丰富的功能和灵活的解决方案。本文将探讨Spring MVC的国际化部分内容,并通过自己

    2024年01月18日
    浏览(38)
  • Spring Boot实现国际化

    config controller 在Thymeleaf模板中引用国际化消息:

    2024年01月23日
    浏览(35)
  • 第七十一回:国际化设置

    我们在上一章回中介绍了Card Widget相关的内容,本章回中将介绍 国际化设置 .闲话休提,让我们一起Talk Flutter吧。 我们在这里说的国际化设置是指在App设置相关操作,这样可以让不同国家的用户使用App时呈现不同的语言。总之,就是通过相关的操作,让App支持多个国家的语言

    2024年02月11日
    浏览(48)
  • Android国际化各国语言简写

    2024年02月16日
    浏览(38)
  • 如何优雅的实现前端国际化?

    JavaScript 中每个常见问题都有许多成熟的解决方案。当然,国际化 (i18n) 也不例外,有很多成熟的 JavaScript i18n 库可供选择,下面就来分享一些热门的前端国际化库! i18next 是一个用 JavaScript 编写的全面的国际化框架,提供标准的 i18n 功能,包括复数、上下文、插值、格式等。

    2024年01月23日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包