net.sourceforge.jbizmo.commons.i18n.I18N Maven / Gradle / Ivy
The newest version!
/*
* This file is part of JBizMo, a set of tools, libraries and plug-ins
* for modeling and creating Java-based enterprise applications.
* For more information visit:
*
* http://sourceforge.net/projects/jbizmo/
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.sourceforge.jbizmo.commons.i18n;
import java.lang.invoke.MethodHandles;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Utility class for the translation of text fragments
*
*
* Copyright 2017 (C) by Martin Ganserer
*
* @author Martin Ganserer
* @version 1.0.0
*/
public class I18N {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String LABEL_SEPARATOR = ":";
/**
* Prevent instantiation
*/
private I18N() {
}
/**
* @param resourceBoundle
* @param key
* @param params
* @return the translation text from the given resource bundle by using the given key
*/
public static String getTranslation(ResourceBundle resourceBoundle, String key, Object... params) {
String msg;
try {
msg = resourceBoundle.getString(key);
}
catch (final Exception e) {
logger.error("Could not find translation for key '{}'!", key, e);
return "???-" + key + "-???";
}
if (params.length > 0) {
final var format = new MessageFormat(msg);
msg = format.format(params);
}
return msg;
}
/**
* @param resourceBoundle
* @param key
* @return the translation for a field label from the given resource bundle by using the given key
*/
public static String getTranslationForFieldLabel(ResourceBundle resourceBoundle, String key) {
return getTranslation(resourceBoundle, key) + LABEL_SEPARATOR;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy