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)
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 if(language != locale)
49 strcpy(language, locale);
50 dot = strstr(language, ".");
56 sprintf(fileName, "<:%s>locale/%s/LC_MESSAGES/%s.mo", module.name, locale, name);
58 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", locale, name);
59 f = FileOpen(fileName, read);
62 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", locale, name);
63 f = FileOpen(fileName, read);
67 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", locale, name);
68 f = FileOpen(fileName, read);
73 f.Read(&magic, sizeof(uint), 1);
74 if(magic == 0x950412de || magic == 0xde120495)
76 Map<String, String> textMap;
77 bool swap = magic != 0x950412de;
80 uint origStrings = 0, transStrings = 0;
81 uint hashingSize = 0, hashingOffset = 0;
83 f.Read(&revision, sizeof(uint), 1); if(swap) SWAP_DWORD(revision);
84 f.Read(&numStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(numStrings);
85 f.Read(&origStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(origStrings);
86 f.Read(&transStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(transStrings);
87 f.Read(&hashingSize, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingSize);
88 f.Read(&hashingOffset, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingOffset);
93 MapIterator<String, Map<String, String>> it { map = moduleMaps };
94 if(it.Index(module.name, false))
96 // TOFIX: delete moduleMaps[module];
98 moduleMaps[module.name] = textMap = { };
99 for(c = 0; c < numStrings; c++)
101 uint len = 0, offset = 0;
102 char * original = null, * translated = null;
104 f.Seek(origStrings + c*2*sizeof(uint), start);
105 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
106 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
107 f.Seek(offset, start);
109 original = new byte[len + 1];
110 f.Read(original, 1, len + 1);
112 f.Seek(transStrings + c*2*sizeof(uint), start);
113 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
114 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
115 f.Seek(offset, start);
117 translated = new byte[len + 1];
118 f.Read(translated, 1, len + 1);
122 MapIterator<String, String> it { map = textMap };
123 // TOFIX: Memory leak if the add fails
124 if(it.Index(original, false))
127 textMap[original] = translated;
137 printf("Invalid format while loading %s\n", fileName);
144 public dllexport void UnloadTranslatedStrings(Module module)
146 MapIterator<String, Map<String, String>> it { map = moduleMaps };
147 if(it.Index(module.name, false))
150 moduleMaps.Delete(it.pointer);
154 public dllexport char * GetTranslatedString(Module module, char * string, char * stringAndContext)
156 Map<String, String> textMap = moduleMaps ? moduleMaps[module.name] : null;
157 char * result = textMap ? textMap[stringAndContext ? stringAndContext : string] : string;
158 return (result && result[0]) ? result : string;