| 1 |
{ |
| 2 |
Copyright 2019-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Utilities for localizing CGE applications using GetText. } |
| 17 |
unit CastleLocalizationGetText; |
| 18 |
|
| 19 |
{$ifndef FPC} |
| 20 |
{$message fatal 'This unit is only for now only for FPC, not Delphi. Porting our localization to Delphi is a TODO.'} |
| 21 |
{$endif} |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses SysUtils, Classes, GetText, |
| 26 |
CastleClassUtils, CastleUnicode; |
| 27 |
|
| 28 |
type |
| 29 |
ETranslationEmptyId = class(Exception); |
| 30 |
|
| 31 |
{ Generate text to insert to POT (PO template) to allow translating given text. |
| 32 |
Depending on MsgidFromId: |
| 33 |
@unorderedList( |
| 34 |
@item(@true (default): |
| 35 |
|
| 36 |
msgid is Id, msgstr is EnglishText in output. |
| 37 |
This looks non-standard, but is sometimes more comfortable: |
| 38 |
@unorderedList( |
| 39 |
@item EnglishText may be empty. |
| 40 |
@item EnglishText may be repeated many times in the same output file. |
| 41 |
@item Changing EnglishText later doesn't require updating all PO files, as EnglishText is not used as the key. |
| 42 |
) |
| 43 |
) |
| 44 |
|
| 45 |
@item(@false: |
| 46 |
|
| 47 |
msgid is EnglishText, msgstr is empty, and we place special comment in the output to preserve Id. |
| 48 |
This is more standard, following typical GetText usage, |
| 49 |
and some software UI may assume that msgid is an English text. |
| 50 |
) |
| 51 |
) } |
| 52 |
function GetTextPoEntry(const Context, Id, EnglishText: String; |
| 53 |
const MsgidFromId: Boolean = true): String; |
| 54 |
|
| 55 |
{ Generate GetText PO file contents that can be used as a basis for translators |
| 56 |
to localize this component. The component may be a hierarchy of user interface |
| 57 |
and transformations, just like for @link(CastleComponentSerialize). |
| 58 |
|
| 59 |
A typical workflow of translation is: |
| 60 |
|
| 61 |
@orderedList( |
| 62 |
|
| 63 |
@item(Generate a file containing everything to translate in your components |
| 64 |
by calling something like |
| 65 |
@code(GenerateGetTextPo('castle-data:/gui/*.castle-user-interface');).) |
| 66 |
|
| 67 |
@item(Translate the PO files and generate MO files using GetText tools.) |
| 68 |
|
| 69 |
@item(In game, after determining user language, |
| 70 |
call e.g. @code(TranslateAllDesigns('castle-data:/translations/user_interface.ja.mo');) |
| 71 |
Where "ja" stands for Japanese.) |
| 72 |
) |
| 73 |
|
| 74 |
The argument of this routine can be a TComponent hierarchy, |
| 75 |
which can be a TCastleUserInterface or TCastleTransform hierarchy |
| 76 |
designed in the CGE editor or in code. |
| 77 |
|
| 78 |
The argument can also be an URL of a design (loaded using CastleSerializeComponent) |
| 79 |
or a mask (using wildcards * and ?) to load multiple designs. |
| 80 |
The design basename (part of URL without extension and path) |
| 81 |
is the prefix for all identifiers inside. |
| 82 |
|
| 83 |
The file will include all translatable properties in the given component hierarchy, |
| 84 |
with their current values (by convention in English). |
| 85 |
By convention you store this file as "xxx.pot" or ""xxx.en.po" and then |
| 86 |
translate it e.g. to "xxz.ja.po" which you can use to load Japanese translations |
| 87 |
by TranslateDesigns . } |
| 88 |
function GenerateGetTextPo(const C: TComponent; const GroupName: String): String; |
| 89 |
function GenerateGetTextPo(const UrlMask: String): String; |
| 90 |
|
| 91 |
type |
| 92 |
{ TMOFile descendant that allows iterating through all strings. } |
| 93 |
TCastleMOFile = class(TMOFile) |
| 94 |
private |
| 95 |
function GetKey(const AIndex: Cardinal): String; |
| 96 |
function GetValue(const AIndex: Cardinal): String; |
| 97 |
public |
| 98 |
constructor Create(const Stream: TStream); |
| 99 |
constructor Create(const Url: String); |
| 100 |
|
| 101 |
property Count: Cardinal read StringCount; |
| 102 |
property Keys[const AIndex: Cardinal]: String read GetKey; |
| 103 |
property Values[const AIndex: Cardinal]: String read GetValue; |
| 104 |
|
| 105 |
{ Does the given id exist in MO file. |
| 106 |
Using it is much slower than just attempting to translate using |
| 107 |
@code(Translate) method, but allows to recognize when a key is missing |
| 108 |
(as opposed to when a key translation is empty). } |
| 109 |
function ContainsKey(const TranslationId: String): Boolean; |
| 110 |
function ContainsKeyWithContext(const Context, Id: String): Boolean; |
| 111 |
|
| 112 |
function TranslateWithContext(const Context, Id: String): String; |
| 113 |
end; |
| 114 |
|
| 115 |
{ Load GetText MO file from and URL. } |
| 116 |
function LoadGetTextMo(const Url: String): TCastleMOFile; |
| 117 |
deprecated 'use TCastleMOFile.Create'; |
| 118 |
|
| 119 |
{ Extract from MO file all unique characters in translated strings, |
| 120 |
add them to Characters. } |
| 121 |
procedure AddTranslatedCharacters(const Url: String; const Characters: TUnicodeCharList); |
| 122 |
procedure AddTranslatedCharacters(const Mo: TCastleMOFile; const Characters: TUnicodeCharList); |
| 123 |
|
| 124 |
{ Translate all future designs (component hierarchies) loaded using @link(CastleComponentSerialize) |
| 125 |
using the given GetText MO file. |
| 126 |
This is the simplest way to translate user interface designed using CGE Editor. |
| 127 |
|
| 128 |
The GetText PO file with suitable identifiers should be earlier generated using |
| 129 |
@link(GenerateGetTextPo), translated, and processed to MO format using GetText |
| 130 |
tools. |
| 131 |
|
| 132 |
Calling this routine again will override the effect of the previous call. |
| 133 |
That is, at a given time, only one MO file is "active" and automatically translates |
| 134 |
all the loaded designs. Pass GetTextMoUrl = '' to disable translating further UIs. |
| 135 |
|
| 136 |
@seealso TranslateDesign } |
| 137 |
procedure TranslateAllDesigns(const GetTextMoUrl: String); |
| 138 |
|
| 139 |
{ Translate all possible properties in the given component hierarchy |
| 140 |
with given translation file. Missing translations will cause a warning in |
| 141 |
the log (indicating that your translation file is outdated and |
| 142 |
doesn't reflect all UI stuff anymore). |
| 143 |
|
| 144 |
If all your designs are loaded from files (using @link(CastleComponentSerialize)) |
| 145 |
then usually it's more comfortable to use @link(TranslateAllDesigns) |
| 146 |
instead of this routine. This routine is however more flexible, |
| 147 |
and allows to translate any component hierarchy. |
| 148 |
|
| 149 |
@seealso TranslateAllDesigns } |
| 150 |
procedure TranslateDesign(const C: TComponent; const GroupName: String; const GetTextMo: TCastleMOFile); |
| 151 |
|
| 152 |
{ Use this instead of FPC TranslateResourceStrings. |
| 153 |
This supports URL to MO file, and works cross-platform. } |
| 154 |
procedure CastleTranslateResourceStrings(const GetTextMoUrl: String); |
| 155 |
|
| 156 |
implementation |
| 157 |
|
| 158 |
uses CastleUtils, CastleStringUtils, CastleFindFiles, CastleComponentSerialize, |
| 159 |
CastleUriUtils, CastleLog, CastleDownload; |
| 160 |
|
| 161 |
const |
| 162 |
PoContextDelimiter = #4; |
| 163 |
|
| 164 |
{ GetTextPoEntry ------------------------------------------------------------- } |
| 165 |
|
| 166 |
function GetTextPoEntry(const Context, Id, EnglishText: String; |
| 167 |
const MsgidFromId: Boolean): String; |
| 168 |
|
| 169 |
function StringToPo(const S: String): String; |
| 170 |
begin |
| 171 |
Result := S; |
| 172 |
// convert #13#10 -> #10, i.e. make line endings Unix |
| 173 |
Result := SDeleteChars(Result, [#13]); |
| 174 |
Result := SReplacePatterns(Result, ['\', #10, '"'], ['\\', '\n', '\"'], false); |
| 175 |
Result := '"' + Result + '"'; |
| 176 |
end; |
| 177 |
|
| 178 |
begin |
| 179 |
Result := ''; |
| 180 |
|
| 181 |
{ place Id as "reference" (usually source code filename, |
| 182 |
line number etc. when xgettext generates the file) } |
| 183 |
if Id = '' then |
| 184 |
raise ETranslationEmptyId.CreateFmt('Empty id for "%s"', [EnglishText]); |
| 185 |
if Pos(' ', Id) <> 0 then |
| 186 |
raise ETranslationEmptyId.CreateFmt('Translation id contains space "%s"', [Id]); |
| 187 |
|
| 188 |
if MsgidFromId then |
| 189 |
begin |
| 190 |
if Context <> '' then |
| 191 |
Result += 'msgctxt ' + StringToPo(Context) + NL; |
| 192 |
Result += 'msgid ' + StringToPo(Id) + NL; |
| 193 |
Result += 'msgstr ' + StringToPo(EnglishText) + NL; |
| 194 |
end else |
| 195 |
begin |
| 196 |
// When English text is empty, it is not translatable |
| 197 |
if EnglishText = '' then Exit; |
| 198 |
|
| 199 |
Result += '#: ' + Id + NL; |
| 200 |
|
| 201 |
if Context <> '' then |
| 202 |
Result += 'msgctxt ' + StringToPo(Context) + NL; |
| 203 |
Result += 'msgid ' + StringToPo(EnglishText) + NL; |
| 204 |
Result += 'msgstr ' + StringToPo('') + NL; |
| 205 |
end; |
| 206 |
|
| 207 |
Result += NL; |
| 208 |
end; |
| 209 |
|
| 210 |
{ GenerateGetTextPo(TComponent) and helper ----------------------------------------------- } |
| 211 |
|
| 212 |
type |
| 213 |
TComponentHandler = class |
| 214 |
GroupName: String; |
| 215 |
Output: String; |
| 216 |
procedure TranslateProperty(const Sender: TCastleComponent; |
| 217 |
const PropertyName: String; var PropertyValue: String); |
| 218 |
end; |
| 219 |
|
| 220 |
procedure TComponentHandler.TranslateProperty(const Sender: TCastleComponent; |
| 221 |
const PropertyName: String; var PropertyValue: String); |
| 222 |
begin |
| 223 |
Assert(Sender.Name <> ''); |
| 224 |
Assert(PropertyName <> ''); |
| 225 |
Output := Output + GetTextPoEntry('castle-components', |
| 226 |
GroupName + '.' + Sender.Name + '.' + PropertyName, |
| 227 |
PropertyValue); |
| 228 |
end; |
| 229 |
|
| 230 |
function GenerateGetTextPo(const C: TComponent; const GroupName: String): String; |
| 231 |
var |
| 232 |
Handler: TComponentHandler; |
| 233 |
begin |
| 234 |
Check(GroupName <> '', 'GenerateGetTextPo requires non-empty GroupName'); |
| 235 |
Handler := TComponentHandler.Create; |
| 236 |
try |
| 237 |
Handler.GroupName := GroupName; |
| 238 |
TranslateProperties(C, @Handler.TranslateProperty); |
| 239 |
Result := Handler.Output; |
| 240 |
finally FreeAndNil(Handler) end; |
| 241 |
end; |
| 242 |
|
| 243 |
{ GenerateGetTextPo(string) -------------------------------------------------- } |
| 244 |
|
| 245 |
type |
| 246 |
TFilesHandler = class |
| 247 |
Output: String; |
| 248 |
procedure FoundFile(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TFilesHandler.FoundFile(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 252 |
var |
| 253 |
GroupName: String; |
| 254 |
C, COwner: TComponent; |
| 255 |
begin |
| 256 |
COwner := TComponent.Create(nil); |
| 257 |
try |
| 258 |
C := ComponentLoad(FileInfo.URL, COwner); |
| 259 |
GroupName := DeleteURIExt(ExtractURIName(FileInfo.URL)); |
| 260 |
Output := Output + GenerateGetTextPo(C, GroupName); |
| 261 |
finally FreeAndNil(COwner) end; |
| 262 |
end; |
| 263 |
|
| 264 |
function GenerateGetTextPo(const UrlMask: String): String; |
| 265 |
var |
| 266 |
Handler: TFilesHandler; |
| 267 |
FilesCount: Cardinal; |
| 268 |
begin |
| 269 |
Handler := TFilesHandler.Create; |
| 270 |
try |
| 271 |
FilesCount := FindFiles(UrlMask, false, @Handler.FoundFile, [ffRecursive]); |
| 272 |
WriteLnLog('Generated GetText PO for %d files matching "%s"', [FilesCount, UrlMask]); |
| 273 |
Result := Handler.Output; |
| 274 |
finally FreeAndNil(Handler) end; |
| 275 |
end; |
| 276 |
|
| 277 |
{ LoadGetTextMo -------------------------------------------------------------- } |
| 278 |
|
| 279 |
function LoadGetTextMo(const Url: String): TCastleMOFile; |
| 280 |
begin |
| 281 |
Result := TCastleMOFile.Create(Url); |
| 282 |
end; |
| 283 |
|
| 284 |
{ AddTranslatedCharacters ---------------------------------------------------- } |
| 285 |
|
| 286 |
procedure AddTranslatedCharacters(const Url: String; const Characters: TUnicodeCharList); |
| 287 |
var |
| 288 |
Mo: TCastleMOFile; |
| 289 |
begin |
| 290 |
Mo := TCastleMOFile.Create(Url); |
| 291 |
try |
| 292 |
AddTranslatedCharacters(Mo, Characters); |
| 293 |
finally FreeAndNil(Mo) end; |
| 294 |
end; |
| 295 |
|
| 296 |
procedure AddTranslatedCharacters(const Mo: TCastleMOFile; const Characters: TUnicodeCharList); |
| 297 |
var |
| 298 |
I: Integer; |
| 299 |
begin |
| 300 |
for I := 0 to Mo.Count - 1 do |
| 301 |
Characters.Add(Mo.Values[I]); |
| 302 |
end; |
| 303 |
|
| 304 |
{ TranslateAllDesigns -------------------------------------------------------- } |
| 305 |
|
| 306 |
var |
| 307 |
TranslateAllDesignsMo: TCastleMOFile; |
| 308 |
|
| 309 |
procedure TranslateDesignCallback(const C: TComponent; const GroupName: String); |
| 310 |
begin |
| 311 |
if TranslateAllDesignsMo = nil then |
| 312 |
Exit; // in case this is called after finalization of this unit |
| 313 |
TranslateDesign(C, GroupName, TranslateAllDesignsMo); |
| 314 |
end; |
| 315 |
|
| 316 |
procedure TranslateAllDesigns(const GetTextMoUrl: String); |
| 317 |
begin |
| 318 |
FreeAndNil(TranslateAllDesignsMo); |
| 319 |
OnInternalTranslateDesign := nil; |
| 320 |
|
| 321 |
if GetTextMoUrl <> '' then |
| 322 |
begin |
| 323 |
TranslateAllDesignsMo := TCastleMOFile.Create(GetTextMoUrl); |
| 324 |
OnInternalTranslateDesign := @TranslateDesignCallback; |
| 325 |
end; |
| 326 |
end; |
| 327 |
|
| 328 |
{ TranslateDesign ------------------------------------------------------------ } |
| 329 |
|
| 330 |
type |
| 331 |
TTranslateDesignHelper = class |
| 332 |
GroupName: String; |
| 333 |
Mo: TCastleMOFile; |
| 334 |
procedure TranslateProperty(const Sender: TCastleComponent; |
| 335 |
const PropertyName: String; var PropertyValue: String); |
| 336 |
end; |
| 337 |
|
| 338 |
procedure TTranslateDesignHelper.TranslateProperty(const Sender: TCastleComponent; |
| 339 |
const PropertyName: String; var PropertyValue: String); |
| 340 |
var |
| 341 |
TranslationId: String; |
| 342 |
begin |
| 343 |
Assert(Sender.Name <> ''); |
| 344 |
Assert(PropertyName <> ''); |
| 345 |
TranslationId := 'castle-components' + PoContextDelimiter + |
| 346 |
GroupName + '.' + Sender.Name + '.' + PropertyName; |
| 347 |
if not Mo.ContainsKey(TranslationId) then |
| 348 |
begin |
| 349 |
WritelnWarning('Translation id "%s" is not present in the GetText MO file. Translation was done for an outdated UI file, add new keys to the GetText PO files and translate them and recreate MO file.', |
| 350 |
[TranslationId]); |
| 351 |
Exit; |
| 352 |
end; |
| 353 |
PropertyValue := Mo.Translate(TranslationId); |
| 354 |
end; |
| 355 |
|
| 356 |
procedure TranslateDesign(const C: TComponent; const GroupName: String; const GetTextMo: TCastleMOFile); |
| 357 |
var |
| 358 |
Helper: TTranslateDesignHelper; |
| 359 |
begin |
| 360 |
Helper := TTranslateDesignHelper.Create; |
| 361 |
try |
| 362 |
Helper.Mo := GetTextMo; |
| 363 |
Helper.GroupName := GroupName; |
| 364 |
TranslateProperties(C, @Helper.TranslateProperty); |
| 365 |
finally FreeAndNil(Helper) end; |
| 366 |
end; |
| 367 |
|
| 368 |
{ CastleTranslateResourceStrings --------------------------------------------- } |
| 369 |
|
| 370 |
procedure CastleTranslateResourceStrings(const GetTextMoUrl: String); |
| 371 |
var |
| 372 |
Mo: TCastleMOFile; |
| 373 |
begin |
| 374 |
Mo := TCastleMOFile.Create(GetTextMoUrl); |
| 375 |
try |
| 376 |
TranslateResourceStrings(Mo); |
| 377 |
finally FreeAndNil(Mo) end; |
| 378 |
end; |
| 379 |
|
| 380 |
{ TCastleMOFile ------------------------------------------------------------ } |
| 381 |
|
| 382 |
constructor TCastleMOFile.Create(const Stream: TStream); |
| 383 |
begin |
| 384 |
inherited Create(Stream); |
| 385 |
end; |
| 386 |
|
| 387 |
constructor TCastleMOFile.Create(const Url: String); |
| 388 |
var |
| 389 |
S: TStream; |
| 390 |
begin |
| 391 |
S := Download(Url); |
| 392 |
try |
| 393 |
Create(S); |
| 394 |
finally FreeAndNil(S) end; |
| 395 |
end; |
| 396 |
|
| 397 |
function TCastleMOFile.GetKey(const AIndex: Cardinal): String; |
| 398 |
begin |
| 399 |
Result := OrigStrings^[AIndex]; |
| 400 |
end; |
| 401 |
|
| 402 |
function TCastleMOFile.GetValue(const AIndex: Cardinal): String; |
| 403 |
begin |
| 404 |
Result := TranslStrings^[AIndex]; |
| 405 |
end; |
| 406 |
|
| 407 |
function TCastleMOFile.ContainsKey(const TranslationId: String): Boolean; |
| 408 |
var |
| 409 |
I: Integer; |
| 410 |
begin |
| 411 |
for I := 0 to Count - 1 do |
| 412 |
if Keys[I] = TranslationId then |
| 413 |
Exit(true); |
| 414 |
Result := false; |
| 415 |
end; |
| 416 |
|
| 417 |
function TCastleMOFile.ContainsKeyWithContext(const Context, Id: String): Boolean; |
| 418 |
begin |
| 419 |
Result := ContainsKey(Context + PoContextDelimiter + Id); |
| 420 |
end; |
| 421 |
|
| 422 |
function TCastleMOFile.TranslateWithContext(const Context, Id: String): String; |
| 423 |
begin |
| 424 |
Result := Translate(Context + PoContextDelimiter + Id); |
| 425 |
end; |
| 426 |
|
| 427 |
finalization |
| 428 |
FreeAndNil(TranslateAllDesignsMo); |
| 429 |
end. |