KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > Language


1 package SnowMailClient.Language;
2
3 import snow.utils.storage.FileUtils;
4
5 import java.io.*;
6 import java.util.*;
7 import javax.swing.JOptionPane JavaDoc;
8
9 /** Used to translate whole sentences appearing in the GUI.
10    no intelligence here. The whole sentences must all be translated
11    by a human beeing like you.
12    All argument sentences are english sentences. They are normally
13    available as french or deutsch translations.
14
15    Spaces at beginning and at end of phrases are ignored in the translations,
16    they are generated at runtime.
17    example Translate(" Hello ") will translate "Hello" to "Bonjour" and
18     return " Bonjour ".
19
20    MAIN USAGE:
21
22     + simply call Language.TranslatE("Welcome in SnowMail") for every strings
23     + there is a version with arguments.
24         for a single argument, the string must contain a single %.
25         for two arguments, the string must contain %1 and %2.
26
27 */

28 public final class Language
29 {
30   public static final String JavaDoc ENGLISH = "English";
31   public static final String JavaDoc FRANCAIS = "Francais";
32   public static final String JavaDoc DEUTSCH = "Deutsch";
33   public static final String JavaDoc POLISH = "Polish";
34
35   //private static final String PropertyFile = "SnowMailClient/Language/Languages.properties";
36
private static final String JavaDoc PropertyFile = "Languages.properties";
37
38   //Data (not static)
39
private SentenceDictionary dictionary = null;
40   private String JavaDoc actualLanguage = ENGLISH; // default => no translation
41
// this is the default
42
private boolean readFromJar = true;
43
44   // must be set prior to the first getInstance() !
45
public static String JavaDoc baseDirectory = ".";
46
47   // singleton
48
private Language() { }
49   private static Language languageInstance = null;
50
51   public Locale getLocale()
52   {
53     if(actualLanguage==null) return Locale.ENGLISH;
54     if(actualLanguage.equals(ENGLISH)) return Locale.ENGLISH;
55     if(actualLanguage.equals(FRANCAIS)) return Locale.FRENCH;
56     if(actualLanguage.equals(DEUTSCH)) return Locale.GERMAN;
57     // default
58
return Locale.ENGLISH;
59   }
60
61   private static String JavaDoc defaultLanguage = ENGLISH;
62   public static void setDefaultLanguage(String JavaDoc language)
63   {
64      defaultLanguage = language;
65   }
66
67   /** get the single instance of this class
68   */

69   public static Language getInstance()
70   {
71     if(languageInstance==null)
72     {
73       // create a new instance and initialize it
74
languageInstance = new Language();
75
76       // used to read active language
77
Properties prop = new Properties();
78       File propFile = new File(baseDirectory, PropertyFile);
79       System.out.println("Read language props in "+propFile+" ( base="+baseDirectory+")");
80       if(propFile.exists())
81       {
82         FileInputStream fis = null;
83         try
84         {
85           prop.load(fis = new FileInputStream(propFile));
86         }
87         catch(Exception JavaDoc e)
88         {
89           e.printStackTrace(); // int a = 012;
90
}
91         finally
92         {
93           if(fis!=null) try{ fis.close();} catch(Exception JavaDoc ee){}
94         }
95       }
96
97       String JavaDoc lang = prop.getProperty("ActiveTranslationLanguage", defaultLanguage);
98       // // so that it behaves as with Schmortopf version <= 1.2
99
// per default, try to read language pack from jar first
100
String JavaDoc readFromJarProp = prop.getProperty("ReadFromJar", "True");
101
102       languageInstance.readFromJar = readFromJarProp.equals("True");
103       languageInstance.setActualTranslation(lang, languageInstance.readFromJar);
104
105       System.out.println("Read ini lang = "+lang);
106
107     }
108     return languageInstance;
109   }
110
111   /** return a list of the languages that are present in the jar file
112     usually {French, deutsch}
113   */

114   public String JavaDoc[] getAvailableInternalLanguages()
115   {
116     return SentenceDictionary.getInternalAvailableTranslations();
117   }
118
119   public String JavaDoc[] getAvailableLanguages()
120   {
121     Vector<String JavaDoc> found = new Vector<String JavaDoc>();
122     found.addElement(ENGLISH); // Always present, but never as file...
123

124     File base = new File("SnowMailClient/Language");
125
126     if(base.isDirectory() && base.exists())
127     {
128       File[] files = base.listFiles();
129       for(int i=0; i<files.length; i++)
130       {
131         int pos = files[i].getName().toLowerCase().indexOf(".translation");
132         if(pos!=-1)
133         {
134           found.addElement(files[i].getName().substring(0,pos));
135         }
136       }
137     }
138     return found.toArray(new String JavaDoc[found.size()]);
139   }
140
141   public boolean getActualTranslationWasReadFromJarFile() { return readFromJar; }
142
143   public String JavaDoc getActualTranslation() {return actualLanguage;}
144
145
146   /** set the language used from now to translate sentences
147       and load the appropriate dictionary
148
149       @param fromJarFile read it from jar file if true
150   */

151   public void setActualTranslation(String JavaDoc language, boolean fromJarFile)
152   {
153
154
155     actualLanguage = language;
156     this.readFromJar = fromJarFile;
157
158     // save properties
159
saveProperties();
160
161
162     if(actualLanguage.compareToIgnoreCase("english")==0)
163     {
164       dictionary = null;
165     }
166     else
167     {
168       try
169       {
170         if(fromJarFile)
171         {
172           dictionary = SentenceDictionary.readFromJarFile(language);
173           if(dictionary==null)
174           {
175             dictionary = SentenceDictionary.readFromFile(language, true); // try to read embedded
176
}
177           //System.out.println("Language "+language+" read from jar file");
178
}
179         else
180         {
181           dictionary = SentenceDictionary.readFromFile(language, true); // try to read embedded
182
System.out.println("Language "+language+" read from file");
183         }
184         //null if not found
185
}
186       catch(Exception JavaDoc e)
187       {
188         // occur in case of bad version
189
JOptionPane.showMessageDialog(null, ""+e.getMessage()
190           +"\nDelete the language file in Language/ or use a language file\ncompatible with the current Schmortopf version.", "Language pack error",
191           JOptionPane.ERROR_MESSAGE);
192       }
193     }
194   }
195
196
197   /** @return the ref of the actual dic of read it if it is not already loaded
198    CAUTION: for english: return null becacuse english has no translation on english...
199   */

200   public SentenceDictionary getDictionaryFromFile(String JavaDoc language, boolean useEmbeddedInNotFound )
201   {
202     if(actualLanguage.equals(language)
203      && readFromJar == false) return dictionary;
204
205     try
206     {
207       SentenceDictionary dic = SentenceDictionary.readFromFile(language, useEmbeddedInNotFound);
208       return dic;
209     }
210     catch(Exception JavaDoc e)
211     {
212       // not found...
213
e.printStackTrace();
214       return null;
215     }
216   }
217
218
219   /** NO MORE NECESSARY, repaced with save properties
220       Dictionaries are only stored from the language editor
221   */

222   public void save()
223   {
224   }
225
226
227   /** save the actual settings and the sentence dictionary
228       if not read from jar file
229   */

230   private void saveProperties()
231   {
232      // save properties
233
//
234
String JavaDoc a = "";
235
236
237
238
239      Properties prop = new Properties();
240      prop.setProperty("ActiveTranslationLanguage", actualLanguage);
241      prop.setProperty("ReadFromJar", (readFromJar?"True":"False"));
242
243      File propFile = new File(baseDirectory, PropertyFile);
244      System.out.println("Save language props in "+propFile);
245      FileOutputStream fos = null;
246      try
247      {
248        if(!propFile.getParentFile().exists())
249        {
250          propFile.getParentFile().mkdirs();
251        }
252        fos = new FileOutputStream(propFile);
253        prop.store(fos, "Schmortopf Language Settings");
254      }
255      catch(Exception JavaDoc e)
256      {
257        e.printStackTrace();
258      }
259      finally
260      {
261         FileUtils.closeIgnoringExceptions(fos);
262      }
263
264
265      /*
266      // Save sentences
267      //
268
269      // DO NOT save english
270      if(actualLanguage.compareToIgnoreCase("english")==0) return;
271
272      // DO NOT SAVE when read in jar file
273      if(!readFromJar )
274      {
275         // needs an Language directory...
276         File path = new File("Language");
277         if(!path.exists()) path.mkdirs();
278
279         if(dictionary!=null)
280         {
281           try
282           {
283             dictionary.saveToFile();
284           }
285           catch(Exception e)
286           {
287             e.printStackTrace();
288           }
289         }
290
291      }
292
293      */

294
295
296   }
297
298
299   /** @return the sentence in the actual language.
300       @param sentence is directly search in the english database
301         and translated
302       THIS IS THE BASIC TRANSLATION METHOD
303
304       if not found, return the original string, but add
305       a request for later translation
306   */

307   public static String JavaDoc translate(String JavaDoc sentence)
308   {
309      if(getInstance().actualLanguage.compareToIgnoreCase("english")==0) return sentence;
310      if(getInstance().dictionary!=null)
311      {
312         return getInstance().dictionary.getTranslatedSentence(sentence);
313      }
314      else
315      {
316         return sentence;
317      }
318   }
319
320
321   /** @param _sentenceWithArgument the arg is a % somewhere in text that is replaced
322         by the arg (typical example: a fileName)
323   */

324   public static String JavaDoc translate(String JavaDoc _sentenceWithArgument, String JavaDoc arg)
325   {
326     String JavaDoc sentenceWithArgument = translate(_sentenceWithArgument);
327
328     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sentenceWithArgument);
329     int pos = sentenceWithArgument.indexOf("%");
330     if(pos==-1)
331     {
332       // allow us to correct this
333
System.out.println("***************************************************");
334       System.out.println("Invalid sentence for translation (should have a % for the replacement)");
335       System.out.println(" sentence = "+_sentenceWithArgument);
336       System.out.println(" arg = "+ arg);
337       System.out.println("***************************************************");
338
339       return sentenceWithArgument+" "+arg;
340     }
341
342     return sb.replace(pos,pos+1, arg).toString();
343   }
344
345   /** @param _sentenceWithArgument the args are %1 and %2 somewhere in text that is replaced
346         by the arg (typical example: a fileName)
347   */

