1+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+ /*
4+ Part of the Processing project - http://processing.org
5+
6+ Copyright (c) 2014 The Processing Foundation
7+
8+ This program is free software; you can redistribute it and/or
9+ modify it under the terms of the GNU General Public License
10+ version 2, as published by the Free Software Foundation.
11+
12+ This program is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+
17+ You should have received a copy of the GNU General Public License
18+ along with this program; if not, write to the Free Software Foundation,
19+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+ */
21+
122package processing .app ;
223
3- import java .io .File ;
4- import java .io .IOException ;
5- import java .io .InputStream ;
6- import java .io .InputStreamReader ;
24+ import java .io .*;
725import java .net .URL ;
826import java .net .URLConnection ;
9- import java .util .HashMap ;
10- import java .util .Locale ;
11- import java .util .PropertyResourceBundle ;
12- import java .util .ResourceBundle ;
27+ import java .util .*;
1328
1429import processing .core .PApplet ;
1530
31+
1632/**
1733 * Internationalization (i18n)
18- *
1934 * @author Darius Morawiec
2035 */
2136public class Language {
22-
23- private static Language instance = null ;
24- private String language ;
25- private HashMap <String , String > languages ;
26- private ResourceBundle bundle ;
27- private static final String FILE = "processing.app.languages.PDE" ;
28-
29- private Language () {
30-
31- // Get system language
32- this .language = Locale .getDefault ().getLanguage ();
33-
34- // Set available languages
35- this .languages = new HashMap <String , String >();
36-
37- // Language code:
38- // http://en.wikipedia.org/wiki/ISO_639-1
39- // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
40-
41- // en, English, English
42- this .languages .put (Locale .ENGLISH .getLanguage (), Locale .ENGLISH .getDisplayLanguage (Locale .ENGLISH ));
43- // de, German, Deutsch
44- this .languages .put (Locale .GERMAN .getLanguage (), Locale .GERMAN .getDisplayLanguage (Locale .GERMAN ));
45- // ja, Japanese
46- this .languages .put (Locale .JAPANESE .getLanguage (), Locale .JAPANESE .getDisplayLanguage (Locale .JAPANESE ));
47- // es, Spanish
48- this .languages .put ("es" , Locale .forLanguageTag ("es" ).getDisplayLanguage (Locale .forLanguageTag ("es" )));
49-
50- // Set default language
51- if (!this .languages .containsKey (this .language )) {
52- this .language = "en" ;
53- }
54-
55- // Get saved language
56- try {
57- File file = Base .getContentFile ("lib/language.txt" );
58- if (file .exists ()) {
59- String language = PApplet .loadStrings (file )[0 ];
60- language = language .trim ().toLowerCase ();
61- if (!language .equals ("" )) {
62- this .language = language ;
63- } else {
64- Base .saveFile (this .language , file );
65- }
66- }
67- } catch (Exception e ) {
68- e .printStackTrace ();
69- }
70-
71- // Get bundle with translations (processing.app.language.PDE)
72- this .bundle = ResourceBundle .getBundle (Language .FILE , new Locale (this .language ), new UTF8Control ());
73- }
74-
75- /**
76- * Singleton constructor
77- *
78- * @return
79- */
80- public static synchronized Language init () {
81- if (instance == null ) {
82- instance = new Language ();
83- }
84- return instance ;
85- }
86-
87- /**
88- * Get translation from bundles.
89- *
90- * @param text
91- * @return
92- */
93- public static String text (String text ) {
94- return init ().bundle .getString (text );
95- }
96-
97- /**
98- * Get all available languages
99- *
100- * @return
101- */
102- public static HashMap <String , String > getLanguages () {
103- return init ().languages ;
104- }
105-
106- /**
107- * Get current language
108- *
109- * @return
110- */
111- public static String getLanguage () {
112- return init ().language ;
113- }
114-
115- /**
116- * Set new language
117- *
118- * @param language
119- */
120- public static void setLanguage (String language ) {
121- try {
122- File file = Base .getContentFile ("lib/language.txt" );
123- Base .saveFile (language , file );
124- } catch (Exception e ) {
125- e .printStackTrace ();
126- }
127- }
128-
129- /**
130- * Custom Control class for consitent encoding.
131- * http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
132- */
133- public class UTF8Control extends ResourceBundle .Control {
134- public ResourceBundle newBundle (String baseName , Locale locale , String format , ClassLoader loader , boolean reload ) throws IllegalAccessException , InstantiationException ,IOException {
135- // The below is a copy of the default implementation.
136- String bundleName = toBundleName (baseName , locale );
137- String resourceName = toResourceName (bundleName , "properties" );
138- ResourceBundle bundle = null ;
139- InputStream stream = null ;
140- if (reload ) {
141- URL url = loader .getResource (resourceName );
142- if (url != null ) {
143- URLConnection connection = url .openConnection ();
144- if (connection != null ) {
145- connection .setUseCaches (false );
146- stream = connection .getInputStream ();
147- }
148- }
149- } else {
150- stream = loader .getResourceAsStream (resourceName );
151- }
152- if (stream != null ) {
153- try {
154- // Only this line is changed to make it to read properties
155- // files as UTF-8.
156- bundle = new PropertyResourceBundle (new InputStreamReader (stream , "UTF-8" ));
157- } finally {
158- stream .close ();
159- }
160- }
161- return bundle ;
162- }
163- }
164-
165- }
37+ static private Language instance = null ;
38+ private String language ;
39+ private HashMap <String , String > languages ;
40+ private ResourceBundle bundle ;
41+ static private final String FILE = "processing.app.languages.PDE" ;
42+
43+
44+ private Language () {
45+ // Get system language
46+ this .language = Locale .getDefault ().getLanguage ();
47+
48+ // Set available languages
49+ this .languages = new HashMap <String , String >();
50+
51+ // Language code:
52+ // http://en.wikipedia.org/wiki/ISO_639-1
53+ // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
54+
55+ // en, English, English
56+ this .languages .put (Locale .ENGLISH .getLanguage (), Locale .ENGLISH .getDisplayLanguage (Locale .ENGLISH ));
57+ // de, German, Deutsch
58+ this .languages .put (Locale .GERMAN .getLanguage (), Locale .GERMAN .getDisplayLanguage (Locale .GERMAN ));
59+ // ja, Japanese
60+ this .languages .put (Locale .JAPANESE .getLanguage (), Locale .JAPANESE .getDisplayLanguage (Locale .JAPANESE ));
61+ // es, Spanish
62+ this .languages .put ("es" , Locale .forLanguageTag ("es" ).getDisplayLanguage (Locale .forLanguageTag ("es" )));
63+
64+ // Set default language
65+ if (!this .languages .containsKey (this .language )) {
66+ this .language = "en" ;
67+ }
68+
69+ // Get saved language
70+ try {
71+ File file = Base .getContentFile ("lib/language.txt" );
72+ if (file .exists ()) {
73+ String language = PApplet .loadStrings (file )[0 ];
74+ language = language .trim ().toLowerCase ();
75+ if (!language .equals ("" )) {
76+ this .language = language ;
77+ } else {
78+ Base .saveFile (this .language , file );
79+ }
80+ }
81+ } catch (Exception e ) {
82+ e .printStackTrace ();
83+ }
84+
85+ // Get bundle with translations (processing.app.language.PDE)
86+ this .bundle = ResourceBundle .getBundle (Language .FILE , new Locale (this .language ), new UTF8Control ());
87+ }
88+
89+
90+ /** Singleton constructor */
91+ static public synchronized Language init () {
92+ if (instance == null ) {
93+ instance = new Language ();
94+ }
95+ return instance ;
96+ }
97+
98+
99+ /** Get translation from bundles. */
100+ static public String text (String text ) {
101+ return init ().bundle .getString (text );
102+ }
103+
104+
105+ /** Get all available languages */
106+ static public Map <String , String > getLanguages () {
107+ return init ().languages ;
108+ }
109+
110+
111+ /** Get current language */
112+ static public String getLanguage () {
113+ return init ().language ;
114+ }
115+
116+
117+ /** Set new language */
118+ static public void setLanguage (String language ) {
119+ try {
120+ File file = Base .getContentFile ("lib/language.txt" );
121+ Base .saveFile (language , file );
122+ } catch (Exception e ) {
123+ e .printStackTrace ();
124+ }
125+ }
126+
127+
128+ /**
129+ * Custom Control class for consitent encoding.
130+ * http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
131+ */
132+ class UTF8Control extends ResourceBundle .Control {
133+ public ResourceBundle newBundle (String baseName , Locale locale , String format , ClassLoader loader , boolean reload ) throws IllegalAccessException , InstantiationException ,IOException {
134+ // The below is a copy of the default implementation.
135+ String bundleName = toBundleName (baseName , locale );
136+ String resourceName = toResourceName (bundleName , "properties" );
137+ ResourceBundle bundle = null ;
138+ InputStream stream = null ;
139+ if (reload ) {
140+ URL url = loader .getResource (resourceName );
141+ if (url != null ) {
142+ URLConnection connection = url .openConnection ();
143+ if (connection != null ) {
144+ connection .setUseCaches (false );
145+ stream = connection .getInputStream ();
146+ }
147+ }
148+ } else {
149+ stream = loader .getResourceAsStream (resourceName );
150+ }
151+ if (stream != null ) {
152+ try {
153+ // Only this line is changed to make it to read properties
154+ // files as UTF-8.
155+ bundle = new PropertyResourceBundle (new InputStreamReader (stream , "UTF-8" ));
156+ } finally {
157+ stream .close ();
158+ }
159+ }
160+ return bundle ;
161+ }
162+ }
163+ }
0 commit comments