com.softicar.platform.common.core.i18n.I18nKeyDisplayString Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.core.i18n;
import com.softicar.platform.common.core.i18n.key.II18nKey;
import com.softicar.platform.common.core.locale.CurrentLocale;
/**
* Maps an {@link II18nKey} into an {@link IDisplayString}.
*
* @author Oliver Richers
*/
class I18nKeyDisplayString implements IDisplayString {
private final II18nKey key;
private final Object[] arguments;
public I18nKeyDisplayString(II18nKey key, Object...arguments) {
this.key = key;
this.arguments = new I18nFormatArgumentsLocalizer(arguments).localize();
}
@Override
public String toString() {
return key//
.toLanguage(CurrentLocale.get().getLanguage())
.map(this::formatString)
.orElseGet(this::getFallbackTranslation);
}
private String formatString(String string) {
return String.format(string, arguments);
}
private String getFallbackTranslation() {
return InternalI18nTranslator.translate(key.toEnglish(), arguments);
}
}