~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/language/TGResourceBundle.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.system.language;
 
2
 
 
3
import java.io.IOException;
 
4
import java.net.URL;
 
5
import java.util.Enumeration;
 
6
import java.util.Locale;
 
7
import java.util.Properties;
 
8
 
 
9
public class TGResourceBundle {
 
10
        
 
11
        private Locale locale;
 
12
        private Properties properties;
 
13
        
 
14
        public TGResourceBundle(Locale locale, Properties properties){
 
15
                this.locale = locale;
 
16
                this.properties = properties;
 
17
        }
 
18
        
 
19
        public Locale getLocale() {
 
20
                return this.locale;
 
21
        }
 
22
        
 
23
        public void setLocale(Locale locale) {
 
24
                this.locale = locale;
 
25
        }
 
26
        
 
27
        public String getString(String key) {
 
28
                return this.properties.getProperty(key);
 
29
        }
 
30
        
 
31
        public static TGResourceBundle getBundle(String baseName, Locale locale,ClassLoader loader){
 
32
                Properties properties = new Properties();
 
33
                
 
34
                String bundleName = baseName.replace('.','/');
 
35
                String bundleExtension = ".properties";
 
36
                
 
37
                // load default
 
38
                TGResourceBundle.loadResources( (bundleName + bundleExtension ), properties, loader);
 
39
                
 
40
                // load language
 
41
                bundleName += "_";
 
42
                if(locale.getLanguage() != null && locale.getLanguage().length() > 0){
 
43
                        bundleName += locale.getLanguage();
 
44
                        TGResourceBundle.loadResources( (bundleName + bundleExtension ), properties, loader);
 
45
                }
 
46
                
 
47
                // load country
 
48
                bundleName += "_";
 
49
                if(locale.getCountry() != null && locale.getCountry().length() > 0){
 
50
                        bundleName += locale.getCountry();
 
51
                        TGResourceBundle.loadResources( (bundleName + bundleExtension ), properties, loader);
 
52
                }
 
53
                
 
54
                // load variant
 
55
                bundleName += "_";
 
56
                if(locale.getVariant() != null && locale.getVariant().length() > 0){
 
57
                        bundleName += locale.getVariant();
 
58
                        TGResourceBundle.loadResources( (bundleName + bundleExtension ), properties, loader);
 
59
                }
 
60
                
 
61
                return new TGResourceBundle(locale, properties);
 
62
        }
 
63
        
 
64
        private static void loadResources(String name, Properties p, ClassLoader loader){
 
65
                try {
 
66
                        Enumeration enumeration = loader.getResources(name);
 
67
                        while (enumeration.hasMoreElements()) {
 
68
                                URL url = (URL) enumeration.nextElement();
 
69
                                Properties properties = new Properties();
 
70
                                properties.load( url.openStream() );
 
71
                                p.putAll(properties);
 
72
                        }
 
73
                } catch (IOException e) {
 
74
                        e.printStackTrace();
 
75
                }
 
76
        }
 
77
}