Geyser icon indicating copy to clipboard operation
Geyser copied to clipboard

Custom Translation API

Open Novampr opened this issue 2 months ago • 0 comments

Allows extensions to register new translation strings (by placing the language files with the extension's resources or via code)

Parameters use Minecraft Java's translation parameter system.

Example (src/main/resources/languages/en_US.properties) :

test.translation=Test Translation!
test.parameters=Geyser is so cool! Don't you agree %0$s?

Send a translated message

// connection is a GeyserConnection
connection.sendTranslatedMessage("test.translation");

Get a translated string

String locale = GeyserApi.api().getDefaultLocale();

String translatedMessage = GeyserApi.api().getTranslationString(locale, "test.translation");
String translatedFallbackMessage = GeyserApi.api().getTranslationStringOrDefault(locale, "test.translation", "Fallback :(");
String translatedParameteredMessage = GeyserApi.api().getTranslationStringOrDefault(locale, "test.parameters", "%0$s, this is a fallback message, your language did not have a translation :(", "Player");

logger().info(translatedMessage);

Registering translations via code

@Subscribe
public void registerTranslations(GeyserDefineCustomTranslationsEvent event) {
    event.register(localeManager -> {
        String locale = localeManager.getLocaleCode();
        if (locale.equals("en_us")) { // This mess is why the property files exist, lots of if branches otherwise
            localeManager.registerTranslationString("test.translation2", "United States");
        }
    });
}

TODO:

  • [x] Allow registering translations
  • [x] Allow translations from property files
  • [x] Add a way to access translations in API
  • [x] Add parameters to translation strings

Novampr avatar Sep 30 '25 19:09 Novampr