· 8 years ago · Jun 12, 2018, 12:56 AM
1## XLanguageSystem.java
2import java.util.HashMap;
3import java.util.LinkedList;
4
5public class XLanguageSystem {
6 private HashMap<String, HashMap<String, String>> cache = new HashMap<String, HashMap<String, String>>();
7
8 public XLanguageSystem() {
9 // read available languages from database here...
10 }
11
12
13 public String getEntry(String lang, String trans_id) {
14 if(this.languageExists(lang)) { // language exists...
15 if(this.cacheElementExists(lang, trans_id)) { // element exists in language-cache...
16 // get element and remove it temporarly from the cache...
17 String trans = this.cache.get(lang).remove(trans_id);
18
19 this.cache.get(lang).put(trans_id, trans); // add element back to the cache {element needs to be removed and readded to implement LFU}
20
21 return trans;
22 }
23 else { // element does not exist in language-cache...
24 // - try to get it from the lang-table from the database...
25 // - add it to lang-cache...
26
27 // if that failes...
28 return this.getDefaultTranslation(trans_id);
29 }
30 }
31 else // language does not exist...
32 return this.getDefaultTranslation(trans_id);
33 }
34
35 public void addLanguage(String lang) {
36 this.cache.put(lang, new HashMap<String, String>()); // create cache-HashMap for the language
37
38 // add new language to the db here...
39
40 /*
41
42 "CREATE TABLE `lang" + lang + ` (
43 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
44 `trans_id` VARCHAR(200) NOT NULL KEY,
45 `trans` LONGTEXT NOT NULL KEY,
46 `userid` INT(11) NOT NULL KEY,
47 `active` TINYINT(1) DEFAULT '0'
48 ) ENGINE=MyISAM;
49
50 */
51 }
52
53 public LinkedList<String> getAvailableLanguages() {
54 return new LinkedList<String>(this.cache.keySet());
55 }
56
57 public void removeCacheElemen(String lang, String trans_id) {
58 // if langueage exists and key exists in this language-cache... remove it...
59 if(this.languageExists(lang) && this.cacheElementExists(lang, trans_id))
60 this.cache.get(lang).remove(trans_id);
61 }
62
63
64 private String getDefaultTranslation(String trans_id) {
65 // get default translation for trans_id {default = english}...
66
67 return "";
68 }
69
70 private boolean languageExists(String lang) {
71 return this.cache.containsKey(lang);
72 }
73
74 private boolean cacheElementExists(String lang, String trans_id) {
75 return this.cache.get(lang).containsKey(trans_id);
76 }
77}