From 0db7d132d3bc104015cc2f155d9d2c888954cc2b Mon Sep 17 00:00:00 2001 From: "Jan S." Date: Sat, 11 Nov 2023 14:55:18 +0100 Subject: [PATCH] reduce copy-and-paste code --- .../feeling/decompiler/cfr/i18n/Messages.java | 32 +++++------- .../feeling/decompiler/jd/i18n/Messages.java | 32 +++++------- .../decompiler/procyon/i18n/Messages.java | 25 ++------- .../source/attach/i18n/Messages.java | 24 ++------- .../decompiler/vineflower/i18n/Messages.java | 27 +++++----- .../decompiler/i18n/EcdResouceBundle.java | 51 +++++++++++++++++++ .../sf/feeling/decompiler/i18n/Messages.java | 24 ++------- 7 files changed, 99 insertions(+), 116 deletions(-) create mode 100644 org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/EcdResouceBundle.java diff --git a/org.sf.feeling.decompiler.cfr/src/org/sf/feeling/decompiler/cfr/i18n/Messages.java b/org.sf.feeling.decompiler.cfr/src/org/sf/feeling/decompiler/cfr/i18n/Messages.java index 278709c1..78cd64cb 100644 --- a/org.sf.feeling.decompiler.cfr/src/org/sf/feeling/decompiler/cfr/i18n/Messages.java +++ b/org.sf.feeling.decompiler.cfr/src/org/sf/feeling/decompiler/cfr/i18n/Messages.java @@ -1,33 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2017 Chen Chao and other ECD project contributors. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ package org.sf.feeling.decompiler.cfr.i18n; -import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import org.sf.feeling.decompiler.i18n.EcdResouceBundle; public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.cfr.i18n.messages"; //$NON-NLS-1$ - - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - - try { - String result = RESOURCE_BUNDLE.getString(key); - try { - result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (UnsupportedEncodingException e) { - return '!' + key + '!'; - } - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -37,6 +28,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } + } diff --git a/org.sf.feeling.decompiler.jd/src/org/sf/feeling/decompiler/jd/i18n/Messages.java b/org.sf.feeling.decompiler.jd/src/org/sf/feeling/decompiler/jd/i18n/Messages.java index 0287725a..1731c058 100644 --- a/org.sf.feeling.decompiler.jd/src/org/sf/feeling/decompiler/jd/i18n/Messages.java +++ b/org.sf.feeling.decompiler.jd/src/org/sf/feeling/decompiler/jd/i18n/Messages.java @@ -1,33 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2017 Chen Chao and other ECD project contributors. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ package org.sf.feeling.decompiler.jd.i18n; -import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import org.sf.feeling.decompiler.i18n.EcdResouceBundle; public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.jd.i18n.messages"; //$NON-NLS-1$ - - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - - try { - String result = RESOURCE_BUNDLE.getString(key); - try { - result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (UnsupportedEncodingException e) { - return '!' + key + '!'; - } - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -37,6 +28,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } + } diff --git a/org.sf.feeling.decompiler.procyon/src/org/sf/feeling/decompiler/procyon/i18n/Messages.java b/org.sf.feeling.decompiler.procyon/src/org/sf/feeling/decompiler/procyon/i18n/Messages.java index fea76819..a2ea7fc2 100644 --- a/org.sf.feeling.decompiler.procyon/src/org/sf/feeling/decompiler/procyon/i18n/Messages.java +++ b/org.sf.feeling.decompiler.procyon/src/org/sf/feeling/decompiler/procyon/i18n/Messages.java @@ -1,33 +1,17 @@ package org.sf.feeling.decompiler.procyon.i18n; -import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import org.sf.feeling.decompiler.i18n.EcdResouceBundle; public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.procyon.i18n.messages"; //$NON-NLS-1$ - - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - - try { - String result = RESOURCE_BUNDLE.getString(key); - try { - result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (UnsupportedEncodingException e) { - return '!' + key + '!'; - } - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -37,6 +21,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } + } diff --git a/org.sf.feeling.decompiler.source.attach/src/org/sf/feeling/decompiler/source/attach/i18n/Messages.java b/org.sf.feeling.decompiler.source.attach/src/org/sf/feeling/decompiler/source/attach/i18n/Messages.java index 35a488e8..883c93f9 100644 --- a/org.sf.feeling.decompiler.source.attach/src/org/sf/feeling/decompiler/source/attach/i18n/Messages.java +++ b/org.sf.feeling.decompiler.source.attach/src/org/sf/feeling/decompiler/source/attach/i18n/Messages.java @@ -8,33 +8,17 @@ package org.sf.feeling.decompiler.source.attach.i18n; -import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import org.sf.feeling.decompiler.i18n.EcdResouceBundle; public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.source.attach.i18n.messages"; //$NON-NLS-1$ - - public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - - try { - String result = RESOURCE_BUNDLE.getString(key); - try { - result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (UnsupportedEncodingException e) { - return '!' + key + '!'; - } - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -44,7 +28,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } } diff --git a/org.sf.feeling.decompiler.vineflower/src/org/sf/feeling/decompiler/vineflower/i18n/Messages.java b/org.sf.feeling.decompiler.vineflower/src/org/sf/feeling/decompiler/vineflower/i18n/Messages.java index c8c1ea35..2c4528ce 100644 --- a/org.sf.feeling.decompiler.vineflower/src/org/sf/feeling/decompiler/vineflower/i18n/Messages.java +++ b/org.sf.feeling.decompiler.vineflower/src/org/sf/feeling/decompiler/vineflower/i18n/Messages.java @@ -1,28 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2017 Chen Chao and other ECD project contributors. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ package org.sf.feeling.decompiler.vineflower.i18n; -import java.nio.charset.StandardCharsets; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import org.sf.feeling.decompiler.i18n.EcdResouceBundle; public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.vineflower.i18n.messages"; //$NON-NLS-1$ - - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - try { - String result = RESOURCE_BUNDLE.getString(key); - result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -32,6 +28,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } + } diff --git a/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/EcdResouceBundle.java b/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/EcdResouceBundle.java new file mode 100644 index 00000000..d757c1f4 --- /dev/null +++ b/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/EcdResouceBundle.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2017 Chen Chao and other ECD project contributors. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.sf.feeling.decompiler.i18n; + +import java.io.UnsupportedEncodingException; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class EcdResouceBundle { + + protected final ResourceBundle resourceBundle; + + public EcdResouceBundle(Class messagesClass) { + ClassLoader classLoader = messagesClass.getClassLoader(); + Locale targetLocale = Locale.getDefault(); + String resourceBundleName = messagesClass.getPackageName() + ".messages"; + this.resourceBundle = ResourceBundle.getBundle(resourceBundleName, targetLocale, classLoader); + } + + public String getString(String key) { + try { + String result = resourceBundle.getString(key); + try { + result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (UnsupportedEncodingException e) { + return '!' + key + '!'; + } + return result; + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } + + /** + * Gets formatted translation for current local + * + * @param key the key + * @return translated value string + */ + public String getFormattedString(String key, Object[] arguments) { + return MessageFormat.format(getString(key), arguments); + } +} diff --git a/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/Messages.java b/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/Messages.java index ba1ae694..e30a23d0 100644 --- a/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/Messages.java +++ b/org.sf.feeling.decompiler/src/org/sf/feeling/decompiler/i18n/Messages.java @@ -8,33 +8,15 @@ package org.sf.feeling.decompiler.i18n; -import java.io.UnsupportedEncodingException; -import java.text.MessageFormat; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - public class Messages { - private static final String BUNDLE_NAME = "org.sf.feeling.decompiler.i18n.messages"; //$NON-NLS-1$ - - public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + private static final EcdResouceBundle RESOURCE_BUNDLE = new EcdResouceBundle(Messages.class); private Messages() { } public static String getString(String key) { - - try { - String result = RESOURCE_BUNDLE.getString(key); - try { - result = new String(result.getBytes("ISO-8859-1"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (UnsupportedEncodingException e) { - return '!' + key + '!'; - } - return result; - } catch (MissingResourceException e) { - return '!' + key + '!'; - } + return RESOURCE_BUNDLE.getString(key); } /** @@ -44,7 +26,7 @@ public static String getString(String key) { * @return translated value string */ public static String getFormattedString(String key, Object[] arguments) { - return MessageFormat.format(getString(key), arguments); + return RESOURCE_BUNDLE.getFormattedString(key, arguments); } }