3535 */
3636public class Language {
3737 static private final String FILE = "processing.app.languages.PDE" ;
38- static private final String LISTING = "processing/app/languages/languages.txt" ;
39- static protected final String PREF = "language" ;
38+ //static private final String LISTING = "processing/app/languages/languages.txt";
39+
40+ // Store the language information in a file separate from the preferences,
41+ // because preferences need the language on load time.
42+ static protected final String PREF_FILE = "language.txt" ;
43+ static protected final File prefFile = Base .getSettingsFile (PREF_FILE );
4044
4145 /** Single instance of this Language class */
4246 static private Language instance = null ;
@@ -52,7 +56,7 @@ public class Language {
5256
5357 private Language () {
5458 String systemLanguage = Locale .getDefault ().getLanguage ();
55- String language = Preferences . get ( PREF );
59+ language = loadLanguage ( );
5660 boolean writePrefs = false ;
5761
5862 if (language == null ) {
@@ -72,44 +76,31 @@ private Language() {
7276 writePrefs = true ;
7377 }
7478
75- // // Get saved language
76- // try {
77- // File file = Base.getSettingsFile("language.txt");
78- // if (file.exists()) {
79- // String language = PApplet.loadStrings(file)[0];
80- // language = language.trim().toLowerCase();
81- // if (!language.equals("")) {
82- // this.language = language;
83- // } else {
84- // Base.saveFile(this.language, file);
85- // }
86- // }
87- // } catch (Exception e) {
88- // e.printStackTrace();
89- // }
90-
9179 if (writePrefs ) {
92- Preferences . save ( );
80+ saveLanguage ( language );
9381 }
9482
9583 // Get bundle with translations (processing.app.language.PDE)
9684 bundle = ResourceBundle .getBundle (Language .FILE , new Locale (this .language ), new UTF8Control ());
9785 }
9886
9987
100- String [] listSupported () {
101- // // Old list of languages in alphabetical order.
102- // final String[] SUPPORTED = {
103- // "de", // de, German, Deutsch
104- // "en", // en, English, English
105- // "el", // el, Greek
106- // "es", // es, Spanish
107- // "fr", // fr, French, Français, Langue française
108- // "ja", // ja, Japanese
109- // "nl", // nl, Dutch, Nederlands
110- // "pt", // pt, Portuguese
111- // };
112-
88+ static private String [] listSupported () {
89+ // List of languages in alphabetical order.
90+ final String [] SUPPORTED = {
91+ "de" , // de, German, Deutsch
92+ "en" , // en, English, English
93+ "el" , // el, Greek
94+ "es" , // es, Spanish
95+ "fr" , // fr, French, Français, Langue française
96+ "ja" , // ja, Japanese
97+ "nl" , // nl, Dutch, Nederlands
98+ "pt" , // pt, Portuguese
99+ };
100+ return SUPPORTED ;
101+
102+ /*
103+ // come back to this when bundles are placed outside the JAR
113104 InputStream input = getClass().getResourceAsStream(LISTING);
114105 String[] lines = PApplet.loadStrings(input);
115106 ArrayList<String> list = new ArrayList<String>();
@@ -122,9 +113,40 @@ String[] listSupported() {
122113 list.add(line);
123114 }
124115 return list.toArray(new String[0]);
116+ */
125117 }
126118
127119
120+ /** Read the saved language */
121+ static private String loadLanguage () {
122+ try {
123+ if (prefFile .exists ()) {
124+ String language = PApplet .loadStrings (prefFile )[0 ];
125+ language = language .trim ().toLowerCase ();
126+ if (!language .equals ("" )) {
127+ return language ;
128+ }
129+ }
130+ } catch (Exception e ) {
131+ e .printStackTrace ();
132+ }
133+ return null ;
134+ }
135+
136+
137+ /**
138+ * Save the language directly to a settings file. This is 'save' and not
139+ * 'set' because a language change requires a restart of Processing.
140+ */
141+ static public void saveLanguage (String language ) {
142+ try {
143+ Base .saveFile (language , prefFile );
144+ } catch (Exception e ) {
145+ e .printStackTrace ();
146+ }
147+ }
148+
149+
128150 /** Singleton constructor */
129151 static public synchronized Language init () {
130152 if (instance == null ) {
@@ -152,8 +174,10 @@ static public String getLanguage() {
152174 }
153175
154176
155- // /** Set new language */
177+ // /** Set new language (called by Preferences) */
156178// static public void setLanguage(String language) {
179+ // this.language = language;
180+ //
157181// try {
158182// File file = Base.getContentFile("lib/language.txt");
159183// Base.saveFile(language, file);
0 commit comments