PseudoTranslations.java
4.97 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.i18n.ResourceBundleProvider
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.i18n.impl.bundle;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.i18n.ResourceBundleProvider;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=1, label="%pseudo.name", description="%pseudo.description")
@Service(value={ResourceBundleProvider.class})
@Property(name="service.ranking", intValue={1000})
public class PseudoTranslations
implements ResourceBundleProvider {
private static final Logger log = LoggerFactory.getLogger(PseudoTranslations.class);
private static final String DELIMITER = " ((";
private static final String DELIMITER_END = " ))";
private static final String PATTERN_KEY = "_pseudoPattern_";
private static final List<String> keys = Arrays.asList("_pseudoPattern_");
private static final String LANGUAGE = "zz";
private static final String DEFAULT_PATTERN = "zz USR_{string}_\u5c20";
private static final String DEFAULT_PAGE_PATTERN = "pg PAGE_{string}_\u5c20";
private static final String[] DEFAULT_PATTERNS = new String[]{"zz USR_{string}_\u5c20", "pg PAGE_{string}_\u5c20"};
@Property(value={"zz USR_{string}_\u5c20", "pg PAGE_{string}_\u5c20"})
private static final String PATTERNS = "pseudo.patterns";
private Map<Locale, PseudoBundle> bundles;
public Locale getDefaultLocale() {
return null;
}
public ResourceBundle getResourceBundle(Locale locale) {
if (locale != null && "zz".equals(locale.getLanguage().toLowerCase())) {
return this.bundles.get(locale);
}
return null;
}
public ResourceBundle getResourceBundle(String baseName, Locale locale) {
if (locale != null && "zz".equals(locale.getLanguage().toLowerCase())) {
return this.bundles.get(locale);
}
return null;
}
protected void activate(ComponentContext context) {
Dictionary props = context.getProperties();
String[] patterns = PropertiesUtil.toStringArray(props.get("pseudo.patterns"), (String[])DEFAULT_PATTERNS);
this.bundles = new HashMap<Locale, PseudoBundle>();
for (String pattern : patterns) {
int pos = pattern.indexOf(" ");
if (pos < 0) continue;
String country = pattern.substring(0, pos).toUpperCase();
pattern = pattern.substring(pos + 1);
this.bundles.put(new Locale("zz", country), new PseudoBundle(pattern));
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class PseudoBundle
extends ResourceBundle {
private String pattern;
public PseudoBundle(String pattern) {
this.pattern = pattern;
}
@Override
protected Object handleGetObject(String key) {
String comment;
String str;
if (key == null) {
return null;
}
if ("_pseudoPattern_".equals(key)) {
return this.pattern;
}
int pos = key.indexOf(" ((");
if (pos >= 0) {
str = key.substring(0, pos);
comment = key.substring(pos + " ((".length());
comment = StringUtils.removeEnd((String)comment, (String)" ))");
} else {
str = key;
comment = "";
}
try {
return this.pattern.replace("{string}", str).replace("{comment}", comment);
}
catch (Exception e) {
String msg = "invalid pseudo translation pattern: '" + this.pattern + "': " + e.getMessage();
log.error(msg);
return "ERROR: " + msg;
}
}
@Override
public Enumeration<String> getKeys() {
return Collections.enumeration(keys);
}
private String parseString(String key) {
int pos = key.indexOf(" ((");
if (pos >= 0) {
return key.substring(0, pos);
}
return key;
}
}
}