All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.annimon.tgbotsmodule.services.ResourceBundleLocalizationService Maven / Gradle / Ivy

The newest version!
package com.annimon.tgbotsmodule.services;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class ResourceBundleLocalizationService implements LocalizationService {

    private final String resourceName;
    private final Map bundles;
    private final ResourceBundle defaultBundle;

    public ResourceBundleLocalizationService(String resourceName) {
        this.resourceName = resourceName;
        bundles = new HashMap<>();
        defaultBundle = ResourceBundle.getBundle(resourceName, Locale.ROOT);
        bundles.put("en", defaultBundle);
    }

    @Override
    public String getString(String key, String language) {
        final var bundle = lazyLoadLanguage(language);
        return bundle.getString(key);
    }

    private ResourceBundle lazyLoadLanguage(String language) {
        if (bundles.containsKey(language)) {
            return bundles.get(language);
        }
        try {
            final var bundle = ResourceBundle.getBundle(resourceName, new Locale(language));
            bundles.put(language, bundle);
            return bundle;
        } catch (MissingResourceException mre) {
            return defaultBundle;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy