Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MessageSource

Provides methods for retrieving messages.

Step 1: Replace the MessageSource class

To use this class, the MessageSource on a server must implement the interface org.wcardinal.util.message.ExposableMessageSource. There are the two out-of-the-box implementations of this interface: ExposableReloadableResourceBundleMessageSource and ExposableResourceBundleMessageSource in the package org.wcardinal.util.message.

// Java
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.wcardinal.util.message.ExposableReloadableResourceBundleMessageSource;

@Configuration
public class MessageSourceConfig {
    @Bean
    public MessageSource messageSource(){
        final ExposableReloadableResourceBundleMessageSource result
            = new ExposableReloadableResourceBundleMessageSource();
        result.setBasename("classpath:/i18n/messages");
        result.setDefaultEncoding("UTF-8");
        return result;
    }
}

Step 2: Embed a script

And then embed a message script obtained by ExposableMessages#getScript as follows:

// Java
import org.wcardinal.util.message.ExposableMessages;

@Controller
public class MessageMvcController {
    @Autowired
    ExposableMessages messages;

    @RequestMapping( "/" )
    ModelAndView en( final HttpServletRequest req ) {
        final ModelAndView mav = new ModelAndView();
        mav.addObject( "messageScript", messages.getScript( Locale.ENGLISH ) );
        mav.setViewName("sample-view");
        return mav;
    }
}

<!-- HTML Template (sample-view.html) -->
<!-- Must be placed after the wcardinal script -->
<script th:utext="${ messageScript }"></script>

Please note that the message script must be loaded after the wcardinal.min.js.

Now, translated messages can be obtained by calling MessageSource#get with message IDs.

// JavaScript
messageSource.get( "foo.bar.1" );

For parameterized messages, pass parameters to the get method.

// JavaScript
messageSource.get( "foo.bar.1", 123, "William" );

Hierarchy

  • MessageSource

Index

Properties

Methods

Properties

source

source: PlainObject<string>

Static Private INSTANCE

INSTANCE: MessageSource | null = null

Methods

get

  • get(id: string, ...parameter0: unknown[]): string

Static getInstance

Generated using TypeDoc