-
Notifications
You must be signed in to change notification settings - Fork 18
io microsphere i18n util MessageUtils
Type: Class | Module: microsphere-i18n-core | Package: io.microsphere.i18n.util | Since: 1.0.0
Source:
microsphere-i18n-core/src/main/java/io/microsphere/i18n/util/MessageUtils.java
Message Utilities class for resolving internationalized messages from message patterns.
Message patterns use the format {messageCode} where the code is resolved
via the global ServiceMessageSource.
`// Plain text returned as-is
assertEquals("a", MessageUtils.getLocalizedMessage("a"));
// Message code pattern resolved
assertEquals("测试-a", MessageUtils.getLocalizedMessage("{a`"));
// With locale and arguments
assertEquals("Hello,World", MessageUtils.getLocalizedMessage("{hello}", Locale.ENGLISH, "World"));
}
public abstract class MessageUtilsAuthor: Mercy
-
Introduced in:
1.0.0 -
Current Project Version:
0.2.17-SNAPSHOT
This component is tested and compatible with the following Java versions:
| Java Version | Status |
|---|---|
| Java 17 | ✅ Compatible |
| Java 21 | ✅ Compatible |
| Java 25 | ✅ Compatible |
// Plain text returned as-is
assertEquals("a", MessageUtils.getLocalizedMessage("a"));
// Message code pattern resolved
assertEquals("测试-a", MessageUtils.getLocalizedMessage("{a}"));
// With locale and arguments
assertEquals("Hello,World", MessageUtils.getLocalizedMessage("{hello}", Locale.ENGLISH, "World"));// Testing Simplified Chinese
// null
assertEquals(null, MessageUtils.getLocalizedMessage(null));
// If the message argument is "a", the pattern "{" "}" is not included, and the original content is returned
assertEquals("a", MessageUtils.getLocalizedMessage("a"));
// "{a}" is the Message Code template, where "a" is Message Code
assertEquals("测试-a", MessageUtils.getLocalizedMessage("{a}"));
// The same is true for overloaded methods with Message Pattern arguments
assertEquals("hello", MessageUtils.getLocalizedMessage("hello", "World"));
assertEquals("您好,World", MessageUtils.getLocalizedMessage("{hello}", "World"));
// If message code does not exist, return the original content of message
assertEquals("{code-not-found}", MessageUtils.getLocalizedMessage("{code-not-found}"));
assertEquals("code-not-found", MessageUtils.getLocalizedMessage("{microsphere-test.code-not-found}"));
assertEquals("code-not-found", MessageUtils.getLocalizedMessage("{common.code-not-found}"));
// The test of English
assertEquals("hello", MessageUtils.getLocalizedMessage("hello", Locale.ENGLISH, "World"));
assertEquals("Hello,World", MessageUtils.getLocalizedMessage("{hello}", Locale.ENGLISH, "World"));String code = MessageUtils.resolveMessageCode("{hello}"); // returns "hello"
String noCode = MessageUtils.resolveMessageCode("plain"); // returns nullAdd the following dependency to your pom.xml:
<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-i18n-core</artifactId>
<version>${microsphere-i18n.version}</version>
</dependency>Tip: Use the BOM (
microsphere-i18n-dependencies) for consistent version management. See the Getting Started guide.
import io.microsphere.i18n.util.MessageUtils;| Method | Description |
|---|---|
getLocalizedMessage |
Message Code pattern prefix |
getLocalizedMessage |
Get I18n Message |
resolveMessageCode |
Resolves the message code from a message pattern by extracting the content between |
public static String getLocalizedMessage(String messagePattern, Object... args)Message Code pattern prefix / public static final String MESSAGE_PATTERN_PREFIX = LEFT_CURLY_BRACE;
/** Message Code pattern suffix / public static final String MESSAGE_PATTERN_SUFFIX = RIGHT_CURLY_BRACE;
/* Message Source separator / public static final String SOURCE_SEPARATOR = DOT;
/** Get I18n Message
public static String getLocalizedMessage(String messagePattern, Locale locale, Object... args)Get I18n Message
`// Testing Simplified Chinese
// null
assertEquals(null, MessageUtils.getLocalizedMessage(null));
// If the message argument is "a", the pattern "{" "`" is not included, and the original content is returned
assertEquals("a", MessageUtils.getLocalizedMessage("a"));
// "{a}" is the Message Code template, where "a" is Message Code
assertEquals("测试-a", MessageUtils.getLocalizedMessage("{a}"));
// The same is true for overloaded methods with Message Pattern arguments
assertEquals("hello", MessageUtils.getLocalizedMessage("hello", "World"));
assertEquals("您好,World", MessageUtils.getLocalizedMessage("{hello}", "World"));
// If message code does not exist, return the original content of message
assertEquals("{code-not-found}", MessageUtils.getLocalizedMessage("{code-not-found}"));
assertEquals("code-not-found", MessageUtils.getLocalizedMessage("{microsphere-test.code-not-found}"));
assertEquals("code-not-found", MessageUtils.getLocalizedMessage("{common.code-not-found}"));
// The test of English
assertEquals("hello", MessageUtils.getLocalizedMessage("hello", Locale.ENGLISH, "World"));
assertEquals("Hello,World", MessageUtils.getLocalizedMessage("{hello}", Locale.ENGLISH, "World"));
}
public static String resolveMessageCode(String messagePattern)Resolves the message code from a message pattern by extracting the content between
{} braces.
`String code = MessageUtils.resolveMessageCode("{hello`"); // returns "hello"
String noCode = MessageUtils.resolveMessageCode("plain"); // returns null
}
This documentation was auto-generated from the source code of microsphere-i18n.
i18n-core
- AbstractResourceServiceMessageSource
- AbstractServiceMessageSource
- CompositeServiceMessageSource
- DefaultServiceMessageSource
- EmptyServiceMessageSource
- I18nUtils
- MessageUtils
- PropertiesResourceServiceMessageSource
- ReloadableResourceServiceMessageSource
- ResourceServiceMessageSource
- ServiceMessageException
- ServiceMessageSource
i18n-openfeign
i18n-spring
- AcceptHeaderLocaleResolverBeanPostProcessor
- DelegatingServiceMessageSource
- EnableI18n
- I18nApplicationListener
- I18nBeanUtils
- I18nConstants
- I18nImportBeanDefinitionRegistrar
- I18nLocalValidatorFactoryBeanPostProcessor
- LocaleUtils
- MessageSourceAdapter
- PropertySourcesServiceMessageSource
- ResourceServiceMessageSourceChangedEvent
- ServiceMessageSourceBeanLifecyclePostProcessor
- ServiceMessageSourceFactoryBean
i18n-spring-boot
i18n-spring-cloud
i18n-spring-cloud-server