Implementing Multilingual String Formatting in Java

Implementation Oevrview

This guide demonstrates how to build a utility class that provides formatted strings in multiple languages using Java.

Project Structure

| Component | Purpose | |-----------|--------|| | LocalizedFormatter | Main utility class | | getGreeting(Locale) | Returns localized greeting based on locale parameter |

Code Implementation

Formatter Class Definition

public class LocalizedFormatter {
    
    public String getGreeting(Locale locale) {
        if (locale == null) {
            return "Hello, World!";
        }
        
        switch (locale.getLanguage()) {
            case "zh":
                return "你好,世界!";
            case "fr":
                return "Bonjour le monde !";
            case "en":
            default:
                return "Hello, World!";
        }
    }
}

Alternative Approach: Separate Methods

public class MultilingualSupport {
    
    public String englishMessage() {
        return "Hello, World!";
    }
    
    public String chineseMessage() {
        return "你好,世界!";
    }
    
    public String frenchMessage() {
        return "Bonjour le monde !";
    }
}

Usage Example

public class Main {
    public static void main(String[] args) {
        MultilingualSupport support = new MultilingualSupport();
        
        System.out.println(support.englishMessage());
        System.out.println(support.chineseMessage());
        System.out.println(support.frenchMessage());
    }
}

Output

Hello, World!
你好,世界!
Bonjour le monde !

Best Practices

For production applications, externalize strings to resource bundle files:

resources/
├── messages_en.properties
├── messages_zh.properties
└── messages_fr.properties
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.CHINA);
String greeting = bundle.getString("greeting");

This approach separates content from code, enabling easier maintenance and translation updates without recompilation.

Tags: java i18n Localization multilingual String Formatting

Posted on Sat, 16 May 2026 16:14:52 +0000 by beanfair