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];
35 char genericLocale[256];
39 if(GetEnvironment("LANGUAGE", language, sizeof(language)))
41 else if(GetEnvironment("LC_ALL", lcAll, sizeof(lcAll)))
43 else if(GetEnvironment("LC_MESSAGES", lcMessages, sizeof(lcMessages)))
45 else if(GetEnvironment("LANG", lang, sizeof(lang)))
51 if(language != locale)
52 strcpy(language, locale);
53 dot = strstr(language, ".");
60 strcpy(genericLocale, locale);
61 under = strchr(genericLocale, '_');
67 sprintf(fileName, "<:%s>locale/%s/%s.mo", module.name, name, locale);
69 sprintf(fileName, ":locale/%s/%s.mo", name, locale);
70 f = FileOpen(fileName, read);
74 sprintf(fileName, "<:%s>locale/%s/LC_MESSAGES/%s.mo", module.name, locale, name);
76 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", locale, name);
77 f = FileOpen(fileName, read);
81 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", locale, name);
82 f = FileOpen(fileName, read);
86 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", locale, name);
87 f = FileOpen(fileName, read);
90 if(!f && locale && strcmpi(locale, genericLocale))
92 // Attempt with generic language
94 sprintf(fileName, "<:%s>locale/%s/%s.mo", module.name, name, genericLocale);
96 sprintf(fileName, ":locale/%s/%s.mo", name, genericLocale);
97 f = FileOpen(fileName, read);
101 sprintf(fileName, "<:%s>locale/%s/LC_MESSAGES/%s.mo", module.name, genericLocale, name);
103 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", genericLocale, name);
104 f = FileOpen(fileName, read);
108 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", genericLocale, name);
109 f = FileOpen(fileName, read);
113 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", genericLocale, name);
114 f = FileOpen(fileName, read);
121 f.Read(&magic, sizeof(uint), 1);
122 if(magic == 0x950412de || magic == 0xde120495)
124 Map<String, String> textMap;
125 bool swap = magic != 0x950412de;
128 uint origStrings = 0, transStrings = 0;
129 uint hashingSize = 0, hashingOffset = 0;
131 f.Read(&revision, sizeof(uint), 1); if(swap) SWAP_DWORD(revision);
132 f.Read(&numStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(numStrings);
133 f.Read(&origStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(origStrings);
134 f.Read(&transStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(transStrings);
135 f.Read(&hashingSize, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingSize);
136 f.Read(&hashingOffset, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingOffset);
141 MapIterator<String, Map<String, String>> it { map = moduleMaps };
142 if(it.Index(module.name, false))
144 // TOFIX: delete moduleMaps[module];
146 moduleMaps[module.name] = textMap = { };
147 for(c = 0; c < numStrings; c++)
149 uint len = 0, offset = 0;
150 char * original = null, * translated = null;
152 f.Seek(origStrings + c*2*sizeof(uint), start);
153 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
154 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
155 f.Seek(offset, start);
157 original = new byte[len + 1];
158 f.Read(original, 1, len + 1);
160 f.Seek(transStrings + c*2*sizeof(uint), start);
161 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
162 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
163 f.Seek(offset, start);
165 translated = new byte[len + 1];
166 f.Read(translated, 1, len + 1);
170 MapIterator<String, String> it { map = textMap };
171 // TOFIX: Memory leak if the add fails
172 if(it.Index(original, false))
175 textMap[original] = translated;
185 printf("Invalid format while loading %s\n", fileName);
192 public dllexport void UnloadTranslatedStrings(Module module)
194 MapIterator<String, Map<String, String>> it { map = moduleMaps };
195 if(it.Index(module.name, false))
198 moduleMaps.Delete(it.pointer);
202 public dllexport char * GetTranslatedString(Module module, char * string, char * stringAndContext)
204 Map<String, String> textMap = moduleMaps ? moduleMaps[module.name] : null;
205 char * result = textMap ? textMap[stringAndContext ? stringAndContext : string] : string;
206 return (result && result[0]) ? result : string;