-
Notifications
You must be signed in to change notification settings - Fork 23
/
uLocalization.pas
276 lines (230 loc) · 6.88 KB
/
uLocalization.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
unit uLocalization;
{$MODE objfpc}
interface
uses
IniFiles, SysUtils, Classes;
type
TLanguageCode = String[2];
TLanguageInfo = record
Code: TLanguageCode;
Name, NativeName: WideString;
FileName: String;
AlternativeFor: array of TLanguageCode;
Author: WideString;
end;
TLanguagesArray = array of TLanguageInfo;
ELocalizerException = class(Exception);
TLocalizer = class
private
LangInfo: TLanguageInfo;
Strings: TStringList;
LangsDir: String;
UseAltsForMissedStrings: Boolean; // Try or not read missed strings from default or alternative languages
function GetLanguageInfo: TLanguageInfo;
class function GetLanguageInfoFromIni(const AnIni: TMemIniFile): TLanguageInfo;
procedure ClearLangInfoAndStrings;
public
constructor Create(ALangsDir: String; AnUseAltsForMissedStrings: Boolean = True);
destructor Destroy; override;
//procedure LoadByCode(ALang: TLanguageCode);
procedure LoadFromFile(AFileName: String);
function I18N(StrID: String): WideString;
procedure GetLanguages(var LangsArr: TLanguagesArray);
property LanguageInfo: TLanguageInfo read GetLanguageInfo;
end;
var
Localizer: TLocalizer;
implementation
uses uUtils, FileUtil, LazFileUtils, {Classes,} StrUtils;
{ TLocalizer }
procedure TLocalizer.ClearLangInfoAndStrings;
begin
with LangInfo do
begin
Code := '';
Name := '';
NativeName := '';
FileName := '';
SetLength(AlternativeFor, 0);
Author := '';
end;
Strings.Clear;
end;
constructor TLocalizer.Create(ALangsDir: String; AnUseAltsForMissedStrings: Boolean);
begin
Strings := TStringList.Create;
Strings.NameValueSeparator := '=';
LangInfo.AlternativeFor := nil;
ClearLangInfoAndStrings;
LangsDir := IncludeTrailingBackslash(ALangsDir);
UseAltsForMissedStrings := AnUseAltsForMissedStrings;
end;
destructor TLocalizer.Destroy;
begin
//FreeAndNil(LangInfo.AlternativeFor);
LangInfo.AlternativeFor := nil;
FreeAndNil(Strings);
inherited;
end;
function TLocalizer.GetLanguageInfo: TLanguageInfo;
begin
Result := LangInfo;
end;
class function TLocalizer.GetLanguageInfoFromIni(
const AnIni: TMemIniFile): TLanguageInfo;
const
IniSection = 'info';
var
AlternativeForStr: TStringList;
I: integer;
begin
Result.Code := {Wide}LowerCase(AnIni.ReadString(IniSection, 'LangCode', ''));
Result.Name := AnIni.ReadString(IniSection, 'LangName', '');
Result.NativeName := AnIni.ReadString(IniSection, 'LangNativeName', '');
Result.FileName := AnIni.FileName;
AlternativeForStr := TStringList.Create;
AlternativeForStr.CommaText := AnIni.ReadString(IniSection, 'AlternativeFor', '');
SetLength(Result.AlternativeFor, AlternativeForStr.Count);
for I := 0 to AlternativeForStr.Count - 1 do
Result.AlternativeFor[I] := AlternativeForStr.Strings[I];
AlternativeForStr.Free;
Result.Author := AnIni.ReadString(IniSection, 'Author', '');
end;
procedure TLocalizer.GetLanguages(var LangsArr: TLanguagesArray);
var
SearchRes: TSearchRec;
LangsCount: integer;
Idx: integer;
Ini: TMemIniFile;
begin
// Get amount of available languages
LangsCount := 0;
if FindFirst(LangsDir + '*.ini', faAnyFile, SearchRes) = 0 then
begin
repeat
Inc(LangsCount);
until FindNext(SearchRes) <> 0;
FindClose(SearchRes);
end;
// Allocate memory for array of languages
SetLength(LangsArr, LangsCount);
// Write language info in array
Idx := -1;
if FindFirst(LangsDir + '*.ini', faAnyFile, SearchRes) = 0 then
begin
repeat
Ini := TMemIniFile.Create(LangsDir + SearchRes.Name);
try
try
Inc(Idx);
LangsArr[Idx] := TLocalizer.GetLanguageInfoFromIni(Ini);
finally
//Ini.Free;
FreeAndNil(Ini);
end;
except
end;
until FindNext(SearchRes) <> 0;
FindClose(SearchRes);
end;
end;
function TLocalizer.I18N(StrID: String): WideString;
begin
if LangInfo.Code = '' then
raise ELocalizerException.Create('No localization loaded');
Result := Strings.Values[StrID];
if Result = '' then
Result := '<unknown>';
Result := DecodeControlCharacters(Result);
end;
procedure TLocalizer.LoadFromFile(AFileName: String);
// ToDo: Optimization needed - reduce the number of file reading operations
const
TranslationIniSection = 'translation';
var
Ini: TMemIniFile;
TmpStr: TStringList;
AllLangs: TLanguagesArray;
AltLang: TLanguageCode;
procedure CombineValues(L1: TStrings; const L2: TStrings);
var
Idx: integer;
begin
for Idx := 0 to L2.Count - 1 do
L1.Values[L2.Names[Idx]] := L2.ValueFromIndex[Idx];
end;
begin
ClearLangInfoAndStrings;
{ Check if selected translation file exists }
if not FileExists(AFileName) then
raise ELocalizerException.CreateFmt('Can`t open localization file "%s"', [AFileName]);
{ Read language info }
Ini := TMemIniFile.Create(AFileName);
try
LangInfo := GetLanguageInfoFromIni(Ini);
finally
FreeAndNil(Ini);
end;
if UseAltsForMissedStrings then
begin
{ Read strings from default (English) translation }
if not AnsiEndsStr('en.ini', AFileName) then // Skip for English
begin
Ini := TMemIniFile.Create(LangsDir + 'en.ini');
try
Ini.ReadSectionValues(TranslationIniSection, Strings);
finally
FreeAndNil(Ini);
end;
end;
{ Read and update strings from alternative of specified translation }
GetLanguages(AllLangs);
AltLang := GetAlternativeLanguage(AllLangs, LangInfo.Code);
if AltLang <> '' then
begin
Ini := TMemIniFile.Create(LangsDir + AltLang + '.ini');
TmpStr := TStringList.Create;
try
Ini.ReadSectionValues(TranslationIniSection, TmpStr);
CombineValues(Strings, TmpStr);
finally
FreeAndNil(TmpStr);
end;
end;
end;
{ Read and update strings from specified translation }
Ini := TMemIniFile.Create(AFileName);
try
// Combine translation strings with defaults
TmpStr := TStringList.Create;
try
Ini.ReadSectionValues(TranslationIniSection, TmpStr);
CombineValues(Strings, TmpStr);
finally
FreeAndNil(TmpStr);
end;
finally
FreeAndNil(Ini);
end;
end;
var
LangDir: String;
initialization
begin
{$IfDef Windows}
LangDir := AppendPathDelim(ConcatPaths([ProgramDirectory, 'lang']));
{$EndIf}
{$IfDef Linux}
if IsPortable then
LangDir := AppendPathDelim(ConcatPaths([ProgramDirectory, 'lang']))
else
LangDir := '/usr/share/autoscreenshot/lang/';
{$EndIf}
// ToDo: Use alternatives only in Release build
Localizer := TLocalizer.Create(LangDir, True);
end;
finalization
begin
FreeAndNil(Localizer);
end;
end.