summaryrefslogtreecommitdiff
path: root/src/corelib/gfx_translations.pas
blob: 251427e8dc2f65a4d7e1271e73b98674564561cc (plain)
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
{
    Methods and classes for loading the translations/localizations.
}

unit gfx_translations;

{$mode objfpc}{$H+}

{.$Define DEBUG}

interface

uses
  Classes
  ,SysUtils
  ;


type

  TTranslation = class(TObject)
  private
    FID: string;
  public
    property ID: string read FID;
  end;
  PTranslation = ^TTranslation;


  TTranslationList = class(TObject)
  private
    FCount: integer;
    FItems: PTranslation;
    function    GetItems(Index: integer): TTranslation;
  public
    destructor  Destroy; override;
    function    IndexOf(const ID: string): integer;
    procedure   Add(const ID: string);
    procedure   Clear;
  public
    property    Count: integer read FCount;
    property    Items[Index: integer]: TTranslation read GetItems; default;
  end;


procedure TranslateResourceStrings(const BaseAppName, BaseDirectory, CustomLang: string);


implementation

uses
  GetText
  ,gfx_pofiles
  ,gfx_utils
  ;
  

var
  TranslationList: TTranslationList;
  SystemLanguageID1: string = '';
  SystemLanguageID2: string = '';


procedure CollectTranslations(const BaseAppName, BaseDir: string);
var
  FileInfo: TSearchRec;
  ID: string;
  SearchMask: string;
begin
  // search for all <BaseAppName>.xxx.po files
  if TranslationList = nil then
    TranslationList := TTranslationList.Create
  else
    TranslationList.Clear;
  // add automatic and english translation
  TranslationList.Add('');
  TranslationList.Add('en');
  // search existing translations

  SearchMask := fpgAddTrailingValue(BaseDir, PathDelim, false) + BaseAppName + '.*.po';
  //writeln('CollectTranslations ',SearchMask);
  if SysUtils.FindFirst(SearchMask, faAnyFile, FileInfo) = 0
  then begin
    repeat
      if (FileInfo.Name = '.') or (FileInfo.Name = '..') or (FileInfo.Name = '') then
        Continue;
      ID := copy(FileInfo.Name,length(BaseAppName + '.')+1,
               length(FileInfo.Name)-length(BaseAppName + '..po'));
      //writeln('CollectTranslations A ',FileInfo.Name,' ID=',ID);
      if (ID <> '') and (Pos('.',ID) < 1) and (TranslationList.IndexOf(ID) < 0) then
      begin
        //writeln('CollectTranslations ID=',ID);
        TranslationList.Add(ID);
      end;
    until SysUtils.FindNext(FileInfo) <> 0;
  end;
  SysUtils.FindClose(FileInfo);
end;

procedure TranslateResourceStrings(const BaseAppName, BaseDirectory, CustomLang: string);
const
  Ext = '.%s.po';
var
  Lang: string;
  FallbackLang: string;
  Dir: string;
begin
  {$IFDEF DEBUG}
  writeln('BaseAppName = ',BaseAppName);
  writeln('BaseDirectory = ',BaseDirectory);
  writeln('CustomLang = ',CustomLang);
  {$ENDIF}
  if TranslationList = nil then
    CollectTranslations(BaseAppName, BaseDirectory);
    
  if CustomLang = '' then
  begin
    Lang := SystemLanguageID1;
    FallbackLang := SystemLanguageID2;
  end
  else
  begin
    Lang := CustomLang;
    FallbackLang := '';
  end;
//  writeln('TranslateResourceStrings A Lang=',Lang,' FallbackLang=',FallbackLang);
  Dir := fpgAddTrailingValue(BaseDirectory, PathDelim, false);

  {$IFDEF DEBUG}
  writeln('Lang = ' + Lang);
  writeln('SystemLanguageID1 = ' + SystemLanguageID1);
  writeln('SystemLanguageID2 = ' + SystemLanguageID2);
  writeln('Translation file = ' +Dir + BaseAppName + Ext);
  {$ENDIF}

  // We use one translation file for all fpGUI Toolkit related text and one
  // translation file for all fpGUI based application text
//  if BaseAppName = 'fpgui' then
//    TranslateUnitResourceStrings('',
//      Dir + BaseAppName + Ext, Lang, FallbackLang)
//  else
    TranslateUnitResourceStrings('fake',
      Dir + BaseAppName + Ext, Lang, FallbackLang);
end;

// Strip the '.' onwards part. eg: en_ZA.UTF-8  ->  en_ZA
procedure FixLanguageIDs;
var
  lpos: integer;
begin
  lpos := Pos('.', SystemLanguageID1);
  if lpos > 0 then
    SystemLanguageID1 := Copy(SystemLanguageID1, 0, lpos-1);
end;

{ TTranslationList }

function TTranslationList.GetItems(Index: integer): TTranslation;
begin
  Result := FItems[Index];
end;

destructor TTranslationList.Destroy;
begin
  Clear;
  inherited Destroy;
end;

procedure TTranslationList.Add(const ID: string);
var
  NewTranslation: TTranslation;
begin
  if IndexOf(ID)>=0 then
    raise Exception.Create('TTranslationList.Add '
        + 'ID="' + ID + '" already exists.');
  NewTranslation := TTranslation.Create;
  NewTranslation.FID := ID;
  inc(FCount);
  ReallocMem(FItems, SizeOf(Pointer)*FCount);
  FItems[FCount-1] := NewTranslation;
end;

function TTranslationList.IndexOf(const ID: string): integer;
begin
  Result := FCount - 1;
  while (Result >= 0) and (CompareText(ID, FItems[Result].ID) <> 0) do
    dec(Result);
end;

procedure TTranslationList.Clear;
var
  i: Integer;
begin
  for i := 0 to FCount-1 do
    FItems[i].Free;
  FCount := 0;
  ReallocMem(FItems, 0);
end;

initialization
  TranslationList := nil;
  GetLanguageIDs(SystemLanguageID1, SystemLanguageID2);
  FixLanguageIDs;

finalization
  TranslationList.Free;
  TranslationList := nil;

end.