1 /*
2  * by Shigeru KANEMOTO at SWITCHSCIENCE.
3  *
4  * Extract strings to be translated by:
5  *   % xgettext -L Java --from-code=utf-8 -k_ -d Resources_ja *.java
6  * Extract and merge by:
7  *   % xgettext -j -L Java --from-code=utf-8 -k_ -d Resources_ja *.java
8  *
9  * Edit "Resources_ja.po".
10  * Convert to the properties file format by:
11  *   % msgcat -p Resources_ja.po > Resources_ja.properties
12  */
13 
14 package processing.app;
15 
16 import java.text.MessageFormat;
17 import java.util.Locale;
18 import java.util.MissingResourceException;
19 import java.util.ResourceBundle;
20 
21 public class I18n {
22 
23   static {
24     tr("Arduino");
25     tr("Partner");
26     tr("Recommended");
27     tr("Contributed");
28     tr("Retired");
29 
30     tr("Display");
31     tr("Communication");
32     tr("Signal Input/Output");
33     tr("Sensors");
34     tr("Device Control");
35     tr("Timing");
36     tr("Data Storage");
37     tr("Data Processing");
38     tr("Other");
39     tr("Uncategorized");
40   }
41 
42   // start using current locale but still allow using the dropdown list later
43   private static ResourceBundle i18n;
44 
45   // prompt text stuff
46 
47   public static String PROMPT_YES;
48   public static String PROMPT_NO;
49   public static String PROMPT_CANCEL;
50   public static String PROMPT_OK;
51   public static String PROMPT_BROWSE;
52 
init(String language)53   static protected void init(String language) throws MissingResourceException {
54     String[] languageParts = language.split("_");
55     Locale locale = Locale.getDefault();
56     // both language and country
57     if (languageParts.length == 2) {
58       locale = new Locale(languageParts[0], languageParts[1]);
59       // just language
60     } else if (languageParts.length == 1 && !"".equals(languageParts[0])) {
61       locale = new Locale(languageParts[0]);
62     }
63     // there might be a null pointer exception ... most likely will never happen but the jvm gets mad
64     Locale.setDefault(locale);
65     i18n = ResourceBundle.getBundle("processing.app.i18n.Resources", Locale.getDefault());
66     PROMPT_YES = tr("Yes");
67     PROMPT_NO = tr("No");
68     PROMPT_CANCEL = tr("Cancel");
69     PROMPT_OK = tr("OK");
70     PROMPT_BROWSE = tr("Browse");
71   }
72 
tr(String s)73   public static String tr(String s) {
74     String res;
75     try {
76       if (i18n == null)
77         res = s;
78       else
79         res = i18n.getString(s);
80     } catch (MissingResourceException e) {
81       res = s;
82     }
83 
84     // The single % is the arguments selector in .PO files.
85     // We must put double %% inside the translations to avoid
86     // getting .PO processing in the way.
87     res = res.replace("%%", "%");
88 
89     return res;
90   }
91 
format(String fmt, Object... args)92   public static String format(String fmt, Object... args) {
93     // Single quote is used to escape curly bracket arguments.
94 
95     // - Prevents strings fixed at translation time to be fixed again
96     fmt = fmt.replace("''", "'");
97     // - Replace ' with the escaped version ''
98     fmt = fmt.replace("'", "''");
99 
100     return MessageFormat.format(fmt, args);
101   }
102 
103   /**
104    * Does nothing.
105    * <p>
106    * This method is an hack to extract words with gettext tool.
107    */
unusedStrings()108   protected static void unusedStrings() {
109     // These phrases are defined in the "platform.txt".
110     tr("Arduino AVR Boards");
111     tr("Arduino ARM (32-bits) Boards");
112 
113     // This word is defined in the "boards.txt".
114     tr("Processor");
115   }
116 }
117