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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/language/LanguageManager.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
/*
 
2
 * Created on 09-ene-2006
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.system.language;
 
8
 
 
9
import java.io.File;
 
10
import java.io.FilenameFilter;
 
11
import java.text.MessageFormat;
 
12
import java.util.ArrayList;
 
13
import java.util.Iterator;
 
14
import java.util.List;
 
15
import java.util.Locale;
 
16
 
 
17
import org.herac.tuxguitar.gui.util.TGFileUtils;
 
18
import org.herac.tuxguitar.util.TGClassLoader;
 
19
 
 
20
/**
 
21
 * @author julian
 
22
 * 
 
23
 */
 
24
public class LanguageManager {
 
25
        
 
26
        public static final String PACKAGE = "lang";
 
27
        public static final String PREFIX = "messages";
 
28
        public static final String EXTENSION = ".properties";
 
29
        
 
30
        private TGResourceBundle resources;
 
31
        private String[] languages;
 
32
        private List loaders;
 
33
        
 
34
        public LanguageManager() {
 
35
                this.loaders = new ArrayList();
 
36
                this.loadLanguages();
 
37
        }
 
38
        
 
39
        public void addLoader(LanguageLoader loader){
 
40
                this.loaders.add(loader);
 
41
        }
 
42
        
 
43
        public void removeLoader(LanguageLoader loader){
 
44
                this.loaders.remove(loader);
 
45
        }
 
46
        
 
47
        private void fireChanges(){
 
48
                Iterator it = this.loaders.iterator();
 
49
                while(it.hasNext()){
 
50
                        LanguageLoader loader = (LanguageLoader)it.next();
 
51
                        loader.loadProperties();
 
52
                }
 
53
        }
 
54
        
 
55
        public void setLanguage(String lang) {
 
56
                try {
 
57
                        String baseName = (PACKAGE + "." + PREFIX);
 
58
                        Locale locale = getLocale(lang);
 
59
                        this.resources = TGResourceBundle.getBundle(baseName, locale, TGClassLoader.instance().getClassLoader());
 
60
                        this.fireChanges();
 
61
                } catch (Exception e) {
 
62
                        e.printStackTrace();
 
63
                }
 
64
        }
 
65
        
 
66
        private Locale getLocale(String lang){
 
67
                if(this.isSupportedLanguage(lang)){
 
68
                        String[] locale = lang.split("_");
 
69
                        String language = (locale.length > 0 ? locale[0] : "" );
 
70
                        String country =  (locale.length > 1 ? locale[1] : "" );
 
71
                        String variant =  (locale.length > 2 ? locale[2] : "" );
 
72
                        return new Locale(language, country, variant);
 
73
                }
 
74
                return Locale.getDefault();
 
75
        }
 
76
        
 
77
        private boolean isSupportedLanguage(String lang){
 
78
                if(lang != null && lang.length() > 0 && this.languages != null){
 
79
                        for(int i = 0 ; i < this.languages.length; i ++){
 
80
                                if(this.languages[i].equals(lang)){
 
81
                                        return true;
 
82
                                }
 
83
                        }
 
84
                }
 
85
                return false;
 
86
        }
 
87
        
 
88
        public String getProperty(String key,String value) {
 
89
                try {
 
90
                        String property = this.resources.getString(key);
 
91
                        return (property == null ? value : property );
 
92
                }catch(Throwable throwable){
 
93
                        return value;
 
94
                }
 
95
        }
 
96
        
 
97
        public String getProperty(String key) {
 
98
                return this.getProperty(key,key);
 
99
        }
 
100
        
 
101
        public String getProperty(String key, Object[] arguments) {
 
102
                return getProperty(key,key,arguments);
 
103
        }
 
104
        
 
105
        public String getProperty(String key,String value, Object[] arguments) {
 
106
                String property = this.getProperty(key,value);
 
107
                return ( arguments != null ? MessageFormat.format(property, arguments) : property );
 
108
        }
 
109
        
 
110
        public String[] getLanguages() {
 
111
                return this.languages;
 
112
        }
 
113
        
 
114
        public String getLanguage() {
 
115
                if(this.resources != null){
 
116
                        Locale locale = this.resources.getLocale();
 
117
                        boolean language = (locale.getLanguage() != null && locale.getLanguage().length() > 0);
 
118
                        boolean country = (locale.getCountry() != null && locale.getCountry().length() > 0);
 
119
                        boolean variant = (locale.getVariant() != null && locale.getVariant().length() > 0);
 
120
                        
 
121
                        String localeId = new String();
 
122
                        if( language ){
 
123
                                localeId += locale.getLanguage();
 
124
                        }
 
125
                        if( country ){
 
126
                                localeId += "_" + locale.getCountry();
 
127
                        }
 
128
                        if( variant ){
 
129
                                localeId += "_" + ( country ? locale.getVariant() : ("_" + locale.getVariant()) );
 
130
                        }
 
131
                        return localeId;
 
132
                }
 
133
                return null;
 
134
        }
 
135
        
 
136
        /**
 
137
         * Load language files from lang folder
 
138
         *
 
139
         */
 
140
        private void loadLanguages(){
 
141
                // we need .properties files only
 
142
                final FilenameFilter filter = new FilenameFilter() {
 
143
                        public boolean accept(File dir, String name) {
 
144
                                int prefixIndex = name.indexOf(PREFIX + "_");
 
145
                                int extensionIndex = name.indexOf(EXTENSION);
 
146
                                return (prefixIndex == 0 && extensionIndex > (PREFIX + "_").length());
 
147
                        }
 
148
                };
 
149
                // get the files
 
150
                String langFolderPath = TGFileUtils.PATH_LANGUAGE;
 
151
                if(langFolderPath != null){
 
152
                        File langFolder = new File(langFolderPath);
 
153
                        if(langFolder.exists() && langFolder.isDirectory()){
 
154
                                String[] fileNames = langFolder.list(filter);
 
155
                                this.languages = new String[fileNames.length];
 
156
                                // now iterate over them
 
157
                                for(int i = 0;i < fileNames.length;i++){
 
158
                                        if (fileNames[i].indexOf("messages_") == 0){
 
159
                                                //this.languages[i] = fileNames[i].substring(9,11);
 
160
                                                
 
161
                                                int prefixIndex = fileNames[i].indexOf(PREFIX + "_");
 
162
                                                int extensionIndex = fileNames[i].indexOf(EXTENSION);
 
163
                                                if(prefixIndex == 0 && extensionIndex > (PREFIX + "_").length()){
 
164
                                                        this.languages[i] = fileNames[i].substring( (PREFIX + "_").length() , extensionIndex );
 
165
                                                }
 
166
                                        }
 
167
                                }
 
168
                        }
 
169
                }
 
170
        }
 
171
}
 
 
b'\\ No newline at end of file'