From 271cecff5be63f6bc2c3d2c4cbeec85af9059fa7 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 12 Jun 2018 12:26:30 +0200 Subject: [PATCH] Initial Commit --- .classpath | 7 + .gitignore | 56 +++++ .project | 17 ++ MANIFEST.MF | 3 + org.hwo.i18n.jardesc | 16 ++ src/org/hwo/i18n/Messages.java | 256 +++++++++++++++++++++ src/org/hwo/i18n/Messages.properties | 6 + src/org/hwo/i18n/messages_en_US.properties | 6 + src/org/hwo/i18n/messages_sv_SE.properties | 6 + 9 files changed, 373 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 MANIFEST.MF create mode 100644 org.hwo.i18n.jardesc create mode 100755 src/org/hwo/i18n/Messages.java create mode 100755 src/org/hwo/i18n/Messages.properties create mode 100755 src/org/hwo/i18n/messages_en_US.properties create mode 100755 src/org/hwo/i18n/messages_sv_SE.properties diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..246b79c --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d74d4cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,56 @@ +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +RemoteSystemsTempFiles + +.DS_Store \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..bab1424 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + org.hwo.i18n + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/MANIFEST.MF b/MANIFEST.MF new file mode 100644 index 0000000..e3467bb --- /dev/null +++ b/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Sealed: true + diff --git a/org.hwo.i18n.jardesc b/org.hwo.i18n.jardesc new file mode 100644 index 0000000..11ca0f7 --- /dev/null +++ b/org.hwo.i18n.jardesc @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/org/hwo/i18n/Messages.java b/src/org/hwo/i18n/Messages.java new file mode 100755 index 0000000..037ef82 --- /dev/null +++ b/src/org/hwo/i18n/Messages.java @@ -0,0 +1,256 @@ +package org.hwo.i18n; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.Properties; +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; +import static bootstrapper.logging.LogLevel.*; +import static bootstrapper.logging.Logging.*; + + + +public class Messages { + + protected static Messages pInstance; + protected static List instanceList; + protected static Locale activeLocale; + + protected static List + resourceBundles; + protected static List + properties; + + protected String + BUNDLE_NAME = "org.hwo.i18n.messages"; + protected Class + BUNDLE_CLASS = null; + + + private ResourceBundle defaultResourceBundle; + private ResourceBundle localeResourceBundle; + private Properties missingKeys; + private String missingKeysFileName; + + protected Messages() { + BUNDLE_NAME = String.format("%s", getClass().getCanonicalName()); + initialize(); + } + + protected Messages(String bundleName) + { + BUNDLE_NAME = bundleName; + initialize(); + } + + public void saveMissingStrings() { + + if (missingKeys != null) + { + FileOutputStream fos; + try { + fos = new FileOutputStream(missingKeysFileName); + log("Writing missing strings to %s",missingKeysFileName); + + missingKeys.storeToXML(fos, "missing translations"); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void initialize() + { + log(DEBUG,this.getClass().getName() + ": Using Locale:" + activeLocale.getCountry() + " / " + activeLocale.getLanguage()); + + defaultResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME); + localeResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME,activeLocale); + + instanceList.add(this); + } + + public String getMissingKeysFileName() + { + return missingKeysFileName; + } + public void setMissingKeysFileName(String fileName) + { + if (missingKeys == null){ + enableMissingKeys(); + } + + missingKeysFileName = fileName; + try { + missingKeys.loadFromXML(new FileInputStream(fileName)); + } catch (Exception e){ + log(e); + } + } + + public void enableMissingKeys() + { + if (missingKeys == null) + missingKeys = new Properties(); + } + + public static String getString(String key) { + + for (ResourceBundle bundle: resourceBundles){ + if (bundle.containsKey(key)) + return bundle.getString(key); + } + + for (Messages messages: instanceList) + { + if (messages.hasKey(key)) + return messages.getInstanceString(key); + } + + for (Properties p: properties){ + if (p.containsKey(key)) + return p.getProperty(key); + } + + if (getInstance().missingKeys != null) + { + if (getInstance().missingKeys.containsKey(key)) + return getInstance().missingKeys.getProperty(key); + + getInstance().missingKeys.setProperty(key, key); + } + return key; + } + + public boolean hasKey(String key) + { + return localeResourceBundle.containsKey(key) | defaultResourceBundle.containsKey(key); + } + + protected String getInstanceString(String key) throws MissingResourceException + { + if (localeResourceBundle.containsKey(key)) + return localeResourceBundle.getString(key); + if (defaultResourceBundle.containsKey(key)) + return defaultResourceBundle.getString(key); + throw new MissingResourceException(BUNDLE_NAME, key, ""); + } + + public static Messages getInstance() + { + return pInstance; + } + + + private static String[] generateFileNamesForBase(Class clazz,Locale locale){ + + if (locale == null) + locale = activeLocale; + + return new String[]{ + String.format("%s.msg_%s_%s", clazz.getCanonicalName(), locale.getLanguage(), locale.getCountry()), + String.format("%s.msg_%s_%s", clazz.getSimpleName(), locale.getLanguage(), locale.getCountry()), + String.format("%s.msg_%s", clazz.getCanonicalName(), locale.getLanguage()), + String.format("%s.msg_%s", clazz.getSimpleName(), locale.getLanguage()), + String.format("%s.msg", clazz.getCanonicalName()), + String.format("%s.msg", clazz.getSimpleName()) + }; + } + + private static File[] possibleFilesFor(Class clazz){ + LinkedList files = new LinkedList<>(); + + for (String fn: generateFileNamesForBase(clazz, null)){ + File f = new File(fn); + if (f.exists()) + files.add(f); + } + + return files.toArray(new File[0]); + } + + + private static ResourceBundle tryLoad(InputStream is){ + try { + return new PropertyResourceBundle(is); + } catch (Exception e){ + log(e); + } + + return null; + } + + public static boolean loadMessages(Class clazz){ +// boolean leastOne = false; +// String[] fileNames = generateFileNamesForBase(clazz, null); + + try { + ResourceBundle bundle = ResourceBundle.getBundle(clazz.getCanonicalName(),activeLocale); + if (bundle != null) { + resourceBundles.add(bundle); + log(DEBUG,"ResourceBundle found for %s",clazz.getCanonicalName()); + return true; + } + } catch (RuntimeException ex) { + log(ex); + } + return false; + +// for (String fn: fileNames){ +// InputStream is = clazz.getClassLoader().getResourceAsStream(fn); +// if (is == null){ +// if (new File(fn).exists()) +// try { +// is = new FileInputStream(fn); +// } catch (FileNotFoundException e) { +// log(e); +// } +// } +// if (is != null){ +// ResourceBundle rb = tryLoad(is); +// if (rb != null){ +// log(INFO,"I18N: loaded %s",fn); +// resourceBundles.add(rb); +// continue; +// } +// } +// +// Properties prop = new Properties(); +// try { +// String xfn = String.format("%s.xml", fn); +// prop.loadFromXML(new FileInputStream(xfn)); +// properties.add(prop); +// log(INFO,"I18N: loaded %s",xfn); +// continue; +// } catch (Exception e) { +// } +// +// log(INFO,"I18N: does not exist: %s",fn); +// } +// +// return leastOne; + } + + static { + resourceBundles = new ArrayList<>(); + properties = new ArrayList<>(); + + if (activeLocale == null) + activeLocale = Locale.getDefault(); + + if (instanceList == null) + instanceList = new LinkedList(); + + if (pInstance == null) + pInstance = new Messages(); + } + +} diff --git a/src/org/hwo/i18n/Messages.properties b/src/org/hwo/i18n/Messages.properties new file mode 100755 index 0000000..f2b863a --- /dev/null +++ b/src/org/hwo/i18n/Messages.properties @@ -0,0 +1,6 @@ +org.hwo=HWOs Java Framework +interface=Schnittstelle +ok=OK +cancel=abbrechen +SerialPortChooser.0=Schnittstelle w\u00E4hlen + diff --git a/src/org/hwo/i18n/messages_en_US.properties b/src/org/hwo/i18n/messages_en_US.properties new file mode 100755 index 0000000..0c940a9 --- /dev/null +++ b/src/org/hwo/i18n/messages_en_US.properties @@ -0,0 +1,6 @@ +org.hwo=HWOs Java Framework +interface=Interface +ok=OK +cancel=Cancel +SerialPortChooser.0=Select Interface + diff --git a/src/org/hwo/i18n/messages_sv_SE.properties b/src/org/hwo/i18n/messages_sv_SE.properties new file mode 100755 index 0000000..87576ce --- /dev/null +++ b/src/org/hwo/i18n/messages_sv_SE.properties @@ -0,0 +1,6 @@ +org.hwo=HWOs Java Framework +interface=Gränssnitt +ok=OK +cancel=avbryta +SerialPortChooser.0=Välj granssnitt: +