The following code works in a Java action that takes two strings as parameters (the enumeration and the language code) and returns the translated caption.
// BEGIN USER CODE
try {
final String[] proxyPath = Enumeration.split("\\.");
if (proxyPath.length != 3) {
throw new MendixRuntimeException("Enumeration should be in the "
+ "format module.enumeration.value`.");
}
final String proxyClassName = String.format("%s.proxies.%s",
proxyPath[0].toLowerCase(), proxyPath[1]);
final Class<?> proxyClass = Class.forName(proxyClassName);
if (!proxyClass.isEnum()) {
throw new MendixRuntimeException("Given enumeration is a "
+ "class, but not an enumeration.");
}
final Field enumField = proxyClass.getField(proxyPath[2]);
final Method captionMethod = proxyClass.getMethod("getCaption",
String.class);
return (String) captionMethod.invoke(enumField.get(null),
LanguageCode);
} catch (ClassNotFoundException cnfe) {
throw new MendixRuntimeException("Cannot find given enumeration.",
cnfe);
} catch (NoSuchFieldException nsfe) {
throw new MendixRuntimeException("Can find enumeration, but not "
+ "the requested value.", nsfe);
} catch (NoSuchMethodException nsme) {
throw new MendixRuntimeException("Can find enumeration, but not "
+ "the getCaption method.", nsme);
} catch (SecurityException se) {
throw new MendixRuntimeException("Cannot access enumeration.", se);
} catch (Exception e) {
throw new MendixRuntimeException("Unexpected error", e);
}
// END USER CODE
This is essentially an adapted version of the second answer you linked to. The following happens:
The above uses basic Java reflection, which can trigger a lot of errors, hence the error handling. This should result in somewhat readable errors in the console.
For completeness, this is the full Java action code (you can deduce the parameter names and types from this):
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à , etc. are supported in comments.
package translatedenumerationjava.actions;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.mendix.systemwideinterfaces.MendixRuntimeException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
public class JA_GetEnumerationCaption extends CustomJavaAction<java.lang.String>
{
private java.lang.String Enumeration;
private java.lang.String LanguageCode;
public JA_GetEnumerationCaption(IContext context, java.lang.String Enumeration, java.lang.String LanguageCode)
{
super(context);
this.Enumeration = Enumeration;
this.LanguageCode = LanguageCode;
}
@java.lang.Override
public java.lang.String executeAction() throws Exception
{
// BEGIN USER CODE
try {
final String[] proxyPath = Enumeration.split("\\.");
if (proxyPath.length != 3) {
throw new MendixRuntimeException("Enumeration should be in the "
+ "format module.enumeration.value`.");
}
final String proxyClassName = String.format("%s.proxies.%s",
proxyPath[0].toLowerCase(), proxyPath[1]);
final Class<?> proxyClass = Class.forName(proxyClassName);
if (!proxyClass.isEnum()) {
throw new MendixRuntimeException("Given enumeration is a "
+ "class, but not an enumeration.");
}
final Field enumField = proxyClass.getField(proxyPath[2]);
final Method captionMethod = proxyClass.getMethod("getCaption",
String.class);
return (String) captionMethod.invoke(enumField.get(null),
LanguageCode);
} catch (ClassNotFoundException cnfe) {
throw new MendixRuntimeException("Cannot find given enumeration.",
cnfe);
} catch (NoSuchFieldException nsfe) {
throw new MendixRuntimeException("Can find enumeration, but not "
+ "the requested value.", nsfe);
} catch (NoSuchMethodException nsme) {
throw new MendixRuntimeException("Can find enumeration, but not "
+ "the getCaption method.", nsme);
} catch (SecurityException se) {
throw new MendixRuntimeException("Cannot access enumeration.", se);
} catch (Exception e) {
throw new MendixRuntimeException("Unexpected error", e);
}
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "JA_GetEnumerationCaption";
}
// BEGIN EXTRA CODE
// END EXTRA CODE
}
And a screenshot of calling the action: