4 #ifndef ECERE_BOOTSTRAP
5 #define FileOpen FileOpenBuffered
8 Map<String, Map<String, String>> moduleMaps { };
10 #define SWAP_DWORD(dword) ((((unsigned int)(dword) & 0x000000ff) << 24) \
11 | (((unsigned int)(dword) & 0x0000ff00) << 8) \
12 | (((unsigned int)(dword) & 0x00ff0000) >> 8) \
13 | (((unsigned int)(dword) & 0xff000000) >> 24))
15 public dllexport void LoadTranslatedStrings(Module module, char * name)
18 char fileName[MAX_LOCATION];
26 if(GetEnvironment("LANGUAGE", language, sizeof(language)))
28 else if(GetEnvironment("LC_ALL", lcAll, sizeof(lcAll)))
30 else if(GetEnvironment("LC_MESSAGES", lcMessages, sizeof(lcMessages)))
32 else if(GetEnvironment("LANG", lang, sizeof(lang)))
38 strcpy(language, locale);
39 dot = strstr(language, ".");
45 sprintf(fileName, "<:%s>/locale/%s/LC_MESSAGES/%s.mo", module.name, locale, name);
47 sprintf(fileName, ":locale/%s/LC_MESSAGES/%s.mo", locale, name);
48 f = FileOpen(fileName, read);
51 sprintf(fileName, "locale/%s/LC_MESSAGES/%s.mo", locale, name);
52 f = FileOpen(fileName, read);
56 sprintf(fileName, "/usr/share/locale/%s/LC_MESSAGES/%s.mo", locale, name);
57 f = FileOpen(fileName, read);
62 f.Read(&magic, sizeof(uint), 1);
63 if(magic == 0x950412de || magic == 0xde120495)
65 Map<String, String> textMap;
66 bool swap = magic != 0x950412de;
69 uint origStrings = 0, transStrings = 0;
70 uint hashingSize = 0, hashingOffset = 0;
72 f.Read(&revision, sizeof(uint), 1); if(swap) SWAP_DWORD(revision);
73 f.Read(&numStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(numStrings);
74 f.Read(&origStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(origStrings);
75 f.Read(&transStrings, sizeof(uint), 1); if(swap) SWAP_DWORD(transStrings);
76 f.Read(&hashingSize, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingSize);
77 f.Read(&hashingOffset, sizeof(uint), 1); if(swap) SWAP_DWORD(hashingOffset);
82 MapIterator<String, Map<String, String>> it { map = moduleMaps };
83 if(it.Index(module.name, false))
85 // TOFIX: delete moduleMaps[module];
87 moduleMaps[module.name] = textMap = { };
88 for(c = 0; c < numStrings; c++)
90 uint len = 0, offset = 0;
91 char * original = null, * translated = null;
93 f.Seek(origStrings + c*2*sizeof(uint), start);
94 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
95 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
96 f.Seek(offset, start);
98 original = new byte[len + 1];
99 f.Read(original, 1, len + 1);
101 f.Seek(transStrings + c*2*sizeof(uint), start);
102 f.Read(&len, sizeof(uint), 1); if(swap)SWAP_DWORD(len);
103 f.Read(&offset, sizeof(uint), 1); if(swap)SWAP_DWORD(offset);
104 f.Seek(offset, start);
106 translated = new byte[len + 1];
107 f.Read(translated, 1, len + 1);
110 textMap[original] = translated;
119 printf("Invalid format while loading %s\n", fileName);
125 public dllexport void UnloadTranslatedStrings(Module module)
127 MapIterator<String, Map<String, String>> it { map = moduleMaps };
128 if(it.Index(module.name, false))
131 moduleMaps.Delete(it.pointer);
135 public dllexport char * GetTranslatedString(Module module, char * string)
137 Map<String, String> textMap = moduleMaps ? moduleMaps[module.name] : null;
138 return textMap ? textMap[string] : string;