fix: workaround for online translations using freemarker template models
This commit is contained in:
parent
9d4d29b653
commit
95d559712f
2 changed files with 35 additions and 2 deletions
|
@ -25,7 +25,7 @@ public class I18nBundle {
|
||||||
private static final Log log = LogFactory.getLog(I18nBundle.class);
|
private static final Log log = LogFactory.getLog(I18nBundle.class);
|
||||||
private static final String startSep = "\u25a4";
|
private static final String startSep = "\u25a4";
|
||||||
private static final String endSep = "\u25a5";
|
private static final String endSep = "\u25a5";
|
||||||
private static final String intSep = "\u25a6";
|
public static final String intSep = "\u25a6";
|
||||||
private static final String MESSAGE_BUNDLE_NOT_FOUND = "Text bundle ''{0}'' not found.";
|
private static final String MESSAGE_BUNDLE_NOT_FOUND = "Text bundle ''{0}'' not found.";
|
||||||
private static final String MESSAGE_KEY_NOT_FOUND = "Text bundle ''{0}'' has no text for ''{1}''";
|
private static final String MESSAGE_KEY_NOT_FOUND = "Text bundle ''{0}'' has no text for ''{1}''";
|
||||||
|
|
||||||
|
@ -91,6 +91,7 @@ public class I18nBundle {
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
for (int i = 0; i < parameters.length; i++) {
|
||||||
separatedArgs += parameters[i] + intSep;
|
separatedArgs += parameters[i] + intSep;
|
||||||
}
|
}
|
||||||
|
|
||||||
return startSep + key + intSep + textString + intSep + separatedArgs + message + endSep;
|
return startSep + key + intSep + textString + intSep + separatedArgs + message + endSep;
|
||||||
} else {
|
} else {
|
||||||
return message;
|
return message;
|
||||||
|
|
|
@ -3,11 +3,15 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.i18n.freemarker;
|
package edu.cornell.mannlib.vitro.webapp.i18n.freemarker;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.developer.DeveloperSettings;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.developer.Key;
|
||||||
import freemarker.template.TemplateMethodModelEx;
|
import freemarker.template.TemplateMethodModelEx;
|
||||||
import freemarker.template.TemplateModel;
|
import freemarker.template.TemplateModel;
|
||||||
import freemarker.template.TemplateModelException;
|
import freemarker.template.TemplateModelException;
|
||||||
|
@ -65,7 +69,11 @@ public class I18nStringTemplateModel implements TemplateMethodModelEx,
|
||||||
.get(i));
|
.get(i));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return MessageFormat.format(textString, unwrappedArgs);
|
if(isOnlineTranslationsEnabled()) {
|
||||||
|
return getOnlineTranslationsFormattedMessage(textString, unwrappedArgs);
|
||||||
|
} else {
|
||||||
|
return MessageFormat.format(textString, unwrappedArgs);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
String message = "Can't format '" + key + "' from bundle '"
|
String message = "Can't format '" + key + "' from bundle '"
|
||||||
+ bundleName + "', wrong argument types: " + args
|
+ bundleName + "', wrong argument types: " + args
|
||||||
|
@ -76,4 +84,28 @@ public class I18nStringTemplateModel implements TemplateMethodModelEx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits preProcessed string, formats message with arguments, lists arguments before message
|
||||||
|
* and combines preProcessed string back to be used with online translations.
|
||||||
|
* Substitutes arguments in message which is a part of preProcessed string
|
||||||
|
* @param preProcessed String "startSep + key + intSep + textString + intSep + message + endSep"
|
||||||
|
* @param arguments that should be listed before message and substituted in the message itself
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getOnlineTranslationsFormattedMessage(String preProcessed, Object[] unwrappedArgs) {
|
||||||
|
String[] parts = preProcessed.split(I18nBundle.intSep);
|
||||||
|
final int messageIndex = parts.length -1;
|
||||||
|
String message = MessageFormat.format(parts[messageIndex], unwrappedArgs);
|
||||||
|
String[] arguments = Arrays.copyOf(unwrappedArgs, unwrappedArgs.length, String[].class);
|
||||||
|
parts[messageIndex] = "";
|
||||||
|
String result = String.join(I18nBundle.intSep, parts) +
|
||||||
|
String.join(I18nBundle.intSep, arguments) +
|
||||||
|
I18nBundle.intSep + message ;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isOnlineTranslationsEnabled() {
|
||||||
|
return DeveloperSettings.getInstance().getBoolean(Key.I18N_ONLINE_TRANSLATION);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue