Changeset 4159 in josm for trunk/src


Ignore:
Timestamp:
2011-06-23T17:51:29+02:00 (13 years ago)
Author:
stoecker
Message:

load translation files also from plugins

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r4137 r4159  
    6161import org.openstreetmap.josm.tools.CheckParameterUtil;
    6262import org.openstreetmap.josm.tools.GBC;
     63import org.openstreetmap.josm.tools.I18n;
    6364import org.openstreetmap.josm.tools.ImageProvider;
    6465
     
    448449            allPluginLibraries.addAll(info.libraries);
    449450            File pluginJar = new File(pluginDir, info.name + ".jar");
     451            I18n.addTexts(pluginJar);
    450452            URL pluginJarUrl = PluginInformation.fileToURL(pluginJar);
    451453            allPluginLibraries.add(pluginJarUrl);
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r4144 r4159  
    33
    44import java.io.BufferedInputStream;
     5import java.io.File;
     6import java.io.FileInputStream;
    57import java.io.InputStream;
     8import java.io.IOException;
    69import java.net.URL;
    710import java.text.MessageFormat;
     
    912import java.util.Comparator;
    1013import java.util.HashMap;
     14import java.util.jar.JarInputStream;
     15import java.util.zip.ZipEntry;
    1116import java.util.Locale;
    1217import java.util.Vector;
     
    2732        MODE_CS, MODE_AR, MODE_PL/*, MODE_RO*/, MODE_RU, MODE_SK/*, MODE_SL*/}
    2833    private static PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
     34    private static String loadedCode = "en";
    2935
    3036    /* Localization keys for file chooser (and color chooser). */
     
    340346    }
    341347
     348    public static void addTexts(File source)
     349    {
     350        FileInputStream fis = null;
     351        JarInputStream jar = null;
     352        FileInputStream fisTrans = null;
     353        JarInputStream jarTrans = null;
     354        String enfile = "data/en.lang";
     355        String langfile = "data/"+loadedCode+".lang";
     356        try
     357        {
     358            ZipEntry e;
     359            fis = new FileInputStream(source);
     360            jar = new JarInputStream(fis);
     361            boolean found = false;
     362            while(!found && (e = jar.getNextEntry()) != null)
     363            {
     364                String name = e.getName();
     365                if(name.equals(enfile))
     366                    found = true;
     367            }
     368            if(found)
     369            {
     370                fisTrans = new FileInputStream(source);
     371                jarTrans = new JarInputStream(fisTrans);
     372                found = false;
     373                while(!found && (e = jarTrans.getNextEntry()) != null)
     374                {
     375                    String name = e.getName();
     376                    if(name.equals(langfile))
     377                        found = true;
     378                }
     379                if(found)
     380                    load(jar, jarTrans, true);
     381            }
     382        }
     383        catch(IOException e)
     384        {
     385        }
     386        finally
     387        {
     388            try
     389            {
     390                if(jar != null)
     391                    jar.close();
     392                if(fis != null)
     393                    fis.close();
     394                if(jarTrans != null)
     395                    jarTrans.close();
     396                if(fisTrans != null)
     397                    fisTrans.close();
     398            }
     399            catch(IOException e)
     400            {
     401            }
     402        }
     403    }
     404
    342405    private static boolean load(String l)
    343406    {
     
    353416            return false;
    354417        URL tr = Main.class.getResource("/data/"+l+".lang");
    355         if(tr == null)
     418        if(tr == null || !languages.containsKey(l))
    356419        {
    357420            int i = l.indexOf('_');
     
    360423            }
    361424            tr = Main.class.getResource("/data/"+l+".lang");
    362             if(tr == null)
     425            if(tr == null || !languages.containsKey(l))
    363426                return false;
    364427        }
    365 
    366         HashMap<String, String> s = new HashMap<String, String>();
    367         HashMap<String, String[]> p = new HashMap<String, String[]>();
     428        try
     429        {
     430            if(load(en.openStream(), tr.openStream(), false))
     431            {
     432                pluralMode = languages.get(l);
     433                loadedCode = l;
     434                return true;
     435            }
     436        }
     437        catch(IOException e)
     438        {
     439        }
     440        return false;
     441    }
     442
     443    private static boolean load(InputStream en, InputStream tr, boolean add)
     444    {
     445        HashMap<String, String> s;
     446        HashMap<String, String[]> p;
     447        if(add)
     448        {
     449            s = strings;
     450            p = pstrings;
     451        }
     452        else
     453        {
     454            s = new HashMap<String, String>();
     455            p = new HashMap<String, String[]>();
     456        }
    368457        /* file format:
    369458           for all single strings:
     
    383472        try
    384473        {
    385             InputStream ens = new BufferedInputStream(en.openStream());
    386             InputStream trs = new BufferedInputStream(tr.openStream());
     474            InputStream ens = new BufferedInputStream(en);
     475            InputStream trs = new BufferedInputStream(tr);
    387476            byte[] enlen = new byte[2];
    388477            byte[] trlen = new byte[2];
     
    430519                        trstrings[i] = new String(str, 0, val, "utf-8");
    431520                    }
    432                     if(trnum > 0) {
     521                    if(trnum > 0 && !p.containsKey(enstrings[0])) {
    433522                        p.put(enstrings[0], trstrings);
    434523                    }
     
    471560                                return false;
    472561                            String trstr = new String(str, 0, trval, "utf-8");
    473                             s.put(enstr, trstr);
     562                            if(!s.containsKey(enstr))
     563                                s.put(enstr, trstr);
    474564                        }
    475565                    }
     
    477567            }
    478568        }
    479         catch(Exception e)
     569        catch(IOException e)
    480570        {
    481571            return false;
    482572        }
    483         if(!s.isEmpty() && languages.containsKey(l))
     573        if(!s.isEmpty())
    484574        {
    485575            strings = s;
    486576            pstrings = p;
    487             pluralMode = languages.get(l);
    488577            return true;
    489578        }
Note: See TracChangeset for help on using the changeset viewer.