Skip to content

Commit

Permalink
reduce copy-and-paste code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz committed Nov 11, 2023
1 parent c4dfbd0 commit 0db7d13
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

}

0 comments on commit 0db7d13

Please sign in to comment.