diff --git a/org.adempiere.base/src/org/compiere/util/Msg.java b/org.adempiere.base/src/org/compiere/util/Msg.java index ad09d39037..544ce6f473 100644 --- a/org.adempiere.base/src/org/compiere/util/Msg.java +++ b/org.adempiere.base/src/org/compiere/util/Msg.java @@ -54,7 +54,7 @@ public final class Msg * Get Message Object * @return Msg */ - private static synchronized Msg get() + public static synchronized Msg get() { if (s_msg == null) s_msg = new Msg(); @@ -81,7 +81,7 @@ public final class Msg * @param ad_language Language Key * @return HashMap of Language */ - private synchronized CCache getMsgMap (String ad_language) + public synchronized CCache getMsgMap (String ad_language) { String AD_Language = ad_language; if (AD_Language == null || AD_Language.length() == 0) @@ -101,7 +101,12 @@ public final class Msg return retValue; } // getMsgMap - private synchronized CCache getElementMap (String ad_language) + /** + * Get language specific translation map for AD_Element + * @param ad_language + * @return ad_element map + */ + public synchronized CCache getElementMap (String ad_language) { String AD_Language = ad_language; if (AD_Language == null || AD_Language.length() == 0) @@ -688,7 +693,33 @@ public final class Msg return outStr.toString(); } // parseTranslation + /** + * + * @param adLanguage + * @param text + * @return true if translation exists for text and adLanguage + */ + public static boolean hasTranslation(String adLanguage, String text) + { + if (Util.isEmpty(text, true)) + return false; + + String AD_Language = adLanguage; + if (AD_Language == null || AD_Language.length() == 0) + AD_Language = Language.getBaseAD_Language(); + // Check AD_Message + String retStr = get().lookup (AD_Language, text); + if (!Util.isEmpty(retStr, true)) + return true; + + // Check AD_Element + retStr = getElement(AD_Language, text, false); + if (!Util.isEmpty(retStr, true)) + return true; + + return false; + } /** * Get translated text message for AD_Message, ampersand cleaned (used to indicate shortcut) * @param ctx Context to retrieve language diff --git a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/MsgResourceBundle.java b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/MsgResourceBundle.java new file mode 100644 index 0000000000..bc9b5dfb64 --- /dev/null +++ b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/MsgResourceBundle.java @@ -0,0 +1,110 @@ +/*********************************************************************** + * This file is part of iDempiere ERP Open Source * + * http://www.idempiere.org * + * * + * Copyright (C) Contributors * + * * + * This program 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 program 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 program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - hengsin * + **********************************************************************/ +package org.adempiere.report.jasper; + +import java.util.Collections; +import java.util.Enumeration; +import java.util.Locale; +import java.util.ResourceBundle; +import java.util.Set; + +import org.compiere.util.Env; +import org.compiere.util.Msg; + +/** + * Resource bundle wrapper for {@link Msg#translate(String, boolean, String)} + * @author hengsin + * + */ +public class MsgResourceBundle extends ResourceBundle { + + private ResourceBundle overridingResourceBundle; + private Locale locale; + private String adLanguage; + private boolean isSOTrx; + + /** + * + * @param overridingResourceBundle + */ + public MsgResourceBundle(ResourceBundle overridingResourceBundle) { + this(overridingResourceBundle, Env.getLocale(Env.getCtx()), Env.getAD_Language(Env.getCtx()), Env.isSOTrx(Env.getCtx())); + } + + /** + * + * @param overridingResourceBundle + * @param locale + * @param adLanguage + * @param isSOTrx + */ + public MsgResourceBundle(ResourceBundle overridingResourceBundle, Locale locale, String adLanguage, boolean isSOTrx) { + this.overridingResourceBundle = overridingResourceBundle; + this.locale = locale; + this.adLanguage = adLanguage; + this.isSOTrx = isSOTrx; + } + + @Override + protected Object handleGetObject(String key) { + if (overridingResourceBundle != null && overridingResourceBundle.containsKey(key)) { + return overridingResourceBundle.getObject(key); + } + return Msg.translate(adLanguage, isSOTrx, key); + } + + @Override + public Enumeration getKeys() { + Set set = null; + if (overridingResourceBundle != null) { + set = overridingResourceBundle.keySet(); + } + if (set == null) + set = Msg.get().getMsgMap(adLanguage).keySet(); + else + set.addAll(Msg.get().getMsgMap(adLanguage).keySet()); + set.addAll(Msg.get().getElementMap(adLanguage).keySet()); + return Collections.enumeration(set); + } + + @Override + public Locale getLocale() { + return this.locale; + } + + @Override + public String getBaseBundleName() { + return getClass().getName(); + } + + @Override + public boolean containsKey(String key) { + if (overridingResourceBundle != null) { + if (overridingResourceBundle.containsKey(key)) + return true; + } + return Msg.hasTranslation(adLanguage, key); + } +} diff --git a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java index 96939c3a41..c0632f3d15 100644 --- a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java +++ b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java @@ -658,15 +658,17 @@ public class ReportStarter implements ProcessCall, ClientProcess } else { resFile = getFileResourceFile(resourcePath, bundleName, currLang); } - if (resFile!=null) { + + PropertyResourceBundle res = null; + if (resFile!=null) { try { - PropertyResourceBundle res = new PropertyResourceBundle( new FileInputStream(resFile)); - params.put("RESOURCE", res); - params.put(JRParameter.REPORT_RESOURCE_BUNDLE, res); + res = new PropertyResourceBundle( new FileInputStream(resFile)); + params.put("RESOURCE", res); } catch (IOException e) { ; } } + params.put(JRParameter.REPORT_RESOURCE_BUNDLE, new MsgResourceBundle(res)); Connection conn = null; JRSwapFileVirtualizer virtualizer = null;