File: translate.cpp

package info (click to toggle)
bouml 4.21-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 73,336 kB
  • ctags: 55,459
  • sloc: cpp: 290,644; makefile: 228; sh: 13
file content (210 lines) | stat: -rw-r--r-- 4,298 bytes parent folder | download
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
// *************************************************************************
//
// Copyright 2004-2010 Bruno PAGES  .
//
// This file is part of the BOUML Uml Toolkit.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// e-mail : bouml@free.fr
// home   : http://bouml.free.fr
//
// *************************************************************************

#include <stdio.h>
#include <qdict.h>
#include <qmessagebox.h>

#include "translate.h"

static QDict<QString> * CurrentTranslation = 0;
static QString CurrentLang;

QString current_lang()
{
  return CurrentLang;
}

void set_lang(QString l)
{
  if (l != CurrentLang) {
    if (l.isEmpty()) {
      delete CurrentTranslation;
      CurrentTranslation = 0;
      CurrentLang = "";
    }
    else {
      FILE * fp = fopen((const char *) l, "rb");
      
      if (fp == 0) {
	QMessageBox::critical(0, "Bouml",
			      "cannot open file " +
			      l +
			      "\n, still use " + 
			      ((CurrentLang.isEmpty()) ? "english" : CurrentLang));

	return;
      }
      
      CurrentLang = l;
      
      if (CurrentTranslation == 0) {
	CurrentTranslation = new QDict<QString>(997);
	CurrentTranslation->setAutoDelete(TRUE);
      }
      else
	CurrentTranslation->clear();
      
      int len, c, i;
      
      while ((len = fgetc(fp)) != EOF) {
	len += (fgetc(fp) << 8);
	
	QString en((const QChar *) 0, (unsigned) len);
	
	for (i = 0; i != len; i += 1) {
	  c = fgetc(fp);
	  
	  en[i] = QChar(fgetc(fp), c);
	}
	
	len = fgetc(fp);
	len += (fgetc(fp) << 8);
	
	if (len != 0) {
	  QString * s = new QString((const QChar *) 0, (unsigned) len);
	  
	  for (i = 0; i != len; i += 1) {
	    c = fgetc(fp);
	    
	    s->at(i) = QChar(fgetc(fp), c);
	  }
	  
	  CurrentTranslation->insert(en, s);
	}
      }
      
      fclose(fp);
    }
  }
}

static void translate(QString & s)
{
  if (CurrentTranslation == 0)
    return;
  
  static QChar c1(' ');
  static QChar c2(':');
  static QChar c3('\n');
  
  QString k, b, e;
  int ln = s.length();
  int index1 = 0;
  
  while (index1 != ln) {
    QCharRef c = s.at(index1);
    
    if ((c != c1) && (c != c2) && (c != c3))
      break;
    index1 += 1;
  }
  
  int index2 = ln;
  
  while (--index2 > index1) {
    QCharRef c = s.at(index2);
    
    if ((c != c1) && (c != c2) && (c != c3))
      break;
  }
   
  if (index1 != 0) {
    // some removed chars at beginning
    b = s.left(index1);
    
    if (index2 != (ln - 1)) {
      // some removed chars at end
      e = s.mid(index2 + 1);
      k = s.mid(index1, index2 - index1 + 1);
    }
    else
      k = s.mid(index1);
  }
  else if (index2 != (ln - 1)) {
    // some removed chars at end
      // some removed chars at end
      e = s.mid(index2 + 1);
      k = s.mid(0, index2 + 1);
  }
  else
    k = s;

  QString * s2 = CurrentTranslation->find(k);
  
  if (s2 != 0)
    s = b + *s2 + e;
}

QString TR(QString s)
{
  translate(s);
  return s;
}

QString TR(QString s, QString a1)
{
  translate(s); 
  
  int index;
  
  if ((index = s.find("%1")) != -1)
    s.replace(index, 2, a1);
  
  return s;
}

QString TR(QString s, QString a1, QString a2)
{
  translate(s);  
  
  int index;
  
  if ((index = s.find("%1")) != -1)
    s.replace(index, 2, a1);
  
  if ((index = s.find("%2")) != -1)
    s.replace(index, 2, a2);
  
  return s;
}

QString TR(QString s, QString a1, QString a2, QString a3)
{
  translate(s); 
  
  int index;
  
  if ((index = s.find("%1")) != -1)
    s.replace(index, 2, a1);
  
  if ((index = s.find("%2")) != -1)
    s.replace(index, 2, a2);
  
  if ((index = s.find("%3")) != -1)
    s.replace(index, 2, a3);
  
  return s;
}