summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/Java-experiments/Multilingual/host/src/org/reprap/Translation.java
blob: 8061dc3ffb67415908b53d8ef4a67915fa7820f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.reprap;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;

public class Translation {

	private static Translation globalTranslation = null;

	private Properties currentTranslation = null;
	private Properties defaultTranslation = null;

	private String defaultLanguage = "en_GB";
	private String currentLanguage = null;
	
	private Translation(){		
		currentTranslation = new Properties();
		
		try {
			currentLanguage = Preferences.loadGlobalString("Language");
		} catch (IOException e){
			System.err.println("Translation class: Error loading the preferences file: "+e.getMessage());
			currentLanguage = defaultLanguage;
		}
		if (currentLanguage == null){
			currentLanguage = defaultLanguage;
		}
		//now we have found a language setting

		//first load the default language file
		defaultTranslation = loadTranslationFile(defaultLanguage);
		
		if (currentLanguage.equalsIgnoreCase(defaultLanguage)){
			currentTranslation = defaultTranslation;
		} else {
			currentTranslation = loadTranslationFile(currentLanguage);
		}
		
	}
	
	public static Translation getGlobalTranslation(){
		initIfNeeded(); 
		return globalTranslation;
	}
	
	synchronized private static void initIfNeeded() {
		if (globalTranslation == null)
			globalTranslation = new Translation();
	}
	
	private Properties loadTranslationFile(String languageCode){
		Properties p = new Properties();
		File f = new File("lib" + File.separatorChar + "translations" + File.separatorChar + languageCode + ".properties");
		try {

			p.load(f.toURL().openStream());
			
		} catch (IOException e){
			System.err.println("Translation class: Error loading the Translation file: "+f.getAbsolutePath());
		}
		return p;
	}
	
	public static String translate(String toBeTranslated){
		initIfNeeded();
		return globalTranslation.getTranslation(toBeTranslated);
	}
	
	private String getTranslation(String toBeTranslated){
		String translatedText = null;
		
		translatedText = currentTranslation.getProperty(toBeTranslated);
		
		// fall back to default language if translation is not found in current language
		if (translatedText == null){
			translatedText = defaultTranslation.getProperty(toBeTranslated);
			System.err.println("Translation class: missing translation: \"" + toBeTranslated + "\" in file: " + currentLanguage + ".properties");
			
			//return "(to be translated)" if there is no translation in either language
			if (translatedText == null){
				System.err.println("Translation class: no translation for \""+toBeTranslated+ "\" was found. Spelling?");
				translatedText = "(to be translated)";
			}
		}		
		
		return translatedText;
	}
	
//	public static void main(String[] args){
//
//		Translation t = Translation.getGlobalTranslation();
//		String s = t.getTranslation("MainWindowTitle");
//		
//		System.out.println(s);
//	}
	
}