I18n.java
5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletRequest
* org.apache.sling.api.SlingHttpServletRequest
*/
package com.day.cq.i18n;
import org.apache.sling.api.SlingHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ResourceBundle;
public class I18n {
private final HttpServletRequest request;
private final ResourceBundle resourceBundle;
private static String BUNDLE_REQ_ATTR = "org.apache.sling.i18n.resourcebundle";
public I18n(HttpServletRequest request) {
this.request = request;
this.resourceBundle = null;
}
public I18n(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
this.request = null;
}
private ResourceBundle getResourceBundle() {
if (this.request != null) {
return I18n.getResourceBundle(this.request);
}
return this.resourceBundle;
}
private static ResourceBundle getResourceBundle(HttpServletRequest req) {
if (req instanceof SlingHttpServletRequest) {
return ((SlingHttpServletRequest)req).getResourceBundle(null);
}
return (ResourceBundle)req.getAttribute(BUNDLE_REQ_ATTR);
}
public String get(String text) {
return I18n.get(this.getResourceBundle(), text, null, (Object[])null);
}
public String get(String text, String comment) {
return I18n.get(this.getResourceBundle(), text, comment, (Object[])null);
}
public /* varargs */ String get(String text, String comment, Object ... args) {
return I18n.get(this.getResourceBundle(), text, comment, args);
}
public String getVar(String text) {
return I18n.getVar(this.getResourceBundle(), text, null);
}
public String getVar(String text, String comment) {
return I18n.getVar(this.getResourceBundle(), text, comment);
}
public /* varargs */ String getVar(String text, String comment, Object ... args) {
return I18n.getVar(this.getResourceBundle(), text, comment, args);
}
public static String get(HttpServletRequest request, String text) {
return I18n.get(I18n.getResourceBundle(request), text, null, (Object[])null);
}
public static String get(HttpServletRequest request, String text, String comment) {
return I18n.get(I18n.getResourceBundle(request), text, comment, (Object[])null);
}
public static /* varargs */ String get(HttpServletRequest request, String text, String comment, Object ... args) {
return I18n.get(I18n.getResourceBundle(request), text, comment, args);
}
public static String getVar(HttpServletRequest request, String text) {
return I18n.getVar(I18n.getResourceBundle(request), text, null);
}
public static String getVar(HttpServletRequest request, String text, String comment) {
return I18n.getVar(I18n.getResourceBundle(request), text, comment);
}
public static /* varargs */ String getVar(HttpServletRequest request, String text, String comment, Object ... args) {
return I18n.getVar(I18n.getResourceBundle(request), text, comment, args);
}
public static String get(ResourceBundle resourceBundle, String text) {
return I18n.get(resourceBundle, text, null, (Object[])null);
}
public static String get(ResourceBundle resourceBundle, String text, String comment) {
return I18n.get(resourceBundle, text, comment, (Object[])null);
}
public static /* varargs */ String get(ResourceBundle resourceBundle, String text, String comment, Object ... args) {
String msg;
if (text == null) {
return text;
}
if (resourceBundle == null) {
return I18n.patchText(text, args);
}
boolean equals = false;
if (comment != null && comment.length() > 0) {
String key = text + " ((" + comment + "))";
if (key.equals(msg = resourceBundle.getString(key))) {
equals = true;
msg = text;
}
} else {
msg = resourceBundle.getString(text);
if (text.equals(msg)) {
equals = true;
}
}
try {
return I18n.patchText(msg, args);
}
catch (IllegalArgumentException e) {
if (equals) {
throw e;
}
return msg;
}
}
public static String getVar(ResourceBundle resourceBundle, String text) {
return I18n.get(resourceBundle, text, null);
}
public static String getVar(ResourceBundle resourceBundle, String text, String comment) {
return I18n.get(resourceBundle, text, comment);
}
public static /* varargs */ String getVar(ResourceBundle resourceBundle, String text, String comment, Object ... args) {
return I18n.get(resourceBundle, text, comment, args);
}
private static /* varargs */ String patchText(String text, Object ... args) {
if (text == null || args == null) {
return text;
}
for (int i = 0; i < args.length; ++i) {
Object o = args[i];
if (o == null) continue;
text = text.replace("{" + i + "}", o.toString());
}
return text;
}
}