import java.io.LineNumberReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
+import java.util.Locale;
public class MTranslator extends MObject {
- protected String language = null;
+ protected Locale basicLocale = null;
protected LinkedHashMap<String, LinkedHashMap<String, String>> messages = new LinkedHashMap<String, LinkedHashMap<String, String>>();
/* */
- public MTranslator(String file, String language) throws MTranslationFileParsingTextException {
+ public MTranslator(String file, Locale basicLocale) throws MTranslationFileParsingTextException {
super();
//
- if ((null == language) || ("".equals(language))) {
- throw new IllegalArgumentException("Invalid 'language': null or empty.");
+ if (null == basicLocale) {
+ throw new IllegalArgumentException("Invalid 'basicLocale': null.");
}
//
- this.language = language;
+ this.basicLocale = basicLocale;
this.parseFile(file);
}
- /* Language */
+ /* Locale */
- public String getLanguage() {
- return this.language;
+ public Locale getBasicLocale() {
+ return this.basicLocale;
}
/* Strings management */
if (-1 == a) {
throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: string malformed: %s.", buffer.getLineNumber(), line));
}
- String language = line.substring(0, a).trim();
+ String localeRepresentation = line.substring(0, a).trim();
String translation = line.substring(a + 1).trim();
- if (this.getMessagesReference().get(message).containsKey(language)) {
- throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: duplicated translation for message: %s.", buffer.getLineNumber(), message));
+ if (this.getMessagesReference().get(message).containsKey(localeRepresentation)) {
+ throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: duplicated translation for locale: %s.", buffer.getLineNumber(), message));
}
- this.getMessagesReference().get(message).put(language, translation);
+ this.getMessagesReference().get(message).put(localeRepresentation, translation);
}
}
}
}
}
- public String getLenientTranslation(String message, String language) {
+ public String getLenientTranslation(String message, Locale locale) {
String translation = null;
try {
- translation = this.getTranslation(message, language, false);
+ translation = this.getTranslation(message, locale, false);
}
catch (MTranslationValueNotFoundTextException exception) { // cannot happen
}
return translation;
}
- public String getStrictTranslation(String message, String language) throws MTranslationValueNotFoundTextException {
- return this.getTranslation(message, language, true);
+ public String getStrictTranslation(String message, Locale locale) throws MTranslationValueNotFoundTextException {
+ return this.getTranslation(message, locale, true);
}
- protected String getTranslation(String message, String language, boolean strictMode) throws MTranslationValueNotFoundTextException {
+ protected String getTranslation(String message, Locale locale, boolean strictMode) throws MTranslationValueNotFoundTextException {
if ((null == message) || ("".equals(message))) {
throw new IllegalArgumentException("Invalid 'message': null or empty.");
}
- if ((null == language) || ("".equals(language))) {
- throw new IllegalArgumentException("Invalid 'language': null or empty.");
+ if (null == locale) {
+ throw new IllegalArgumentException("Invalid 'locale': null.");
}
//
if (!this.getMessagesReference().containsKey(message)) {
}
return message;
}
- if (this.getLanguage().equals(language)) {
+ if (this.getBasicLocale().equals(locale)) {
return message;
}
LinkedHashMap<String, String> messageTranslations = this.getMessagesReference().get(message);
- if (!messageTranslations.containsKey(language)) {
+ String localeRepresentation = locale.toString();
+ if (!messageTranslations.containsKey(localeRepresentation)) {
if (strictMode) {
- throw new MTranslationValueNotFoundTextException(String.format("Invalid 'language': %s: translation not available for message: %s.", language, message));
+ throw new MTranslationValueNotFoundTextException(String.format("Invalid 'locale': %s: translation not available for message: %s.", localeRepresentation, message));
}
- return message;
+ String localeFallbackRepresentation = locale.getLanguage();
+ if (!messageTranslations.containsKey(localeFallbackRepresentation)) {
+ return message;
+ }
+ return messageTranslations.get(localeFallbackRepresentation);
}
- return messageTranslations.get(language);
+ return messageTranslations.get(localeRepresentation);
}
}