4 #if defined(ECERE_BOOTSTRAP) || defined(ECERE_STATIC)
6 #if !defined(ECERE_BOOTSTRAP)
11 #ifndef ECERE_BOOTSTRAP
12 #define FileOpen FileOpenBuffered
15 static Map<String, Map<String, String>> moduleMaps { };
17 #define SWAP_DWORD(dword) ((((unsigned int)(dword) & 0x000000ff) << 24) \
18 | (((unsigned int)(dword) & 0x0000ff00) << 8) \
19 | (((unsigned int)(dword) & 0x00ff0000) >> 8) \
20 | (((unsigned int)(dword) & 0xff000000) >> 24))
22 public dllexport void LoadTranslatedStrings(Module module, char * name)
25 char fileName[MAX_LOCATION];
33 if(GetEnvironment("LANGUAGE", language, sizeof(language)))
35 else if(GetEnvironment("LC_ALL", lcAll, sizeof(lcAll)))
37 else if(GetEnvironment("LC_MESSAGES", lcMessages, sizeof(lcMessages)))
39 else if(GetEnvironment("LANG", lang, sizeof(lang)))
45 strcpy(language, locale);
46 dot = strstr(language, ".");
52 sprintf(fileName, "<:%s>locale/%s/LC_MESSAGES/%s.mo", module.name, locale, name);
54 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", locale, name);
55 f = FileOpen(fileName, read);
58 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", locale, name);
59 f = FileOpen(fileName, read);
63 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", locale, name);
64 f = FileOpen(fileName, read);
69 f.Read(&magic, sizeof(uint), 1);
70 if(magic == 0x950412de || magic == 0xde120495)
72 Map<String, String> textMap;
73 bool swap = magic != 0x950412de;
76 uint origStrings = 0, transStrings = 0;
77 uint hashingSize = 0, hashingOffset = 0;
79 f.Read(&revision, sizeof(uint), 1); if(swap) SWAP_DWORD(revision);
80 f.Read(&numStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(numStrings);
81 f.Read(&origStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(origStrings);
82 f.Read(&transStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(transStrings);
83 f.Read(&hashingSize, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingSize);
84 f.Read(&hashingOffset, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingOffset);
89 MapIterator<String, Map<String, String>> it { map = moduleMaps };
90 if(it.Index(module.name, false))
92 // TOFIX: delete moduleMaps[module];
94 moduleMaps[module.name] = textMap = { };
95 for(c = 0; c < numStrings; c++)
97 uint len = 0, offset = 0;
98 char * original = null, * translated = null;
100 f.Seek(origStrings + c*2*sizeof(uint), start);
101 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
102 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
103 f.Seek(offset, start);
105 original = new byte[len + 1];
106 f.Read(original, 1, len + 1);
108 f.Seek(transStrings + c*2*sizeof(uint), start);
109 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
110 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
111 f.Seek(offset, start);
113 translated = new byte[len + 1];
114 f.Read(translated, 1, len + 1);
118 MapIterator<String, String> it { map = textMap };
119 // TOFIX: Memory leak if the add fails
120 if(it.Index(original, false))
123 textMap[original] = translated;
133 printf("Invalid format while loading %s\n", fileName);
139 public dllexport void UnloadTranslatedStrings(Module module)
141 MapIterator<String, Map<String, String>> it { map = moduleMaps };
142 if(it.Index(module.name, false))
145 moduleMaps.Delete(it.pointer);
149 public dllexport char * GetTranslatedString(Module module, char * string, char * stringAndContext)
151 Map<String, String> textMap = moduleMaps ? moduleMaps[module.name] : null;
152 char * result = textMap ? textMap[stringAndContext ? stringAndContext : string] : string;
153 return (result && result[0]) ? result : string;