I18NProvider.java
package info.textgrid.rep.i18n;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import io.quarkus.logging.Log;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
/**
* Utility class to keep track of available and requested language.
*
* Available and default language are configured by lang.available
* and lang.default spring configuration properties. The translations
* are loaded from i18n/Language_xx.properties
*
* This class keeps the compatibility to jsp code accessing an
* i18n hashmap, which originated in porting
* JavaServerFaces el-i18n functionality.
*
* @author Ubbo Veentjer
*/
@ApplicationScoped
public class I18NProvider {
@ConfigProperty(name = "lang.available")
private List<String> langAvailable;
@ConfigProperty(name = "lang.default")
private String langDefault;
private HashMap<String, I18N> i18nMap = new HashMap<String, I18N>();
@PostConstruct
public void postConstruct() {
Log.info("Initializing I18NProvider with available languages: " + langAvailable);
Log.info("Default language: " + langDefault);
for (String lang : langAvailable) {
I18N i18n = new I18N(lang, I18NUtils.getTranslationMap(new Locale(lang)));
i18nMap.put(lang, i18n);
Log.info("Loaded translations for language: " + lang);
}
}
/**
* Get matching i18n object for a locale
*
* @param locale
* @return i18n object for a locale
*/
public I18N getI18N(Locale locale) {
Locale selectedLocale;
if(langAvailable.contains(locale.getLanguage())) {
selectedLocale = locale;
} else {
selectedLocale = new Locale(langDefault);
}
return i18nMap.get(selectedLocale.getLanguage());
}
}