dawdler-i18n-core
Module Introduction
The i18n core module, which provides annotations and dynamic proxy-based injection of internationalized resources.
1. Adding the Dependency to pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-i18n-core</artifactId>
2. properties File Description
Internationalization resource configuration files must be placed on the classpath. The file naming convention is base-name.properties or base-name_language-code.properties.
For example:
- messages.properties (default language)
- messages_zh.properties (Chinese)
- messages_en.properties (English)
- messages_zh_CN.properties (Simplified Chinese)
- messages_en_US.properties (American English)
Example configuration file content (messages.properties):
# Welcome message
welcome.message=欢迎使用系统
user.login.success=用户登录成功
error.code.404=页面未找到
Example configuration file content (messages_en.properties):
# Welcome message
welcome.message=Welcome to the system
user.login.success=User login successful
error.code.404=Page not found
3. I18nInjector Annotation
Used to inject I18nOperator.
The value of the I18nInjector annotation takes baseName as the base name of the resource file (such as messages).
Parameter description:
- value: the base name of the resource file, excluding the language code and the
.propertiessuffix
For details, see:
dawdler-server-plug-i18n provides injection support on the dawdler-server side.
dawdler-client-plug-i18n provides injection support on the web side.
4. I18nOperator Interface
String get(String key); // Get the resource value for the current locale
String get(String key, Locale locale); // Get the resource value for the specified locale
The get overload without a Locale parameter uses the locale stored in TLS_LOCALE; if none is set, it uses Locale.getDefault().
5. Usage Examples
5.1 Basic Usage
The following example injects I18nOperator via @I18nInjector into a dawdler-managed component (such as a class annotated with @Service or @Controller).
@Service("my-service")
public class MyService {
@I18nInjector("messages") // messages is the base name of the resource file
private I18nOperator i18nOperator;
public void doSomething() {
// Get the resource value for the current locale
String message = i18nOperator.get("welcome.message");
// Get the resource value for the specified locale
String englishMessage = i18nOperator.get("welcome.message", Locale.ENGLISH);
}
}
5.2 Usage in a Web Environment
In a web environment, I18nInterceptor parses the locale from the request and puts it into TLS_LOCALE, so business code can simply call get.
@Controller
public class UserController {
@I18nInjector("messages") // messages is the base name of the resource file
private I18nOperator i18nOperator;
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome() {
// The locale is taken first from the URL parameter locale, then from the Accept-Language request header
String welcomeMessage = i18nOperator.get("welcome.message");
return welcomeMessage;
}
}
5.3 Usage in a Server Environment
In a microservice architecture, the web side passes the locale to the server side via RpcContext through dawdler-client-i18n; the server-side I18nFilter puts it into TLS_LOCALE, and business code can simply call get.