In Spring Boot, implementing internationalization (i18n) and localization (l10n) primarily relies on resource bundles to store text messages for various languages. Below, I will provide a detailed explanation of the process and implementation.
1. Creating Resource Files
First, create property files (.properties) for each language supported by your application. These files are typically placed in the src/main/resources directory. For example, if your application needs to support English and Chinese, you can create the following files:
messages.properties(English - default)messages_zh_CN.properties(Simplified Chinese)
These files contain corresponding key-value pairs for text in different languages. For example:
messages.properties
shellwelcome.message=Welcome
messages_zh_CN.properties
shellwelcome.message=欢迎
2. Configuring Spring Boot
In a Spring Boot application, configure the MessageSource bean, which handles internationalized message resolution. This is achieved by adding the following code to a configuration class:
javaimport org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; @Configuration public class InternationalizationConfig { @Bean public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource source = new ResourceBundleMessageSource(); source.setBasenames("messages"); source.setDefaultEncoding("UTF-8"); return source; } }
This configuration sets the base name to messages, causing Spring to automatically locate all property files starting with messages.
3. Using MessageSource
In your controller or service layer, utilize MessageSource to retrieve appropriate internationalized messages. For example:
javaimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest; import java.util.Locale; @Controller public class WelcomeController { @Autowired private MessageSource messageSource; @Autowired private LocaleResolver localeResolver; @GetMapping("/welcome") @ResponseBody public String welcome(HttpServletRequest request) { Locale locale = localeResolver.resolveLocale(request); return messageSource.getMessage("welcome.message", null, locale); } }
Here, LocaleResolver extracts the current Locale from the request, and getMessage retrieves the corresponding message based on this Locale.
4. Setting Locale
Spring Boot enables Locale setting and resolution via the LocaleResolver interface. The AcceptHeaderLocaleResolver is commonly used, which automatically determines the Locale based on the HTTP header Accept-Language. Customize this behavior in a configuration class, for example:
javaimport org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; import java.util.Locale; @Configuration public class LocaleConfig { @Bean public AcceptHeaderLocaleResolver localeResolver() { AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver(); resolver.setDefaultLocale(Locale.ENGLISH); return resolver; } }
With these configurations, a Spring Boot application dynamically displays content in the appropriate language based on user region settings, effectively achieving internationalization and localization.