-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTranslation.java
123 lines (111 loc) · 3.63 KB
/
Translation.java
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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.nls;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.TreeMap;
/**
* Describes a translated property.
* <p>
* Used by {@link Babelfish} to manage all translations available.
*/
public class Translation {
private boolean autocreated;
private boolean used;
private final String key;
private final Map<String, String> translationTable = new TreeMap<>();
/**
* Creates a new translation, containing all native language values for the given key.
*
* @param key the key for which the translations are held
*/
protected Translation(String key) {
this.key = key;
}
/**
* Returns <tt>true</tt> if the translation was auto created as it was missing in the .properties files.
*
* @return <tt>true</tt> if the translation is auto created, <tt>false</tt> otherwise
*/
public boolean isAutocreated() {
return autocreated;
}
/**
* Determines if this translation has been used at least once.
*
* @return <tt>true</tt> if the translation was used, <tt>false</tt> otherwise
*/
public boolean isUsed() {
return used;
}
/**
* Sets the the autocreated flag.
*
* @param autocreated determines if the translation was automatically created by the system (true).
*/
protected void setAutocreated(boolean autocreated) {
this.autocreated = autocreated;
}
/**
* Adds a translation for the given language.
*
* @param lang a two-letter language code for which the given translation should be used
* @param value the translation for the given language
* @return the previous translation stored for the given language or <tt>null</tt> if there was none present.
*/
public String addTranslation(String lang, String value) {
return translationTable.put(lang, value);
}
/**
* Returns the key for which the translation can be found
*
* @return the name of the key used to register the translation
*/
public String getKey() {
return key;
}
/**
* Returns the translation for the given language
*
* @param lang the language as two-letter code
* @param fallback the fallback language as two-letter code
* @return a translation in the requested language or the key if no translation was found
*/
public String translate(@Nonnull String lang, @Nullable String fallback) {
this.used = true;
String result = translationTable.get(lang);
if (result == null && fallback != null) {
result = translationTable.get(fallback);
}
if (result == null) {
return key;
}
return result;
}
/**
* Returns the translation for the given language
*
* @param lang the language as two-letter code
* @return a translation in the requested language or <tt>null</tt> if no translation was found
*/
public String translateWithoutFallback(String lang) {
this.used = true;
return translationTable.get(lang);
}
/**
* Determines if a translation for the given language is available
*
* @param lang the language as two-letter code
* @return <tt>true</tt> if a translation for the given language exists, <tt>false</tt> otherwise
*/
public boolean hasTranslation(String lang) {
this.used = true;
return translationTable.containsKey(lang);
}
}