001: package Language;
002:
003: import java.io.*;
004: import java.util.*;
005: import javax.swing.JOptionPane;
006:
007: /** used to translate whole sentences appearing in the GUI.
008: no intelligence here. The whole sentences must all be translated
009: by a human beeing like you.
010: All argument sentences are english sentences. They are normally
011: available as french or german translations.
012:
013: Spaces at beginning and at end of phrases are ignored in the translations,
014: they are generated at runtime.
015: example Translate(" Hello ") will translate "Hello" to "Bonjour" and
016: return " Bonjour ".
017:
018: MAIN USAGE:
019:
020: + simply call Language.Translate("Welcome in Schmortopf") for every strings
021: + there is a version with arguments.
022: for a single argument, the string must contain a single %.
023: for two arguments, the string must contain %1 and %2.
024:
025: */
026: public class Language {
027: public static final String ENGLISH = "English";
028: public static final String FRANCAIS = "French";//"Français";
029: public static final String DEUTSCH = "German";//"Deutsch";
030: private static final String PropertyFile = "Language/Languages.properties";
031:
032: //Data (not static)
033: private SentenceDictionary dictionary = null;
034: private String actualLanguage = ENGLISH; // default => no translation
035: // this is the default
036: private boolean readFromJar = true;
037:
038: // singleton
039: private Language() {
040: } // Constructor
041:
042: private static Language LanguageInstance = null;
043:
044: private static String defaultLanguage = ENGLISH;
045:
046: public static void SetDefaultLanguage(String language) {
047: defaultLanguage = language;
048: }
049:
050: /** get the single instance of this class
051: */
052: public static Language GetInstance() {
053: if (LanguageInstance == null) {
054: // create a new instance and initialize it
055: LanguageInstance = new Language();
056:
057: // used to read active language
058: Properties prop = new Properties();
059: File propFile = new File(PropertyFile);
060: if (propFile.exists()) {
061: FileInputStream fis = null;
062: try {
063: prop.load(fis = new FileInputStream(propFile));
064: } catch (Exception e) {
065: e.printStackTrace();
066: } finally {
067: if (fis != null)
068: try {
069: fis.close();
070: } catch (Exception ee) {
071: }
072: }
073: }
074:
075: String lang = prop.getProperty("ActiveTranslationLanguage",
076: defaultLanguage);
077: // // so that it behaves as with Schmortopf version <= 1.2
078: // per default, try to read language pack from jar first
079: String readFromJarProp = prop.getProperty("ReadFromJar",
080: "True");
081:
082: LanguageInstance.readFromJar = readFromJarProp
083: .equals("True");
084: LanguageInstance.setActualTranslation(lang,
085: LanguageInstance.readFromJar);
086:
087: //System.out.println("Read ini lang = "+lang);
088:
089: }
090: return LanguageInstance;
091: }
092:
093: /** return a list of the languages that are present in the jar file
094: usually {French, German}
095: */
096: public String[] GetAvailableInternalLanguages() {
097: return SentenceDictionary.getInternalAvailableTranslations();
098: }
099:
100: public String[] GetAvailableLanguages() {
101: Vector found = new Vector();
102: found.addElement(ENGLISH); // Always present, but never as file...
103:
104: File base = new File("Language");
105: if (base.isDirectory() && base.exists()) {
106: File[] files = base.listFiles();
107: for (int i = 0; i < files.length; i++) {
108: if (files[i].getName().toLowerCase().endsWith(
109: ".translation")) {
110: int pos = files[i].getName().toLowerCase().indexOf(
111: ".translation");
112: if (pos != -1) {
113: found.addElement(files[i].getName().substring(
114: 0, pos));
115: }
116: }
117: }
118: }
119: return (String[]) found.toArray(new String[found.size()]);
120: }
121:
122: public boolean GetActualTranslationWasReadFromJarFile() {
123: return readFromJar;
124: }
125:
126: public String GetActualTranslation() {
127: return actualLanguage;
128: }
129:
130: /** set the language used from now to translate sentences
131: and load the appropriate dictionary
132:
133: @param fromJarFile read it from jar file if true
134: */
135: public void setActualTranslation(String language,
136: boolean fromJarFile) {
137: /* if(actualLanguage.equals(language)
138: && readFromJar == fromJarFile) return;
139: */
140:
141: actualLanguage = language;
142: this .readFromJar = fromJarFile;
143:
144: // save properties
145: saveProperties();
146:
147: if (actualLanguage.compareToIgnoreCase("english") == 0) {
148: dictionary = null;
149: } else {
150: try {
151: if (fromJarFile) {
152: System.out.println("Language " + language
153: + " read from jar file");
154: dictionary = SentenceDictionary
155: .ReadFromJarFile(language);
156: } else {
157: System.out.println("Language " + language
158: + " read from file");
159: dictionary = SentenceDictionary.ReadFromFile(
160: language, true); // try to read embedded
161: }
162: //null if not found
163: } catch (Exception e) {
164: // occur in case of bad version
165: JOptionPane
166: .showMessageDialog(
167: null,
168: ""
169: + e.getMessage()
170: + "\nDelete the language file in Language/ or use a language file\ncompatible with the current Schmortopf version.",
171: "Language pack error",
172: JOptionPane.ERROR_MESSAGE);
173: }
174: }
175: }
176:
177: /** @return the ref of the actual dic of read it if it is not already loaded
178: CAUTION: for english: return null becacuse english has no translation on english...
179: */
180: public SentenceDictionary getDictionaryFromFile(String language,
181: boolean useEmbeddedInNotFound) {
182: if (actualLanguage.equals(language) && readFromJar == false)
183: return dictionary;
184:
185: try {
186: SentenceDictionary dic = SentenceDictionary.ReadFromFile(
187: language, useEmbeddedInNotFound);
188: return dic;
189: } catch (Exception e) {
190: // not found...
191: e.printStackTrace();
192: return null;
193: }
194: }
195:
196: /** save the actual settings and the sentence dictionary
197: if not read from jar file
198: */
199: private void saveProperties() {
200: // save properties
201: //
202:
203: Properties prop = new Properties();
204: prop.setProperty("ActiveTranslationLanguage", actualLanguage);
205: prop.setProperty("ReadFromJar",
206: (readFromJar ? "True" : "False"));
207:
208: File propFile = new File(PropertyFile);
209: FileOutputStream fos = null;
210: try {
211: if (!propFile.getParentFile().exists()) {
212: propFile.getParentFile().mkdirs();
213: }
214: prop.store(fos = new FileOutputStream(propFile),
215: "Schmortopf Language Settings");
216: } catch (Exception e) {
217: e.printStackTrace();
218: } finally {
219: if (fos != null)
220: try {
221: fos.close();
222: } catch (Exception ee) {
223: }
224: }
225:
226: /*
227: // Save sentences
228: //
229:
230: // DO NOT save english
231: if(actualLanguage.compareToIgnoreCase("english")==0) return;
232:
233: // DO NOT SAVE when read in jar file
234: if(!readFromJar )
235: {
236: // needs an Language directory...
237: File path = new File("Language");
238: if(!path.exists()) path.mkdirs();
239:
240: if(dictionary!=null)
241: {
242: try
243: {
244: dictionary.saveToFile();
245: }
246: catch(Exception e)
247: {
248: e.printStackTrace();
249: }
250: }
251:
252: }
253:
254: */
255:
256: }
257:
258: /** @return the sentence in the actual language.
259: @param sentence is directly search in the english database
260: and translated
261: THIS IS THE BASIC TRANSLATION METHOD
262:
263: if not found, return the original string, but add
264: a request for later translation
265: */
266: public static String Translate(String sentence) {
267: if (GetInstance().actualLanguage.compareToIgnoreCase("english") == 0)
268: return sentence;
269: if (GetInstance().dictionary != null) {
270: return GetInstance().dictionary
271: .getTranslatedSentence(sentence);
272: } else {
273: return sentence;
274: }
275: }
276:
277: /** @param sentenceWithArgument the arg is a % somewhere in text that is replaced
278: by the arg (typical example: a fileName)
279: */
280: public static String Translate(String _sentenceWithArgument,
281: String arg) {
282: String sentenceWithArgument = Translate(_sentenceWithArgument);
283:
284: StringBuffer sb = new StringBuffer(sentenceWithArgument);
285: int pos = sentenceWithArgument.indexOf("%");
286: if (pos == -1) {
287: // allow us to correct this
288: System.out
289: .println("***************************************************");
290: System.out
291: .println("Invalid sentence for translation (should have a % for the replacement)");
292: System.out.println(" sentence = " + _sentenceWithArgument);
293: System.out.println(" arg = " + arg);
294: System.out
295: .println("***************************************************");
296:
297: return sentenceWithArgument + " " + arg;
298: }
299:
300: return sb.replace(pos, pos + 1, arg).toString();
301: }
302:
303: /** @param sentenceWithArgument the args are %1 and %2 somewhere in text that is replaced
304: by the arg (typical example: a fileName)
305: */
306: public static String Translate(String _sentenceWithArgument,
307: String arg1, String arg2) {
308: String sentenceWithArgument = Translate(_sentenceWithArgument);
309: StringBuffer sb = new StringBuffer(sentenceWithArgument);
310:
311: // first argument replacement
312: int pos1 = sentenceWithArgument.indexOf("%1");
313: if (pos1 == -1) {
314: System.out
315: .println("***************************************************");
316: System.out
317: .println("Invalid sentence for translation (should have a %1 for the first replacement)");
318: System.out.println(" sentence = " + _sentenceWithArgument);
319: System.out.println(" arg1 = " + arg1);
320: System.out.println(" arg2 = " + arg2);
321: System.out
322: .println("***************************************************");
323:
324: return sentenceWithArgument + " " + arg1 + " " + arg2;
325: }
326:
327: sb = sb.replace(pos1, pos1 + 2, arg1);
328:
329: // second argument replacement
330: sentenceWithArgument = sb.toString();
331: int pos2 = sentenceWithArgument.indexOf("%2");
332: if (pos2 == -1) {
333: System.out
334: .println("***************************************************");
335: System.out
336: .println("Invalid sentence for translation (should have a %2 for the first replacement)");
337: System.out.println(" sentence = " + _sentenceWithArgument);
338: System.out.println(" arg1 = " + arg1);
339: System.out.println(" arg2 = " + arg2);
340: System.out
341: .println("***************************************************");
342:
343: return sb.toString() + " " + arg2;
344: }
345:
346: sb = sb.replace(pos2, pos2 + 2, arg2);
347:
348: return sb.toString();
349: }
350:
351: } // Language
|