Commits

P_W999 committed 7422f1d

- allow user to change language of error messages

  • Participants
  • Parent commits 147311d
  • Tags v0.0.4

Comments (0)

Files changed (2)

File src/be/pw/jexif/DemoApp.java

 			LOG.error(e1.getMessage());
 		}
 		tool.stop();
-		IMessageConveyor mc = new MessageConveyor(Locale.ENGLISH);
+		/*IMessageConveyor mc = new MessageConveyor(Locale.ENGLISH);
 		System.out.println(mc.getMessage(Errors.GENERAL, "test"));
 
 		mc = new MessageConveyor(new Locale("nl"));
 		System.out.println(mc.getMessage(Errors.GENERAL, "test"));
 
-		System.out.println(Cal10nUtil.get(Errors.GENERAL, "test"));
+		System.out.println(Cal10nUtil.get(Errors.GENERAL, "test"));*/
+		
+		
 
 		JExifInfo gpsInfo = tool.getInfo(gps);
 		// 50.84791165546716N 4.350469368264016E == Bourse @ Brussels
 		gpsInfo.setGPSInfo("60 35 13.08", "S", "131 50 9.38", "W", "100", "1");
 		tool.stop();
 
+		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
+		Cal10nUtil.changeLocale(Locale.ENGLISH);
+		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
+		Cal10nUtil.changeLocale(new Locale("nl"));
+		System.out.println(Cal10nUtil.get(Errors.VALIDATION_RAT_FOR_INT));
+		
 		LOG.info("Executing took {} ms", watch.elapsedTime(TimeUnit.MILLISECONDS));
 		LOG.info("Finished main method");
 	}

File src/be/pw/jexif/internal/util/Cal10nUtil.java

 package be.pw.jexif.internal.util;
 
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
 import be.pw.jexif.enums.Errors;
 import ch.qos.cal10n.IMessageConveyor;
 import ch.qos.cal10n.MessageConveyor;
  * @author phillip
  */
 public final class Cal10nUtil {
+	
+	/**
+	 * Logger for this class.
+	 */
+	private static final Logger LOG = LoggerFactory.getLogger(Cal10nUtil.class);
+	
 	/**
 	 * The application wide MessageConveyor.
 	 */
-	private static final IMessageConveyor mc = new MessageConveyor(Locale.ENGLISH); // TODO: allow this to be changed on the fly
-
+	private static IMessageConveyor mc;
+	/**
+	 * List containing the supported Languages.
+	 */
+	private static final List<Locale> supportedLanguages;
+	
+	static {
+		supportedLanguages = new ArrayList<>(2);
+		supportedLanguages.add(Locale.ENGLISH);
+		supportedLanguages.add(new Locale("nl"));
+		Locale l = Locale.getDefault();
+		if (!supportedLanguages.contains(l)) {
+			LOG.info(MessageFormat.format("Default locale {0} was not supported, falling back to english", l));
+			l = Locale.ENGLISH;
+		}
+		mc = new MessageConveyor(l);
+	}
+	
+	public static void changeLocale(Locale l) {
+		Preconditions.checkNotNull(l, "Provided locale can not be null");
+		if (!supportedLanguages.contains(l)) {
+			LOG.info(MessageFormat.format("The locale {0} was not supported, falling back to english", l));
+			l = Locale.ENGLISH;
+		}
+		mc = new MessageConveyor(l);
+	}
+	
 	/**
 	 * Returns an i18n'ed error message.
 	 * @param error the error-message.
 			return b.toString();
 		}
 	}
+	
+	/**
+	 * Returns a list of supported languages.
+	 * @return
+	 */
+	public static final List<Locale> getSupportedLanguages() {
+		return supportedLanguages;
+	}
 }