348   public static String JavaDoc translate(String JavaDoc _sentenceWithArgument, String JavaDoc arg1, String JavaDoc arg2)
349   {
350     String JavaDoc sentenceWithArgument = translate(_sentenceWithArgument);
351     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sentenceWithArgument);
352
353     // first argument replacement
354
int pos1 = sentenceWithArgument.indexOf("%1");
355     if(pos1==-1)
356     {
357       System.out.println("***************************************************");
358       System.out.println("Invalid sentence for translation (should have a %1 for the first replacement)");
359       System.out.println(" sentence = "+_sentenceWithArgument);
360       System.out.println(" arg1 = "+ arg1);
361       System.out.println(" arg2 = "+ arg2);
362       System.out.println("***************************************************");
363
364       return sentenceWithArgument+" "+arg1+" "+arg2;
365     }
366
367     sb = sb.replace(pos1,pos1+2, arg1);
368
369     // second argument replacement
370
sentenceWithArgument = sb.toString();
371     int pos2 = sentenceWithArgument.indexOf("%2");
372     if(pos2==-1)
373     {
374       System.out.println("***************************************************");
375       System.out.println("Invalid sentence for translation (should have a %2 for the first replacement)");
376       System.out.println(" sentence = "+_sentenceWithArgument);
377       System.out.println(" arg1 = "+ arg1);
378       System.out.println(" arg2 = "+ arg2);
379       System.out.println("***************************************************");
380
381       return sb.toString()+" "+arg2;
382     }
383
384     sb = sb.replace(pos2,pos2+2, arg2);
385
386     return sb.toString();
387   }
388
389   /** @param _sentenceWithArgument the args are %1 and %2 somewhere in text that is replaced
390         by the arg (typical example: a fileName)
391   */

392   public static String JavaDoc translate(String JavaDoc _sentenceWithArgument, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3)
393   {
394     String JavaDoc sentenceWithArgument = translate(_sentenceWithArgument);
395     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sentenceWithArgument);
396
397     // first argument replacement
398
int pos1 = sentenceWithArgument.indexOf("%1");
399     if(pos1==-1)
400     {
401       System.out.println("***************************************************");
402       System.out.println("Invalid sentence for translation (should have a %1 for the first replacement)");
403       System.out.println(" sentence = "+_sentenceWithArgument);
404       System.out.println(" arg1 = "+ arg1);
405       System.out.println(" arg2 = "+ arg2);
406       System.out.println("***************************************************");
407
408       return sentenceWithArgument+" "+arg1+" "+arg2;
409     }
410
411     sb = sb.replace(pos1,pos1+2, arg1);
412
413     // second argument replacement
414
sentenceWithArgument = sb.toString();
415     int pos2 = sentenceWithArgument.indexOf("%2");
416     if(pos2==-1)
417     {
418       System.out.println("***************************************************");
419       System.out.println("Invalid sentence for translation (should have a %2 for the second replacement)");
420       System.out.println(" sentence = "+_sentenceWithArgument);
421       System.out.println(" arg1 = "+ arg1);
422       System.out.println(" arg2 = "+ arg2);
423       System.out.println(" arg3 = "+ arg3);
424       System.out.println("***************************************************");
425
426       return sb.toString()+" "+arg2;
427     }
428
429     sb = sb.replace(pos2,pos2+2, arg2);
430
431     // third argument replacement
432
sentenceWithArgument = sb.toString();
433     int pos3 = sentenceWithArgument.indexOf("%3");
434     if(pos3==-1)
435     {
436       System.out.println("***************************************************");
437       System.out.println("Invalid sentence for translation (should have a %3 for the third replacement)");
438       System.out.println(" sentence = "+_sentenceWithArgument);
439       System.out.println(" arg1 = "+ arg1);
440       System.out.println(" arg2 = "+ arg2);
441       System.out.println(" arg3 = "+ arg3);
442       System.out.println("***************************************************");
443
444       return sb.toString()+" "+arg3;
445     }
446
447     sb = sb.replace(pos3, pos3+2, arg3);
448
449     return sb.toString();
450   }
451
452 } // Language
Popular Tags