File: lang.cc

package info (click to toggle)
graphthing 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 856 kB
  • ctags: 700
  • sloc: cpp: 7,716; yacc: 209; sh: 206; lex: 188; makefile: 11
file content (415 lines) | stat: -rw-r--r-- 9,674 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
//	lang.cc
//

#include "wx/string.h"

#include <iostream>
#include <map>
#include <stdexcept>
#include <stdlib.h>
#include <string>
#include <vector>
#include "lang.h"

#include "phrases.h"


int Translator::warn_untranslated;


static struct lang_info {
	char *code;	// ISO639 code (e.g. "de")
	char *name;	// full name (e.g. "German")
	Language lang;	// enum value (e.g. German)
} languages[] = {
	{ "ca", "Catalan", Catalan },
	{ "zh", "ChineseSimp", ChineseSimp },
	{ "da", "Danish", Danish },
	{ "nl", "Dutch", Dutch },
	{ "en", "English", English },
	{ "eo", "Esperanto", Esperanto },
	{ "fi", "Finnish", Finnish },
	{ "fr", "French", French },
	{ "de", "German", German },
	{ "el", "Greek", Greek },
	{ "it", "Italian", Italian },
	{ "no", "Norwegian", Norwegian },
	{ "pl", "Polish", Polish },
	{ "pt", "Portuguese", Portuguese },
	{ "ro", "Romanian", Romanian },
	{ "es", "Spanish", Spanish },
	{ "sv", "Swedish", Swedish },
	{ "sv_CK", "SwedishChef", SwedishChef } // CK is Cook Islands (geddit?)
};
#define NUM_LANGUAGES	(int) (sizeof (languages) / sizeof (languages[0]))


PhraseBlock::PhraseBlock (wxString eng, type_t type)
		: english (eng), type (type)
{
}

PhraseBlock::~PhraseBlock ()
{
}

void PhraseBlock::set_type (type_t type)
{
	this->type = type;
}

void PhraseBlock::set_param (int id, wxString param)
{
	if (type == concat) {
		concats.push_back (param);
		return;
	} else if (type == replace) {
		if (id == 0)
			base = param;
		else if (id == 1)
			substr = param;
		else if (id == 2)
			substr_new = param;
		else
			std::cerr << "Bad id for replace in set_param().\n";
		return;
	}

	std::cerr << "Bad set_param() call.\n";
}

void PhraseBlock::add (Language lang, wxString translation)
{
	if (type != regular) {
		std::cerr << "Adding a translation to non-regular block!\n";
		return;
	}
	translations[lang] = translation;
}

bool PhraseBlock::has_translation (Language lang) const
{
	if (lang == English)
		return true;
	if (type == concat) {
		PhraseBlock *phr;
		std::vector<wxString>::const_iterator it;
		for (it = concats.begin (); it != concats.end (); ++it) {
			phr = translator->lookup_phr (*it);
			if (!phr->has_translation (lang))
				return false;
		}
		return true;
	}
	if (type == literal)
		return true;
	if (type == replace) {
		PhraseBlock *phr;
		phr = translator->lookup_phr (base);
		if (!phr)
			return false;
		return phr->has_translation (lang);
	}

	// must be a regular phrase
	std::map<Language, wxString>::const_iterator it;
	it = translations.find (lang);
	if (it == translations.end ())
		return false;
	return true;
}

const wxString PhraseBlock::lookup (Language lang) const
{
	std::map<Language, wxString>::const_iterator it;

	if (lang == English)
		return english;
	if (type == concat) {
		// special case: concat
		wxString ret;
		std::vector<wxString>::const_iterator it;
		for (it = concats.begin (); it != concats.end (); ++it)
			ret += translator->lookup (lang, *it);
		return ret;
	} else if (type == literal) {
		// special case: literal
		return english;
	} else if (type == replace) {
		// special case: replace
		wxString str = translator->lookup (lang, base);
		str.Replace (substr.c_str (), substr_new.c_str ());
		return str;
	}

	it = translations.find (lang);
	if (it == translations.end ()) {
		// fallback to English
		if (Translator::warn_untranslated)
			std::cerr << "WARNING: \"" << english.mb_str (wxConvUTF8)
				<< "\" doesn't have a translation for "
				<< Translator::get_language_name (lang).mb_str (wxConvUTF8)
				<< ".\n";
		return english;
	}
	return it->second;
}

int PhraseBlock::verify_phrase () const
{
	static const wxString punct = wxT(".:!");
	int tail_len, warns;
	std::map<Language, wxString>::const_iterator it;

	warns = 0;

	// Test 0: Only attempt verification if this is a regular phrase
	if (type != regular)
		return 0;

	// Test 1: If English phrase ends in a certain punctuation substring,
	//		then all the translations must, too.
	for (tail_len = 1; tail_len < 5; ++tail_len) {
		wxString prop_tail = english.Right (tail_len);
		if (!punct.Contains (prop_tail.Left (1))) {
			--tail_len;
			break;
		}
	}
	if (tail_len > 0) {
		const wxString tail = english.Right (tail_len);

		for (it = translations.begin (); it != translations.end ();
									++it) {
			Language lang = it->first;
			wxString trans = it->second;
			const wxString tail_trans = trans.Right (tail_len);
			if (!tail.IsSameAs (tail_trans)) {
				std::cerr << "WARNING: \""
					<< english.mb_str (wxConvUTF8)
					<< "\"\n\tends with \""
					<< tail.mb_str (wxConvUTF8)
					<< "\", but the translation for "
					<< Translator::get_language_name (lang).mb_str (wxConvUTF8)
					<< " doesn't!\n";
				++warns;
			}
		}
	}

	// Test 2: If English phrase includes "&", so must the translations.
	static const wxString amp = wxT("&");
	if (english.Contains (amp)) {
		for (it = translations.begin (); it != translations.end ();
									++it) {
			Language lang = it->first;
			wxString trans = it->second;
			if (!trans.Contains (amp)) {
				std::cerr << "WARNING: \""
					<< english.mb_str (wxConvUTF8)
					<< "\"\n\thas a hotkey specifier (&),"
					<< " but the translation for "
					<< Translator::get_language_name (lang).mb_str (wxConvUTF8)
					<< " doesn't!\n";
				++warns;
			}
		}
	}

	// TODO
	// Test 3: If English has a substitution sequence (e.g. "%i"), then
	//		the translations must, too. (and in the right order!)



	return warns;
}

