IDEMPIERE-4850 JasperReport: Implement ResourceBundle backed by AD_Message and AD_Element (#747)

* IDEMPIERE-4850 JasperReport: Implement ResourceBundle backed by AD_Message and AD_Element
This commit is contained in:
hengsin 2021-06-29 18:20:11 +08:00 committed by GitHub
parent 0e6e5efc7d
commit c33d0528a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 150 additions and 7 deletions

View File

@ -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<String,String> getMsgMap (String ad_language)
public synchronized CCache<String,String> 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<String,String> getElementMap (String ad_language)
/**
* Get language specific translation map for AD_Element
* @param ad_language
* @return ad_element map
*/
public synchronized CCache<String,String> 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

View File

@ -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<String> getKeys() {
Set<String> 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);
}
}

View File

@ -658,15 +658,17 @@ public class ReportStarter implements ProcessCall, ClientProcess
} else {
resFile = getFileResourceFile(resourcePath, bundleName, currLang);
}
PropertyResourceBundle res = null;
if (resFile!=null) {
try {
PropertyResourceBundle res = new PropertyResourceBundle( new FileInputStream(resFile));
res = new PropertyResourceBundle( new FileInputStream(resFile));
params.put("RESOURCE", res);
params.put(JRParameter.REPORT_RESOURCE_BUNDLE, res);
} catch (IOException e) {
;
}
}
params.put(JRParameter.REPORT_RESOURCE_BUNDLE, new MsgResourceBundle(res));
Connection conn = null;
JRSwapFileVirtualizer virtualizer = null;