1313public class I18nMessage {
1414 private static ResourceBundle bundle ;
1515
16- public static class TEST {
17- public static final String WELCOME = "TEST_WELCOME" ;
18- }
19-
2016 public static class IR {
2117 public static final String IR_SEND_SCORE_SUCCESS = "IR_SEND_SCORE_SUCCESS" ;
2218 public static final String IR_SEND_SCORE_FAILED = "IR_SEND_SCORE_FAILED" ;
2319 }
2420
21+ public static class TEST {
22+ public static final String TEST_ERROR = "TEST_ERROR" ;
23+ }
24+
25+ /**
26+ * For creating an error code automatically, all error messages should be registered here with their full-name
27+ */
28+ private enum ErrorMessage {
29+ IR_SEND_SCORE_FAILED ,
30+ TEST_ERROR ;
31+ }
32+
2533 /**
2634 * An explicit function that allows you set a bundle manually
2735 *
@@ -42,4 +50,16 @@ public static String getI18nMessage(String key) {
4250 public static String getI18nMessage (String key , Object ... args ) {
4351 return MessageFormat .format (getI18nMessage (key ), args );
4452 }
53+
54+ public static String getI18nError (String key , Object ... args ) {
55+ try {
56+ ErrorMessage errorMessage = ErrorMessage .valueOf (key );
57+ // Error code starts from 1 (we don't want a code like 0000)
58+ int errorCode = errorMessage .ordinal () + 1 ;
59+ String originalMessage = getI18nMessage (key , args );
60+ return String .format ("[%04d]" , errorCode ) + originalMessage ;
61+ } catch (IllegalArgumentException e ) {
62+ throw new RuntimeException (e );
63+ }
64+ }
4565}
0 commit comments