springboot+beetl+i18n搭建國(guó)際化網(wǎng)站

直接上項(xiàng)目地址請(qǐng)查看

1.為什么是beetl?

首先本項(xiàng)目為什么選型beetl這個(gè)渲染引擎呢?因?yàn)闆](méi)有那么多為什么,純粹是順手,但是像其它老大哥模板像freemarker,thymeleaf將會(huì)一一推出教程,有人說(shuō)beetl效率高得嚇人,其實(shí)做項(xiàng)目更多時(shí)候除了效率還得看穩(wěn)定度、普及度、易用性。當(dāng)然這些都是題外話,廢話少說(shuō),馬上進(jìn)入主題。

首先這次使用的是springboot2.1.5,springboot1.x和2.x差距甚大,也是前人踩坑,后人乘涼的一個(gè)框架,經(jīng)歷過(guò)1.x的坑,在2.x可以說(shuō)迎來(lái)開發(fā)者爆發(fā)增長(zhǎng)的時(shí)段,隨著設(shè)計(jì)模式選型日漸成熟,一代又一代的迭代后,所以我選擇了2.1.5,也不是說(shuō)2.1.5之前版本變化很大,當(dāng)然中庸選擇也算是比較符合現(xiàn)在的趨勢(shì),你選2.1.0甚至選2.0.5也沒(méi)多大關(guān)系。一句話只要穩(wěn)定就好。

代碼部分

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl-framework-starter</artifactId>
            <version>${beetl.version}</version>
        </dependency>
    </dependencies>

這可以說(shuō)都是最新的版本。指的是beetl。

package org.yick.i18n.config;

import java.util.Locale;

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.EnableWebMvc;
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;

@Configuration
@EnableWebMvc
public class WebConfigure implements WebMvcConfigurer{

    @Override
     public void addInterceptors(InterceptorRegistry registry) {
        //添加國(guó)際化攔截器
        registry.addInterceptor(localeChangeInterceptor());
    }
    
    /**
     * 國(guó)際化切換攔截器
     * 
     * @return 國(guó)際化切換攔截器
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        //自定義攔截參數(shù)名稱
        interceptor.setParamName("lang");
        return interceptor;
    }

    /**
     * 國(guó)際化處理器
     * 
     * @return 國(guó)際化處理器
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        //設(shè)置默認(rèn)區(qū)域
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }
}
package org.yick.i18n.config;


import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.yick.i18n.beetl.BeetlGroupConfiguration;

@Configuration
public class BeetlConfigure {

    @Value("${beetl.templatesPath}")
    String templatesPath;// 模板根目錄 ,比如 "templates"
    
    @Value("${beetl.configPath}")
    String configPath;
    
    @Autowired
    private ResourceLoader resourceLoader;
    
    @Bean(initMethod = "init")
    public BeetlGroupUtilConfiguration beetlConfiguration() {
        BeetlGroupConfiguration beetlGroupUtilConfiguration = new BeetlGroupConfiguration();
        // 獲取SpringBoot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = BeetlConfigure.class.getClassLoader();
        }
        Resource resource = resourceLoader.getResource(configPath);
        beetlGroupUtilConfiguration.setConfigFileResource(resource);
        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, templatesPath);
        beetlGroupUtilConfiguration.setResourceLoader(cploder);
        return beetlGroupUtilConfiguration;

    }

    @Bean
    public BeetlSpringViewResolver beetlViewResolver() {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlConfiguration());
        return beetlSpringViewResolver;
    }
}

詳細(xì)代碼到csdn上看https://download.csdn.net/download/yixiaohui54321/11492303

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容