Skip to content

API Usage

BloodyTim3 edited this page Dec 16, 2024 · 6 revisions

API

Set the provider details from another plugin

Example:

public class MyPlugin extends PluginBase {

    @Override
    public void onLoad() {
        FuturePlots.getInstance().getProvider().setDataClient(new DataClient(
                ClientType.MONGODB, new MongoDBDetails(
                "uri",
                "database"
        )));
    }
}

Custom language handler

Example:

public class MyPlugin extends PluginBase {

    @Override
    public void onLoad() {
        LanguageManager.setHandler(new CustomLanguageHandler());
    }
}
public class CustomLanguageHandler implements LanguageProvider {
    private final Map<String, Map<String, String>> translations = new HashMap<>();

    @Override
    public void init() {
        Map<String, String> enTranslations = new HashMap<>();
        enTranslations.put("greeting", "Hello, %s!");
        
        Map<String, String> deTranslations = new HashMap<>();
        deTranslations.put("greeting", "Hallo, %s!");

        translations.put("en", enTranslations);
        translations.put("de", deTranslations);
    }

    @Override
    public String message(String locale, String key, Object... replacements) {
        // Standard-Locale, falls nicht gefunden
        Map<String, String> localeTranslations = translations.getOrDefault(locale, translations.get("en"));

        // Übersetzung anhand des Schlüssels abrufen
        String template = localeTranslations.getOrDefault(key, key);

        // Platzhalter durch Ersatzwerte ersetzen
        return String.format(template, replacements);
    }

    public static void main(String[] args) {
        CustomLanguageHandler handler = new CustomLanguageHandler();
        handler.init();

        // Test-Ausgaben
        System.out.println(handler.message("en", "greeting", "John")); // Ausgabe: Hello, John!
        System.out.println(handler.message("de", "greeting", "Hans")); // Ausgabe: Hallo, Hans!
        System.out.println(handler.message("fr", "greeting", "Jean")); // Ausgabe: Hello, Jean! (Fällt auf Standard zurück)
    }
}
Clone this wiki locally