1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.protocol.configuration;
22
23 import org.waarp.common.logging.SysErrLogger;
24 import org.waarp.common.utility.ParametersChecker;
25 import org.waarp.common.utility.SystemPropertyUtil;
26
27 import java.text.MessageFormat;
28 import java.util.Locale;
29 import java.util.MissingResourceException;
30 import java.util.ResourceBundle;
31
32 public class Messages {
33 private static final String BUNDLE_NAME = "messages";
34
35 private static ResourceBundle resourceBundle;
36 private static String slocale = "en";
37
38 static {
39 try {
40 setSlocale(
41 SystemPropertyUtil.get(R66SystemProperties.OPENR66_LOCALE, "en"));
42 if (ParametersChecker.isEmpty(getSlocale())) {
43 setSlocale("en");
44 }
45 init(new Locale(getSlocale()));
46 } catch (final Throwable ignored) {
47 SysErrLogger.FAKE_LOGGER
48 .syserr("Error during static execution",
49 ignored);
50 }
51 }
52
53 private Messages() {
54 }
55
56 public static void init(Locale locale) {
57 if (locale == null) {
58 setSlocale("en");
59 locale = new Locale(getSlocale());
60 } else {
61 setSlocale(locale.getLanguage());
62 }
63 resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
64 }
65
66 public static String getString(final String key) {
67 try {
68 return resourceBundle.getString(key);
69 } catch (final MissingResourceException e) {
70 return '!' + key + '!';
71 }
72 }
73
74 public static String getString(final String key, final Object... args) {
75 try {
76 final String source = resourceBundle.getString(key);
77 return MessageFormat.format(source, args);
78 } catch (final MissingResourceException e) {
79 return '!' + key + '!';
80 }
81 }
82
83
84
85
86 public static String getSlocale() {
87 return slocale;
88 }
89
90
91
92
93 public static void setSlocale(final String slocale) {
94 Messages.slocale = slocale;
95 }
96 }