springboot+beetl+i18n搭建國際化網站

直接上項目地址請查看

1.爲什麼是beetl?

首先本項目爲什麼選型beetl這個渲染引擎呢?因爲沒有那麼多爲什麼,純粹是順手,但是像其它老大哥模板像freemarker,thymeleaf將會一一推出教程,有人說beetl效率高得嚇人,其實做項目更多時候除了效率還得看穩定度、普及度、易用性。當然這些都是題外話,廢話少說,馬上進入主題。

首先這次使用的是springboot2.1.5,springboot1.x和2.x差距甚大,也是前人踩坑,後人乘涼的一個框架,經歷過1.x的坑,在2.x可以說迎來開發者爆發增長的時段,隨着設計模式選型日漸成熟,一代又一代的迭代後,所以我選擇了2.1.5,也不是說2.1.5之前版本變化很大,當然中庸選擇也算是比較符合現在的趨勢,你選2.1.0甚至選2.0.5也沒多大關係。一句話只要穩定就好。

代碼部分

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>

這可以說都是最新的版本。指的是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) {
        //添加國際化攔截器
        registry.addInterceptor(localeChangeInterceptor());
    }
    
    /**
     * 國際化切換攔截器
     * 
     * @return 國際化切換攔截器
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        //自定義攔截參數名稱
        interceptor.setParamName("lang");
        return interceptor;
    }

    /**
     * 國際化處理器
     * 
     * @return 國際化處理器
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        //設置默認區域
        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;
    }
}

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

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