PhraseBlock *Translator::lookup_phr (const wxString s) const
{
	std::map<const wxString, PhraseBlock *>::const_iterator it;

	it = phrases.find (s);
	if (it == phrases.end ())
		return 0;		// no translations found for phrase
	return it->second;
}

void Translator::split_lang_spec (const char *spec, wxString &LL, wxString &CC)
									const
{
	wxString in = wxString (spec, wxConvUTF8);
	int pos;

	pos = in.Find ('_');
	if (pos < 0) {
		LL = in;
		CC = wxT("");
		return;
	}
	LL = in.Mid (0, pos);
	CC = in.Mid (pos + 1);
}

Translator::Translator (int debug)
{
	default_lang = English;
	warn_untranslated = debug;
}

Translator::~Translator ()
{
	std::map<const wxString, PhraseBlock *>::const_iterator it;

	for (it = phrases.begin (); it != phrases.end (); ++it)
		delete it->second;
}

const char *yy_lang_data;
void Translator::init ()
{
#if 0
	// debug
	extern int yy_lang_debug;
	yy_lang_debug = 1;
#endif
	extern int yy_lang_parse ();

	yy_lang_data = phrases_raw;
	if (yy_lang_parse ()) {
		// error!
		throw std::runtime_error ("Bad phrase file!");
	}
}

wxString Translator::get_language_name (Language lang)
{
	for (int i = 0; i < NUM_LANGUAGES; ++i)
		if (languages[i].lang == lang)
			return wxString (languages[i].name, wxConvUTF8);
	return wxT("Unknown");
}

Language Translator::get_language_from_name (const wxString lang)
{
	for (int i = 0; i < NUM_LANGUAGES; ++i) {
		if (lang == wxString (languages[i].name, wxConvUTF8)) {
			return languages[i].lang;
		}
	}

	return English;		// TODO: anything better we can do here?
}

void Translator::verify_phrases () const
{
	std::map<const wxString, PhraseBlock *>::const_iterator it;
	int tot_warns = 0, tot_missing = 0, total = 0;

	for (it = phrases.begin (); it != phrases.end (); ++it) {
		PhraseBlock *phr = it->second;
		tot_warns += phr->verify_phrase ();

		// don't check 'concat' phrase blocks, because their parts
		// will be caught later (or earlier)
		if (phr->get_type () == PhraseBlock::concat)
			continue;
		if (!phr->has_translation (default_lang)) {
			tot_missing += 1;
			std::cerr << "\"" << it->first.mb_str (wxConvUTF8) << "\"\n";
		}
		total += 1;
	}

	if (tot_warns > 0) {
		std::cerr << "-------------\n";
		std::cerr << tot_warns << " warning" <<
			(tot_warns > 1 ? "s" : "") << ".\n";
		std::cerr << "-------------\n";
	}
	if (tot_missing > 0) {
		double perc = 100 - (tot_missing * 100.0) / total;
		std::cerr << "-------------\n";
		std::cerr << "\""
			<< Translator::get_language_name (default_lang).mb_str (wxConvUTF8)
			<< "\" is missing " << tot_missing << "/" << total
			<< " translations (" << perc << "% complete).\n";
		std::cerr << "-------------\n";
	}
}

bool Translator::guess_language (bool ignore_cc)
{
	// First, check environment variables
	char *envvars[3] = { "LANGUAGE", "LC_ALL", "LANG" };
	for (int i = 0; i < 3; ++i) {
		char *lang = getenv (envvars[i]);
		if (!lang)
			continue;
		wxString LL, CC;
		if (ignore_cc)
			split_lang_spec (lang, LL, CC);
		else
			LL = wxString (lang, wxConvUTF8);
		//std::cerr << "Looking for '" << LL << "'.\n";
		for (int j = 0; j < NUM_LANGUAGES; ++j) {
			if (LL == wxString (languages[j].code, wxConvUTF8)) {
				// Found it!
				set_language (languages[j].lang);
				return true;
			}
		}
	}

	// Failed!

	if (ignore_cc)
		return false;	// can't do any more

	// Try without country code
	return guess_language (true);
}

void Translator::set_language (Language lang)
{
	default_lang = lang;
}

Language Translator::get_language () const
{
	return default_lang;
}

void Translator::add_phrase (PhraseBlock *phr)
{
	wxString eng = phr->lookup (English);
	phrases[eng] = phr;
}

const wxString Translator::lookup (Language lang, const char *s) const
{
	return lookup (lang, wxString (s, wxConvUTF8));
}

const wxString Translator::lookup (Language lang, const wxString s) const
{
	PhraseBlock *phr;

	if (lang == English)
		return s;
	phr = lookup_phr (s);
	if (!phr) {
		// fallback to English
		if (warn_untranslated)
			std::cerr << "WARNING: \"" << s.mb_str (wxConvUTF8)
					<< "\" doesn't have a PhraseBlock.\n";
		return s;
	}
	return phr->lookup (lang);
}

const wxString Translator::lookup (const wxString s) const
{
	return lookup (default_lang, s);
}