package ua.com.minersstudios.whomine.util;
import gnu.gettext.GettextResource;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
public class i18n
{
private final ResourceBundle resourceBundle;
static
{
resourceBundle = ResourceBundle.getBundle("Messages");
final double percentage = concludeTranslationCompleteness(resourceBundle);
Locale locale = Locale.getDefault();
getLogger().fine(MessageFormat.format("{0}_{1} translation ({2,percent}) is loaded based on your default locale.",
locale.getLanguage(), locale.getCountry(), percentage));
}
public String i18n(@NonNull final String msg) { return GettextResource.gettext(resourceBundle, msg); }
public String i18n(@NonNull final String msgWithContext, @NonNull final String fallbackMsg)
{
try { return resourceBundle.getString(msgWithContext); }
catch (MissingResourceException e) { return fallbackMsg; }
}
private double concludeTranslationIncompleteness(@NonNull final ResourceBundle resourceBundle)
{
final int valueCount = resourceBundle.keySet().size();
int emptyValueCount = 0;
for (String key : resourceBundle.keySet())
{
String value = resourceBundle.getString(key);
if (value == null || value.trim().isEmpty()) ++emptyValueCount;
}
return ((double)emptyValueCount / valueCount) * 100.0;
}
private double concludeTranslationCompleteness(@NonNull final ResourceBundle resourceBundle)
{
return 100.0 - concludeTranslationIncompleteness(resourceBundle);
}
}
import java.util.ResourceBundle;