springboot,頁面配置國際化語言顯示

創建國際化語言包

SpringBoot自動配置好了管理國際化資源文件的組件:

MessageSourceAutoConfiguration
@Bean
    public MessageSource messageSource(MessageSourceProperties properties) {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (StringUtils.hasText(properties.getBasename())) {
            
        //設置國際化資源文件的基礎名(去掉語言國家代碼的:login_zh_CN.properties,zh_CN就是國家代碼,英語爲:en_US)
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
        }

        if (properties.getEncoding() != null) {
            messageSource.setDefaultEncoding(properties.getEncoding().name());
        }

        messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
        Duration cacheDuration = properties.getCacheDuration();
        if (cacheDuration != null) {
            messageSource.setCacheMillis(cacheDuration.toMillis());
        }

        messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
        messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
        return messageSource;
    }

因爲語言包放在了i18n的package下,所以需要修改yml或properties配置文件,springboot默認是messages下

頁面這裏用了layui的滑塊驗證,需要獲取值,需要用[[#{}]]來獲取值

最後效果

中文效果下:

英文效果下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章