Skip to content

Commit

Permalink
Named arguments support
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaochao8 committed Jun 23, 2020
1 parent ec4d697 commit 8657eeb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/main/java/com/vmware/vipclient/i18n/util/FormatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
package com.vmware.vipclient.i18n.util;

import java.text.FieldPosition;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.l2.text.MessageFormat;

public class FormatUtils {

static Logger logger = LoggerFactory.getLogger(FormatUtils.class);
private FormatUtils() {

}
Expand All @@ -29,8 +35,34 @@ public static String format(String pattern, Object... arguments) {
return java.text.MessageFormat.format(escaped, arguments);
}

@Deprecated
public static String formatMsg(String pattern, Locale locale, Object... arguments) {
if (pattern != null && !pattern.isEmpty() && arguments != null && arguments.length > 0) {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
return messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
}
return pattern;
}

public static String formatMsg(String pattern, Locale locale, Map<String, Object> arguments) {
if (pattern != null && !pattern.isEmpty() && arguments != null && arguments.size() > 0) {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
return messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
}
return pattern;
}

public static String format(String pattern, Locale locale, Object... arguments) {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
return messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
try {
MessageFormat messageFormat = new MessageFormat(pattern, locale);
pattern = messageFormat.format(arguments, new StringBuilder(), new FieldPosition(0)).toString();
if (VIPCfg.getInstance().isPseudo() && pattern != null && !pattern.startsWith(ConstantsKeys.PSEUDOCHAR)) {
pattern = ConstantsKeys.PSEUDOCHAR2 + pattern + ConstantsKeys.PSEUDOCHAR2;
}
return pattern;
} catch (Exception e) {
logger.error(e.getMessage());
}
return pattern;
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/vmware/vip/i18n/MessageFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.vmware.vipclient.i18n.base.cache.FormattingCache;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.l2.text.MessageFormat;
import com.vmware.vipclient.i18n.util.FormatUtils;

public class MessageFormatTest extends BaseTestClass {

Expand Down Expand Up @@ -99,4 +100,17 @@ public void testFormat() {
Assert.assertEquals("102 psa",
msgFmt4.format(new Object[] { 102 }, new StringBuilder(), new FieldPosition(0)).toString());
}

public void testNamedArguments() {
String msg = "{a} - {b} of {c} customers";

Map<String, Object> msgargs = new HashMap<>();
msgargs.put("a", 1);
msgargs.put("b", 5);
msgargs.put("c", 10);

String formatted = FormatUtils.formatMsg(msg, Locale.ENGLISH, msgargs);

Assert.assertEquals("1 - 5 of 10 customers", formatted);
}
}

0 comments on commit 8657eeb

Please sign in to comment.