Skip to content

io microsphere i18n util MessageUtils

github-actions[bot] edited this page Jul 9, 2026 · 12 revisions

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

Overview

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.

Example Usage

`// 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"));
}

Declaration

public abstract class MessageUtils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.17-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// 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"));

Method Examples

getLocalizedMessage

// 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"));

resolveMessageCode

String code = MessageUtils.resolveMessageCode("{hello}"); // returns "hello"
  String noCode = MessageUtils.resolveMessageCode("plain"); // returns null

Usage

Maven Dependency

Add 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

import io.microsphere.i18n.util.MessageUtils;

API Reference

Public Methods

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

Method Details

getLocalizedMessage

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

getLocalizedMessage

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"));
}

resolveMessageCode

public static String resolveMessageCode(String messagePattern)

Resolves the message code from a message pattern by extracting the content between {} braces.

Example Usage

`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.

Clone this wiki locally