reorganization of the code - extraction of a method for preprocessing input strings for MethodFormat.format method

This commit is contained in:
Dragan Ivanovic 2022-12-16 11:40:10 +01:00
parent 311744ef64
commit fce6dc6f78
2 changed files with 18 additions and 3 deletions

View file

@ -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 "";
}
}

View file

@ -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) +