00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00030
00031
00032
00033
00034
00035 #include "stratagus.h"
00036
00037 #include <map>
00038 #include <string>
00039
00040 #include "translate.h"
00041 #include "iolib.h"
00042
00043
00044
00045
00046
00047 typedef std::map<std::string, std::string> EntriesType;
00048 static EntriesType Entries;
00049 std::string StratagusTranslation;
00050 std::string GameTranslation;
00051
00052
00053
00054
00055
00059 const char *Translate(const char *str)
00060 {
00061 EntriesType::iterator i = Entries.find(str);
00062 if (i != Entries.end() && !i->second.empty()) {
00063 return i->second.c_str();
00064 } else {
00065 return str;
00066 }
00067 }
00068
00072 void AddTranslation(const std::string &str1, const std::string &str2)
00073 {
00074 if (str2.empty()) {
00075 Entries[str1] = str1;
00076 } else {
00077 Entries[str1] = str2;
00078 }
00079 }
00080
00084 void LoadPO(const std::string &file)
00085 {
00086 FILE *fd;
00087 char buf[4096];
00088 enum { MSGNONE, MSGID, MSGSTR } state;
00089 char msgid[16 * 1024];
00090 char msgstr[16 * 1024];
00091 char *s;
00092 char *currmsg = NULL;
00093 char fullfile[1024];
00094
00095 if (file.empty()) {
00096 return;
00097 }
00098
00099 LibraryFileName(file.c_str(), fullfile, sizeof(fullfile));
00100 fd = fopen(fullfile, "rb");
00101 if (!fd) {
00102 fprintf(stderr, "Could not open file: %s\n", file.c_str());
00103 return;
00104 }
00105
00106 state = MSGNONE;
00107 msgid[0] = msgstr[0] = '\0';
00108
00109
00110 char c = fgetc(fd);
00111 if (c == (char)0xEF) {
00112 fgetc(fd);
00113 fgetc(fd);
00114 } else {
00115 rewind(fd);
00116 }
00117
00118 while (fgets(buf, sizeof(buf), fd)) {
00119
00120 if (buf[0] == '#') {
00121 continue;
00122 }
00123
00124 s = buf;
00125
00126
00127 if (!strncmp(s, "msgid ", 6)) {
00128 if (state == MSGSTR) {
00129 *currmsg = '\0';
00130 if (*msgid != '\0') {
00131 AddTranslation(msgid, msgstr);
00132 }
00133 }
00134 state = MSGID;
00135 currmsg = msgid;
00136 *currmsg = '\0';
00137 s += 6;
00138 while (*s == ' ') ++s;
00139 } else if (!strncmp(s, "msgstr ", 7)) {
00140 if (state == MSGID) {
00141 *currmsg = '\0';
00142 }
00143 state = MSGSTR;
00144 currmsg = msgstr;
00145 *currmsg = '\0';
00146 s += 7;
00147 while (*s == ' ') ++s;
00148 }
00149
00150
00151 if (*s == '"') {
00152 ++s;
00153 while (*s && *s != '"') {
00154 if (*s == '\\') {
00155 ++s;
00156 if (*s) {
00157 if (*s == 'n') {
00158 *currmsg++ = '\n';
00159 } else if (*s == 't') {
00160 *currmsg++ = '\t';
00161 } else if (*s == 'r') {
00162 *currmsg++ = '\r';
00163 } else if (*s == '"') {
00164 *currmsg++ = '"';
00165 } else if (*s == '\\') {
00166 *currmsg++ = '\\';
00167 } else {
00168 fprintf(stderr, "Invalid escape character: %c\n", *s);
00169 }
00170 ++s;
00171 } else {
00172 fprintf(stderr, "Unterminated string\n");
00173 }
00174 } else {
00175 *currmsg++ = *s++;
00176 }
00177 }
00178 continue;
00179 }
00180 }
00181 if (state == MSGSTR) {
00182 *currmsg = '\0';
00183 AddTranslation(msgid, msgstr);
00184 }
00185
00186 fclose(fd);
00187 }
00188
00194 void SetTranslationsFiles(const std::string &stratagusfile, const std::string &gamefile)
00195 {
00196 LoadPO(stratagusfile);
00197 LoadPO(gamefile);
00198 StratagusTranslation = stratagusfile;
00199 GameTranslation = gamefile;
00200 }
00202