Debian Codegraph @

engine/stratagus/translate.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name translate.cpp - Translate languages. */
11 //
12 // (c) Copyright 2005-2009 by Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36
37 #include <map>
38 #include <string>
39 #include <cstdio>
40
41 #include "translate.h"
42 #include "iolib.h"
43
44 /*----------------------------------------------------------------------------
45 -- Variables
46 ----------------------------------------------------------------------------*/
47
48 typedef std::map<std::string, std::string> EntriesType;
49 static EntriesType Entries;
50 std::string StratagusTranslation;
51 std::string GameTranslation;
52
53 /*----------------------------------------------------------------------------
54 -- Functions
55 ----------------------------------------------------------------------------*/
56
57 /**
58 ** Translate a string
59 */
60 const char *Translate(const char *str)
61 {
62 EntriesType::iterator i = Entries.find(str);
63 if (i != Entries.end() && !i->second.empty()) {
64 return i->second.c_str();
65 } else {
66 return str;
67 }
68 }
69
70 /**
71 ** Add a translation
72 */
73 void AddTranslation(const std::string &str1, const std::string &str2)
74 {
75 if (str2.empty()) {
76 Entries[str1] = str1;
77 } else {
78 Entries[str1] = str2;
79 }
80 }
81
82 /**
83 ** Load a .po file
84 */
85 void LoadPO(const std::string &file)
86 {
87 FILE *fd;
88 char buf[4096];
89 enum { MSGNONE, MSGID, MSGSTR } state;
90 char msgid[16 * 1024];
91 char msgstr[16 * 1024];
92 char *s;
93 char *currmsg = NULL;
94 char fullfile[1024];
95
96 if (file.empty()) {
97 return;
98 }
99
100 LibraryFileName(file.c_str(), fullfile, sizeof(fullfile));
101 fd = fopen(fullfile, "rb");
102 if (!fd) {
103 fprintf(stderr, "Could not open file: %s\n", file.c_str());
104 return;
105 }
106
107 state = MSGNONE;
108 msgid[0] = msgstr[0] = '\0';
109
110 // skip 0xEF utf8 intro if found
111 char c = fgetc(fd);
112 if (c == (char)0xEF) {
113 fgetc(fd);
114 fgetc(fd);
115 } else {
116 rewind(fd);
117 }
118
119 while (fgets(buf, sizeof(buf), fd)) {
120 // Comment
121 if (buf[0] == '#') {
122 continue;
123 }
124
125 s = buf;
126
127 // msgid or msgstr
128 if (!strncmp(s, "msgid ", 6)) {
129 if (state == MSGSTR) {
130 *currmsg = '\0';
131 if (*msgid != '\0') {
132 AddTranslation(msgid, msgstr);
133 }
134 }
135 state = MSGID;
136 currmsg = msgid;
137 *currmsg = '\0';
138 s += 6;
139 while (*s == ' ') ++s;
140 } else if (!strncmp(s, "msgstr ", 7)) {
141 if (state == MSGID) {
142 *currmsg = '\0';
143 }
144 state = MSGSTR;
145 currmsg = msgstr;
146 *currmsg = '\0';
147 s += 7;
148 while (*s == ' ') ++s;
149 }
150
151 // String
152 if (*s == '"') {
153 ++s;
154 while (*s && *s != '"') {
155 if (*s == '\\') {
156 ++s;
157 if (*s) {
158 if (*s == 'n') {
159 *currmsg++ = '\n';
160 } else if (*s == 't') {
161 *currmsg++ = '\t';
162 } else if (*s == 'r') {
163 *currmsg++ = '\r';
164 } else if (*s == '"') {
165 *currmsg++ = '"';
166 } else if (*s == '\\') {
167 *currmsg++ = '\\';
168 } else {
169 fprintf(stderr, "Invalid escape character: %c\n", *s);
170 }
171 ++s;
172 } else {
173 fprintf(stderr, "Unterminated string\n");
174 }
175 } else {
176 *currmsg++ = *s++;
177 }
178 }
179 continue;
180 }
181 }
182 if (state == MSGSTR) {
183 *currmsg = '\0';
184 AddTranslation(msgid, msgstr);
185 }
186
187 fclose(fd);
188 }
189
190 /**
191 ** Set the stratagus and game translations
192 **
193 ** Those filenames will be saved in the preferences when SavePreferences will be called.
194 */
195 void SetTranslationsFiles(const std::string &stratagusfile, const std::string &gamefile)
196 {
197 LoadPO(stratagusfile);
198 LoadPO(gamefile);
199 StratagusTranslation = stratagusfile;
200 GameTranslation = gamefile;
201 }
202 //@}
203