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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CharMatcher.JAVA_UPPER_CASE;
import static com.google.common.base.CharMatcher.anyOf;
import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Iterables.transform;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.google.common.base.Converter;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class I18nKeyImpl
implements I18nKey {
// TODO : Implement thread safe cache
public final static String NOT_FOUND_PREFIX = "!";
public final static String KEY_PREFIX = "[";
public final static String KEY_SUFFIX = "]";
public final static String PATH_SEPARATOR = ".";
private final static String SHORT_PATH = "short";
private final static String DESC_PATH = "desc";
private final static String EMPTY_PATH = "empty";
private final static String ERROR_PATH = "error";
private final static Converter<String, String> converter = UPPER_CAMEL.converterTo(LOWER_CAMEL);
private final static Function<String, String> toLowerCamel = new Function<String, String>() {
@Override
public String apply(String input) {
if (input == null)
return null;
else if ((JAVA_UPPER_CASE.countIn(input) + anyOf("_").countIn(input)) == input.length())
return input;
else
return converter.convert(input);
}
};
private String path;
private String module;
private String object;
private String group;
private String property;
private String value;
public I18nKey setPath(String path) {
this.path = path;
return this;
}
public I18nKey setModule(String module) {
this.module = module;
return this;
}
public I18nKey setObject(String object) {
this.object = object;
return this;
}
public I18nKey setGroup(String group) {
this.group = group;
return this;
}
public I18nKey setProperty(String property) {
this.property = property;
return this;
}
public I18nKey setValue(String value) {
this.value = value;
return this;
}
/**
* @return
*/
private String getPath() {
if (this.path != null)
return this.path;
else
return on(".").skipNulls().join(this.module, this.object, this.group, this.property, this.value);
}
/**
* Get the standard translation key.
*
* @return the string to convert
*/
public String getKey() {
return build(null);
}
/**
* Get the SHORT translation key.<br>
* <br>
* Generaly used into table headers or in minimal forms.
*
* @return the string to convert
*/
public String getShortKey() {
return build(SHORT_PATH);
}
/**
* Get the DESCRIPTION translation key.<br>
* <br>
* Generaly used to describe the thing. For example into tootips.
*
* @return the string to convert
*/
public String getDescriptionKey() {
return build(DESC_PATH);
}
/**
* Get the EMPTY translation key.<br>
* <br>
* Used to indicate how the object must be filled.
*
* @return the string to convert
*/
public String getEmptyKey() {
return build(EMPTY_PATH);
}
/**
* Get the ERROR translation key.<br>
* <br>
* Used to indicate that the value is incorrect
*
* @return the string to convert
*/
public String getErrorKey() {
return build(ERROR_PATH);
}
/**
* Internal method to encapsulate the key with the generic Square Brackets.
*
* @param path
* @return the string to convert
*/
public String build(final String suffix) {
String path = getPath();
if (Strings.emptyToNull(suffix) != null)
path += PATH_SEPARATOR + suffix;
StringBuffer key = new StringBuffer(KEY_PREFIX);
key.append(Joiner.on(PATH_SEPARATOR)
.join(transform(Splitter.on(PATH_SEPARATOR).split(path.toString()), toLowerCamel)));
key.append(KEY_SUFFIX);
return key.toString();
}
@Override
public String toString() {
return getKey();
}
} |
Partager