From: Marco Zanon Date: Wed, 28 Mar 2012 17:42:41 +0000 (+0000) Subject: Replaced String languages with Locales. X-Git-Tag: SVN-to-Git~144 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=5cfdf357b19471e0da3f61ccc4e997dbea2d3690;p=Macaco Replaced String languages with Locales. --- diff --git a/src/java/com/marcozanon/macaco/text/MTranslator.java b/src/java/com/marcozanon/macaco/text/MTranslator.java index b7af0cb..06ee12a 100644 --- a/src/java/com/marcozanon/macaco/text/MTranslator.java +++ b/src/java/com/marcozanon/macaco/text/MTranslator.java @@ -15,30 +15,31 @@ import java.io.IOException; 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> messages = new LinkedHashMap>(); /* */ - 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 */ @@ -91,12 +92,12 @@ public class MTranslator extends MObject { 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); } } } @@ -114,26 +115,26 @@ public class MTranslator extends MObject { } } - 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)) { @@ -142,17 +143,22 @@ public class MTranslator extends MObject { } return message; } - if (this.getLanguage().equals(language)) { + if (this.getBasicLocale().equals(locale)) { return message; } LinkedHashMap 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); } }