1 #ifndef ECERE_COM_MODULE
6 #if defined(ECERE_BOOTSTRAP) || defined(ECERE_STATIC)
8 #if !defined(ECERE_BOOTSTRAP)
13 #ifndef ECERE_BOOTSTRAP
14 #define FileOpen FileOpenBuffered
17 static Map<String, Map<String, String>> moduleMaps { };
19 #define SWAP_DWORD(dword) ((((unsigned int)(dword) & 0x000000ff) << 24) \
20 | (((unsigned int)(dword) & 0x0000ff00) << 8) \
21 | (((unsigned int)(dword) & 0x00ff0000) >> 8) \
22 | (((unsigned int)(dword) & 0xff000000) >> 24))
24 public dllexport void LoadTranslatedStrings(Module module, char * name)
26 #ifndef ECERE_COM_MODULE
28 char fileName[MAX_LOCATION];
36 if(GetEnvironment("LANGUAGE", language, sizeof(language)))
38 else if(GetEnvironment("LC_ALL", lcAll, sizeof(lcAll)))
40 else if(GetEnvironment("LC_MESSAGES", lcMessages, sizeof(lcMessages)))
42 else if(GetEnvironment("LANG", lang, sizeof(lang)))
48 strcpy(language, locale);
49 dot = strstr(language, ".");
55 sprintf(fileName, "<:%s>locale/%s/LC_MESSAGES/%s.mo", module.name, locale, name);
57 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", locale, name);
58 f = FileOpen(fileName, read);
61 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", locale, name);
62 f = FileOpen(fileName, read);
66 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", locale, name);
67 f = FileOpen(fileName, read);
72 f.Read(&magic, sizeof(uint), 1);
73 if(magic == 0x950412de || magic == 0xde120495)
75 Map<String, String> textMap;
76 bool swap = magic != 0x950412de;
79 uint origStrings = 0, transStrings = 0;
80 uint hashingSize = 0, hashingOffset = 0;
82 f.Read(&revision, sizeof(uint), 1); if(swap) SWAP_DWORD(revision);
83 f.Read(&numStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(numStrings);
84 f.Read(&origStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(origStrings);
85 f.Read(&transStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(transStrings);
86 f.Read(&hashingSize, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingSize);
87 f.Read(&hashingOffset, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingOffset);
92 MapIterator<String, Map<String, String>> it { map = moduleMaps };
93 if(it.Index(module.name, false))
95 // TOFIX: delete moduleMaps[module];
97 moduleMaps[module.name] = textMap = { };
98 for(c = 0; c < numStrings; c++)
100 uint len = 0, offset = 0;
101 char * original = null, * translated = null;
103 f.Seek(origStrings + c*2*sizeof(uint), start);
104 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
105 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
106 f.Seek(offset, start);
108 original = new byte[len + 1];
109 f.Read(original, 1, len + 1);
111 f.Seek(transStrings + c*2*sizeof(uint), start);
112 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
113 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
114 f.Seek(offset, start);
116 translated = new byte[len + 1];
117 f.Read(translated, 1, len + 1);
121 MapIterator<String, String> it { map = textMap };
122 // TOFIX: Memory leak if the add fails
123 if(it.Index(original, false))
126 textMap[original] = translated;
136 printf("Invalid format while loading %s\n", fileName);
143 public dllexport void UnloadTranslatedStrings(Module module)
145 MapIterator<String, Map<String, String>> it { map = moduleMaps };
146 if(it.Index(module.name, false))
149 moduleMaps.Delete(it.pointer);
153 public dllexport char * GetTranslatedString(Module module, char * string, char * stringAndContext)
155 Map<String, String> textMap = moduleMaps ? moduleMaps[module.name] : null;
156 char * result = textMap ? textMap[stringAndContext ? stringAndContext : string] : string;
157 return (result && result[0]) ? result : string;