diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/TranslationProvider.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/TranslationProvider.java index 986fcf34d..d54be4da3 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/TranslationProvider.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/TranslationProvider.java @@ -165,7 +165,21 @@ public class TranslationProvider { if (parameters.length == 0) { return textString; } else { - return MessageFormat.format(textString.replaceAll("''", "'").replaceAll("'", "''"), parameters); + return MessageFormat.format(TranslationProvider.preprocessForFormating(textString), parameters); + } + } + + /** + * This method should prepare the inputText for MessageFormat.format method. At the moment it is replacing single + * apostrophe with double, it might be extented in the future with some additional preprocessing. + * @param inputText - string which should be preprocessed + * @return preprocessed input string, i.e. string with replaced single apostrophe with double + */ + public static String preprocessForFormating(String inputText){ + if (inputText != null) { + return inputText.replace("''", "'").replace("'", "''"); + } else { + return ""; } } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/freemarker/I18nStringTemplateModel.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/freemarker/I18nStringTemplateModel.java index 1cf3d310b..2980e1f0f 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/freemarker/I18nStringTemplateModel.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/freemarker/I18nStringTemplateModel.java @@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.i18n.freemarker; import java.text.MessageFormat; import java.util.List; +import edu.cornell.mannlib.vitro.webapp.i18n.TranslationProvider; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -67,7 +68,7 @@ public class I18nStringTemplateModel implements TemplateMethodModelEx, if(isOnlineTranslationsEnabled()) { return getOnlineTranslationsFormattedMessage(textString, unwrappedArgs); } else { - return MessageFormat.format(textString.replaceAll("''", "'").replaceAll("'", "''"), unwrappedArgs); + return MessageFormat.format(TranslationProvider.preprocessForFormating(textString), unwrappedArgs); } } catch (Exception e) { String message = "Can't format '" + key + "', wrong argument types: " + args @@ -89,7 +90,7 @@ public class I18nStringTemplateModel implements TemplateMethodModelEx, private String getOnlineTranslationsFormattedMessage(String preProcessed, Object[] args) { String[] parts = preProcessed.split(I18nBundle.INT_SEP); final int messageIndex = parts.length -1; - String message = MessageFormat.format(parts[messageIndex].replaceAll("''", "'").replaceAll("'", "''"), args); + String message = MessageFormat.format(TranslationProvider.preprocessForFormating(parts[messageIndex]), args); String[] arguments = convertToArrayOfStrings(args); parts[messageIndex] = ""; String result = String.join(I18nBundle.INT_SEP, parts) +