Replaced String languages with Locales.
authorMarco Zanon <info@marcozanon.com>
Wed, 28 Mar 2012 17:42:41 +0000 (17:42 +0000)
committerMarco Zanon <info@marcozanon.com>
Wed, 28 Mar 2012 17:42:41 +0000 (17:42 +0000)
src/java/com/marcozanon/macaco/text/MTranslator.java

index b7af0cbbc04bfd7c313e021cfffc05c285942597..06ee12a44e113097a254db7a5ada6d1b8d10b026 100644 (file)
@@ -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<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 */
@@ -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<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);
     }
 
 }