mercredi 22 juillet 2015

Internationalization in Spring MVC, Bryan Hansen example

I'm trying to implement what Bryan Hansen taught in his spring mvc video serie for internationalization; I'm writing the same code as e does but mine does not work properly and I'm getting the following error message :

No message found under code 'student.name' for locale 'en_US'

my startup class is as follow :

public class Startup implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //get the context : 
        WebApplicationContext context = this.getContext();

        //creates a listener : 
        ContextLoaderListener listener = new ContextLoaderListener(context);
        servletContext.addListener(listener);

        //register as servlet : 
        ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
        dispatcherServlet.setLoadOnStartup(1);
        dispatcherServlet.addMapping("/");
    }

    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

        context.register(WebConfig.class);

        return context;
    }
}

and my web config class is as follow :

@Configuration
@ComponentScan(basePackages = "ga.shahab.controllers")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry resource) {
        resource.addResourceHandler("/res/**").addResourceLocations("/resources/");

        resource.addResourceHandler("/app/*.js").addResourceLocations("/resources/app/");
    }

    //  START : Internationalization I18n
    @Bean
    public MessageSource MessageSource() {
        //      ResourceBundleMessageSource messageSource = new     ResourceBundleMessageSource();
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("messages");

        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocaleResolver localResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.ENGLISH);
        return localeResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor changeInterceptor = new LocaleChangeInterceptor();
        changeInterceptor.setParamName("language");
        registry.addInterceptor(changeInterceptor);
    }

    //  END : Internationalization I18n
    @Bean
    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/Views/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

I'm using spring messages like this :

 <spring:message code="student.name"></spring:message>

and the structure of my project is as follow :

1.src/main/java

2.src/main/resources

my internationalization files are in the second folder( resources )

and finally my messages file includes just one line of code :

student.name=Name

but my project does not work for internationalization.

what's wrong with my sample ?!




Aucun commentaire:

Enregistrer un commentaire