Spring,Moustache Starter - 翻译

时间:2016-08-25 06:38:57

标签: spring spring-mvc spring-boot mustache

我尝试配置我的项目以支持国际化。

我根据最近使用的JSP配置编写了简单的配置。

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.mustache.java.LocalizationMessageInterceptor;

import javax.inject.Inject;
import java.util.Locale;

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {

    @Inject
    private Environment environment;


    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/"
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }

    /* Internationalization beans */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("language");

        return localeChangeInterceptor;
    }

    @Bean(name = "localeResolver")
    public LocaleResolver getLocaleResolver(){
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        localeResolver.setDefaultLocale(new Locale(environment.getProperty("system.default.language")));

        return localeResolver;
    }

    @Bean
    public MessageSource messageSource() {
        final ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
        ret.setBasename("classpath:translations");
        ret.setDefaultEncoding("UTF-8");
        ret.setUseCodeAsDefaultMessage(true);

        return ret;
    }

    @Bean
    @Inject
    public LocalizationMessageInterceptor getLocalizationMessageInterceptor(MessageSource messageSource, LocaleResolver localeResolver) {
        LocalizationMessageInterceptor lmi = new LocalizationMessageInterceptor();
        lmi.setLocaleResolver(localeResolver);
        lmi.setMessageSource(messageSource);
        return lmi;
    }

}

在html文件中,我无法访问翻译值。

${app.name}

未解决,

{{app.name}}

导致异常

No method or field with name 'app'

翻译文件放在资源目录下,默认语言设置为:

system.default.language=en

修改 的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.gateway</groupId>
    <artifactId>gateway-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Gateway Application</name>
    <description>Gateway Application Project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <spring-webmvc-pac4j.version>1.1.1</spring-webmvc-pac4j.version>
        <pac4j.version>1.9.1</pac4j.version>
        <javax.inject.version>1</javax.inject.version>
        <apache-commons.version>3.4</apache-commons.version>
        <spring-devtools.version>1.4.0.RELEASE</spring-devtools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.sps.mustache</groupId>
            <artifactId>mustache-spring-view</artifactId>
            <version>1.3</version>
        </dependency>
        <!-- PAC4J -->
        <dependency>
            <groupId>org.pac4j</groupId>
            <artifactId>pac4j-cas</artifactId>
            <version>${pac4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.pac4j</groupId>
            <artifactId>pac4j-http</artifactId>
            <version>${pac4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.pac4j</groupId>
            <artifactId>spring-webmvc-pac4j</artifactId>
            <version>${spring-webmvc-pac4j.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>${javax.inject.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${apache-commons.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Spring Boot 1.4.0.RELEASE

1 个答案:

答案 0 :(得分:0)

我做了一些不必要的配置。简要总结为4分。

  1. <强> AppConfig.java

    @Configuration
    public class AppConfig extends WebMvcConfigurerAdapter {
    
        @Inject
        private Environment environment;
    
        @Inject
        private MessageSource messageSource;
    
        /* Internationalization beans */
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeChangeInterceptor());
            registry.addInterceptor(getLocalizationMessageInterceptor());
        }
    
        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor(){
            LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
            localeChangeInterceptor.setParamName("language");
    
            return localeChangeInterceptor;
        }
    
    
        @Bean(name = "localeResolver")
        public LocaleResolver getLocaleResolver(){
            CookieLocaleResolver localeResolver = new CookieLocaleResolver();
            localeResolver.setDefaultLocale(new Locale(environment.getProperty("system.default.language")));
    
            return localeResolver;
        }
    
        @Bean
        public LocalizationMessageInterceptor getLocalizationMessageInterceptor() {
            LocalizationMessageInterceptor lmi = new LocalizationMessageInterceptor();
            lmi.setLocaleResolver(getLocaleResolver());
            lmi.setMessageSource(messageSource);
            return lmi;
    
        }
    }
    
  2. 作为@M。 Deinum说MessageSource和资源配置由初学者完成,所以我们不需要它。

    1. <强>的pom.xml

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-mustache</artifactId>
          </dependency>
          <dependency>
              <groupId>com.github.sps.mustache</groupId>
              <artifactId>mustache-spring-view</artifactId>
              <version>1.3</version>
          </dependency>
      
      由于org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor。

    2. ,因此需要使用mustache-spring-view。
    3. <强> message.properties 对于默认的locale(en)文件,message.properties是必需的。 对于不同的区域设置文件,使用了message_LOCALE.properties。

    4. 这些文件应放在资源/.​​

      1. HTML文件 可以使用带符号

        在HTML文件中访问消息值
           {{#i18n}}message-key{{/i18n}}