-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunittranslator.pas
327 lines (272 loc) · 9.7 KB
/
unittranslator.pas
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
unit UnitTranslator;
{ Unit to handle translations of resource strings found in a console
application. It is similar to LCLTranslator (2004-2015 by
V.I. Volchenko and Lazarus Developers Team) with the following changes
- all reference to forms and the LCL are eliminated
- function FindLocaleFileName has been rewritten
- default parameter ForceUpdate in SetDefaultLang is not used as it
applied to Forms. It is kept for compatibility only.
- directories are searched only if they exist which will reduce
the number of searches compared to the original code.
2017 Michel Deslierres
Below is the copyright notice of LCLTranslator.
}
{ Copyright (C) 2004-2015 V.I.Volchenko and Lazarus Developers Team
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library 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 Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
{
This unit translates resource strings in a console program using a .po or .mo
file. For GUI applications (using LCL) use LCLTranslator and DefaultTranslator
instead. The unit searches for translated .po/.mo files in some common places:
<myAddDir>/<userdir>/<Lang>/<file> or <userdir>/<Lang>/file
<myAddDir>/languages/<Lang>/<file>
<myAppDir>/locale/<Lang>/<file>
<myAppDir>/locale/<Lang>/LC_MESSAGES/<file>
<myAppDir><file>
<myAddDir>/<userdir>/<lngfile> or <userdir>/<lngfile>
<myAddDir>/languages/<lngfile>
<myAppDir>/locale/<lngfile>
<myAddDir><lngfile>
where
userdir: can be a sub directory off the application directory or
an absolute directory
Lang: a locale specification
file: will be app.po and then app.mo
lngfile: will be app.<Lang>.po or app.<Lang>.mo
In a Unix (Linux) system, /usr/share/locale/<Lang>/LC_MESSAGES/<file>
is also checked.
The search stops as soon as a translation file is found.
By default, SetDefaultLang('', '') searches for the .po or .mo file
corresponding to the system locale. This can be overriden in the application
command line with the '--LANG LangID' switch or its aliases '--lang' and '-l'.
To translate resource strings in a console application, just use this unit in
the project and enable i18n in the project options. Translate the po files
generated by the IDE and place the translated files in one of the above
directory (changing its name if desired).
Dependency: LCLBase, not LCL.
}
{$mode objfpc}{$H+}
interface
uses
SysUtils, Gettext, Translations, LazUTF8, LazFileUtils;
procedure SetDefaultLang(Const Lang: string; Const Dir: string = ''; ForceUpdate: boolean = true);
function GetDefaultLang: String;
implementation
var
DefaultLang: String = '';
function GetDefaultLang: String;
begin
if DefaultLang = '' then SetDefaultLang('');
GetDefaultLang := DefaultLang;
end;
{Returns Language Id as found in the following order:
1. Lang parameter (if not '')
2. Command line parameter (--Lang | --lang | -l LangId)
3. Environment variable LANG (if not '')
4. Other locale environment variables
'LC_ALL', LC_MESSAGES, 'LANG'}
function GetLang(const Lang: string): string;
var
T: string;
i: integer;
begin
Result := Lang;
if Result = '' then begin
for i := 1 to Paramcount - 1 do
if (ParamStrUTF8(i) = '--LANG')
or (ParamStrUTF8(i) = '-l')
or (ParamStrUTF8(i) = '--lang') then
Result := ParamStrUTF8(i + 1);
end;
if Result = '' then
Result := GetEnvironmentVariableUTF8('LANG');
if Result = '' then begin
T := ''; // removes warning about T not initialized
LazGetLanguageIDs(Result, T);
end;
// In Ubuntu Linux, Locale is 'fr_CA.UTF-8' but there
// is no encoding locale directory of that type
// inside /usr/share/locale but there are subdirectories
// with names such as be@latin and ca@valencia.
// Accordingly, only the first 5 characters will be kept
// if the third character is a '_'.
//
// If this is wrong, then remove these lines and
// add the commented out block in FindLCFile below.
//
if (length(Result) > 5) and (Result[3] = '_') then
setlength(Result, 5);
end;
function FileFound(const fname: string): boolean;
begin
try
result := FileExistsUTF8(fname);
except
result := false;
end;
end;
procedure SetDefaultLang(Const Lang: string; Const Dir: string = '';
ForceUpdate: boolean = true);
{ Arguments:
Lang: a locale specification
Dir: a custom translation files subdirectory (e.g. 'mylng')
ForceUpdate: ignored, kept for compatibility with
LclTranslator.SetDefaultLang.
The locale specification can be reduced, such as 'fr_CA' or 'fr'.
It can be a complete specification such as 'fr_CA.UTF-8' or
'be@latin'. In the first case, only fr_CA is kept. If <file> is
not found, then Lang is reduced to a 2 letter locale, fr and
the search is repeated.
If Dir is not an empty string then the search begins in the
specified Dir sub directory of the executable's directory.
}
var
LangID: string;
AppDir: string;
UserDir: string;
LocaleDir: string;
LanguagesDir: string;
function FindLCIDLcFile(const LID, LCname: string): string;
begin
DefaultLang := LID;
if UserDir <> '' then begin
Result := UserDir + LID + DirectorySeparator + LCname;
if FileFound(Result) then exit;
end;
Result := AppDir + LID + DirectorySeparator + LCname;
if FileFound(Result) then exit;
if LanguagesDir <> '' then begin
Result := LanguagesDir + LID + DirectorySeparator + LCname;
if FileFound(Result) then exit;
end;
if LocaleDir <> '' then begin
Result := LocaleDir + LID + DirectorySeparator + LCname;
if FileFound(Result) then exit;
Result := LocaleDir + LID + DirectorySeparator
+ 'LC_MESSAGES' + DirectorySeparator + LCname;
if FileFound(Result) then exit;
end;
{$IFDEF UNIX}
Result := '/usr/share/locale/' + LID + DirectorySeparator
+ 'LC_MESSAGES' + DirectorySeparator + LCname;
if FileFound(Result) then exit;
{$ENDIF}
// not found
Result := '';
end;
function FindLCFileLCID(const LCname: string): string;
begin
if UserDir <> '' then begin
Result := UserDir + LCName;
if FileFound(Result) then exit;
end;
Result := AppDir + LCname;
if FileFound(Result) then exit;
if LanguagesDir <> '' then begin
Result := LanguagesDir + LCname;
if FileFound(Result) then exit;
end;
if LocaleDir <> '' then begin
Result := LocaleDir + LCname;
if FileFound(Result) then exit;
end;
Result := '';
end;
function FindLocaleFileName(const LCExt: string): string;
var
LID, LCFilename, BaseFilename: string;
begin
BaseFilename := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), '');
LCfilename := BaseFileName + LCExt;
LID := LangID;
Result := FindLCIDLCFile(LID, LCfilename);
if Result <> '' then exit;
Result := FindLCFileLCID(BaseFilename + '.' + LID + LCExt);
if Result <> '' then exit;
(*
// Include this it it is wrong to cut-off language id at
// 5 characters if the third is a '_' in GetLang.
// In that case, the previous search was for fr_CA.UTF-8
// and it is now necessary to do a search for f_CA
if (length(LID) > 5) and (pos('_', LID) = 3) then begin
LID := copy(LID, 1, 5);
Result := FindLCIDLCFile(LID, LCfilename);
if Result <> '' then exit;
Result := FindLCFileLCID(BaseFilename + '.' + LID + LCExt);
if Result <> '' then exit;
end;
*)
if length(LID) > 2 then begin
LID := copy(LID, 1, 2);
Result := FindLCIDLCFile(LID, LCfilename);
if result <> '' then exit;
Result := FindLCFileLCID(BaseFilename + '.' + LID + LCExt);
if result <> '' then exit;
end;
// last effort, '{AppDir}/{AppName}.po' or ('*.mo'). Even if that
// file is found, the target language is unknown.
DefaultLang := '';
Result := AppDir + BaseFilename + LCExt;
if not FileFound(result) then
Result := '';
end;
var
lcfn: string;
begin
LangID := GetLang(Lang);
if length(LangId) < 2 then
exit;
AppDir := ExtractFilePath(ParamStrUTF8(0));
LocaleDir := AppendPathDelim(AppDir + 'locale');
LanguagesDir := AppendPathDelim(AppDir + 'languages');
if not DirectoryExistsUTF8(AppDir) then begin
AppDir := '';
LocaleDir := '';
LanguagesDir := '';
end
else begin
if not DirectoryExistsUTF8(LocaleDir) then
LocaleDir := '';
if not DirectoryExistsUTF8(LanguagesDir) then
LanguagesDir := '';
end;
UserDir := '';
if Dir <> '' then begin
UserDir := AppendPathDelim(Dir);
if not (FilenameIsWinAbsolute(UserDir) or FilenameIsUnixAbsolute(UserDir)) then
UserDir := AppDir + UserDir;
if not DirectoryExistsUTF8(UserDir) then
UserDir := '';
end;
lcfn := FindLocaleFileName('.po');
if lcfn <> '' then
try
Translations.TranslateResourceStrings(lcfn);
except
lcfn := '';
end;
if lcfn = '' then begin
lcfn := FindLocaleFileName('.mo');
if lcfn <> '' then
try
GetText.TranslateResourceStrings(UTF8ToSys(lcfn));
except
lcfn := '';
end;
end;
if lcfn = '' then
DefaultLang := '';
end;
initialization
SetDefaultLang('', 'lng');
end.