aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDirk Fauth2013-11-28 02:36:24 (EST)
committerDirk Fauth2013-11-28 02:36:24 (EST)
commit8ce11393a2753efc8b56161293189cc1b545f778 (patch)
treebd88511cf6604390612eb0679033dbff2a11a015
parent059ebb106998699c6049c0fe773453bd48cbfb47 (diff)
downloadeclipse.platform.runtime-8ce11393a2753efc8b56161293189cc1b545f778.zip
eclipse.platform.runtime-8ce11393a2753efc8b56161293189cc1b545f778.tar.gz
eclipse.platform.runtime-8ce11393a2753efc8b56161293189cc1b545f778.tar.bz2
Bug 421954 - Add support for camel cased member variables inrefs/changes/36/19036/1
new message extension Change-Id: I0f457ab9d405df4108d25ab69f0e9ce202cc3696 Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
-rw-r--r--bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleTranslationProvider.java73
1 files changed, 61 insertions, 12 deletions
diff --git a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleTranslationProvider.java b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleTranslationProvider.java
index 60e68d4..0e20b2e 100644
--- a/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleTranslationProvider.java
+++ b/bundles/org.eclipse.e4.core.services/src/org/eclipse/e4/core/internal/services/ResourceBundleTranslationProvider.java
@@ -28,9 +28,10 @@ public class ResourceBundleTranslationProvider {
/**
*
- * @param resourceBundle The {@link ResourceBundle} to use for translations.
- * Can be <code>null</code>, which will lead to simply return the key
- * modified by prefixing and suffixing it with "!" when calling translate(String).
+ * @param resourceBundle
+ * The {@link ResourceBundle} to use for translations. Can be <code>null</code>,
+ * which will lead to simply return the key modified by prefixing and suffixing it
+ * with "!" when calling translate(String).
*/
public ResourceBundleTranslationProvider(ResourceBundle resourceBundle) {
this.setResourceBundle(resourceBundle);
@@ -41,29 +42,48 @@ public class ResourceBundleTranslationProvider {
* set to this {@link ResourceBundleTranslationProvider}. If there is no {@link ResourceBundle}
* set or there is no translation found for the given key, the key itself prefixed and suffixed
* with "!" will be returned to indicate that there is no translation found.
- * <p>This implementation also supports the usage of dot separation for property keys. As in Java
+ * <p>
+ * This implementation also supports the usage of dot separation for property keys. As in Java
* variables can not be separated with a dot, the underscore needs to be used for separation of
* the variable. This will be replaced automatically to a dot, if there is no translation found
* with an underscore as separator.
* </p>
- * @param key The key of the requested translation property.
- * @return The translation for the given key or the key itself prefixed and suffixed
- * with "!" to indicate that there is no translation available for the
- * given key.
+ * As per request, this implementation also supports camel cased member variables to match the
+ * appropriate property keys. The search order for the translation property key is as follows:
+ * <ol>
+ * <li>key = property key (no modification)</li>
+ * <li>underscorified key = property key (camel cased key is transformed to underscorified key)</li>
+ * <li>dot separated key = property key (underscorified key is transformed to dot separated key)
+ * </li>
+ * <li>nothing matches -&gt; return key prefixed and suffixed with "!"</li>
+ * </ol>
+ *
+ * @param key
+ * The key of the requested translation property.
+ * @return The translation for the given key or the key itself prefixed and suffixed with "!" to
+ * indicate that there is no translation available for the given key.
*/
public String translate(String key) {
String result = ""; //$NON-NLS-1$
try {
if (this.resourceBundle == null) {
result = "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
- }
- else {
+ } else {
+ // search for the key as is
result = this.resourceBundle.getString(key);
}
} catch (MissingResourceException e) {
- if (key.contains("_")) { //$NON-NLS-1$
+ // key was not found as is, check if it is camel cased
+ String uKey = underscorify(key);
+ if (!key.equals(uKey)) {
+ // the underscorify method modified the key, so it seems it was camel cased
+ result = translate(uKey);
+ } else if (key.contains("_")) { //$NON-NLS-1$
+ // underscorify didn't modify the key, but the key contains the underscore
+ // so we check for the dot separated key
result = translate(key.replace('_', '.'));
} else {
+ // nothing matched, so we return the key itself marked with !
result = "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
}
}
@@ -78,9 +98,38 @@ public class ResourceBundleTranslationProvider {
}
/**
- * @param resourceBundle The {@link ResourceBundle} to use for translations.
+ * @param resourceBundle
+ * The {@link ResourceBundle} to use for translations.
*/
public void setResourceBundle(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
}
+
+ /**
+ * This method is used to create a underscorified key out of a camel cased String.
+ * <p>
+ * Note: This method is introduced to support Java Naming Conventions for member variables,
+ * which says that member variables should be camel cased. See <a href=
+ * "http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367"
+ * > Naming Conventions</a>.
+ *
+ * @param key
+ * The possible camel cased key that needs to be underscorified.
+ * @return The underscorified key.
+ */
+ public static String underscorify(String key) {
+ if (key == null || key.isEmpty()) {
+ return key;
+ }
+
+ StringBuilder result = new StringBuilder();
+ for (int i = 0; i < key.length(); i++) {
+ if (Character.isUpperCase(key.charAt(i))) {
+ result.append("_").append(Character.toLowerCase(key.charAt(i))); //$NON-NLS-1$
+ } else {
+ result.append(key.charAt(i));
+ }
+ }
+ return result.toString();
+ }
}