Check for the localized changelog to show
authorNiki Roo <niki@nikiroo.be>
Thu, 14 Dec 2017 12:02:42 +0000 (13:02 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 14 Dec 2017 12:02:42 +0000 (13:02 +0100)
src/be/nikiroo/fanfix/VersionCheck.java
src/be/nikiroo/fanfix/bundles/StringIdBundle.java

index 62ee814071b734b1fadff3f90d5b5b746b6dae1e..00450623cb3dea5603c77ae076eac2c9c8b7f0e9 100644 (file)
@@ -8,6 +8,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import be.nikiroo.utils.Version;
@@ -19,7 +20,7 @@ import be.nikiroo.utils.Version;
  * @author niki
  */
 public class VersionCheck {
-       private static final String url = "https://github.com/nikiroo/fanfix/raw/master/changelog.md";
+       private static final String base = "https://github.com/nikiroo/fanfix/raw/master/changelog${LANG}.md";
 
        private Version current;
        private List<Version> newer;
@@ -108,7 +109,27 @@ public class VersionCheck {
 
                if (Instance.isVersionCheckNeeded()) {
                        try {
-                               InputStream in = Instance.getCache().openNoCache(new URL(url));
+                               // Prepare the URLs according to the user's language
+                               Locale lang = Instance.getTrans().getLanguage();
+                               String fr = lang.getLanguage();
+                               String BE = lang.getCountry().replace(".UTF8", "");
+                               String urlFrBE = base.replace("${LANG}", "-" + fr + "_" + BE);
+                               String urlFr = base.replace("${LANG}", "-" + fr);
+                               String urlDefault = base.replace("${LANG}", "");
+
+                               InputStream in = null;
+                               for (String url : new String[] { urlFrBE, urlFr, urlDefault }) {
+                                       try {
+                                               in = Instance.getCache().openNoCache(new URL(url));
+                                               break;
+                                       } catch (IOException e) {
+                                       }
+                               }
+
+                               if (in == null) {
+                                       throw new IOException("No changelog found");
+                               }
+
                                BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(in, "UTF-8"));
                                try {
@@ -140,8 +161,10 @@ public class VersionCheck {
                                        reader.close();
                                }
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(new IOException(
-                                               "Cannot download latest changelist on github.com", e));
+                               Instance.getTraceHandler()
+                                               .error(new IOException(
+                                                               "Cannot download latest changelist on github.com",
+                                                               e));
                        }
                }
 
index e94c05b448eb3ba3231035f9999ec4dbb22d40b7..24a9252d17a3fefafb2f430d69ad0d50e61e412a 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.fanfix.bundles;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Locale;
 
 import be.nikiroo.utils.resources.TransBundle;
 
@@ -11,6 +12,8 @@ import be.nikiroo.utils.resources.TransBundle;
  * @author niki
  */
 public class StringIdBundle extends TransBundle<StringId> {
+       private String lang;
+
        /**
         * Create a translation service for the given language (will fall back to
         * the default one i not found).
@@ -20,6 +23,19 @@ public class StringIdBundle extends TransBundle<StringId> {
         */
        public StringIdBundle(String lang) {
                super(StringId.class, Target.resources, lang);
+               this.lang = lang;
+       }
+
+       /**
+        * Return the currently used language as a String.
+        * 
+        * @return the language
+        * 
+        * @deprecated use the call from {@link TransBundle} when available
+        */
+       public Locale getLanguage() {
+               return getLocaleFor(lang);
+
        }
 
        /**
@@ -37,4 +53,39 @@ public class StringIdBundle extends TransBundle<StringId> {
                new StringIdBundle(null).updateFile(path);
                System.out.println("Path updated: " + path);
        }
+
+       /**
+        * Return the {@link Locale} representing the given language.
+        * 
+        * @param language
+        *            the language to initialise, in the form "en-GB" or "fr" for
+        *            instance
+        * 
+        * @return the corresponding {@link Locale} or the default {@link Locale} if
+        *         it is not known
+        * 
+        * @deprecated Use the call from {@link TransBundle} when available.
+        */
+       static private Locale getLocaleFor(String language) {
+               Locale locale;
+
+               if (language == null) {
+                       locale = Locale.getDefault();
+               } else {
+                       language = language.replaceAll("_", "-");
+                       String lang = language;
+                       String country = null;
+                       if (language.contains("-")) {
+                               lang = language.split("-")[0];
+                               country = language.split("-")[1];
+                       }
+
+                       if (country != null)
+                               locale = new Locale(lang, country);
+                       else
+                               locale = new Locale(lang);
+               }
+
+               return locale;
+       }
 }