| 1 |
{ |
| 2 |
Copyright 2018 Benedikt Magnus. |
| 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 |
{ Localization system for handling localization. |
| 17 |
Use this in your games for easy localization. A basic example can be found at "examples/localization/custom". |
| 18 |
Note that this is not the only way to localize your Castle Game Engine games. |
| 19 |
You can as well use resourcestrings and standard FPC GetText unit directly, |
| 20 |
as shown in the example code in "examples/localization/gettext". |
| 21 |
|
| 22 |
This unit is deprecated now, as CastleLocalizationGetText offers some additional |
| 23 |
features: |
| 24 |
|
| 25 |
@unorderedList( |
| 26 |
@item(Translating resource strings (so the constant strings in code are "magically" translated),) |
| 27 |
@item(GenerateGetTextPo (generating translation template),) |
| 28 |
@item(TranslateAllDesigns (automatic translation of all deserialized components),) |
| 29 |
@item(uses TCastleComponent.TranslateProperties (multiple translatable properties on a component are possible).) |
| 30 |
) |
| 31 |
} |
| 32 |
|
| 33 |
unit CastleLocalization deprecated 'use CastleLocalizationGetText instead, it offers more features for now (GenerateGetTextPo, TranslateAllDesigns, TCastleComponent.TranslateProperties)'; |
| 34 |
|
| 35 |
{$I castleconf.inc} |
| 36 |
{$ifdef FPC}{$interfaces corba}{$endif} |
| 37 |
|
| 38 |
interface |
| 39 |
|
| 40 |
uses |
| 41 |
Classes, Generics.Collections, |
| 42 |
CastleSystemLanguage, |
| 43 |
CastleControls, CastleOnScreenMenu; |
| 44 |
|
| 45 |
type |
| 46 |
{ Dictionary (LocalizationID/TranslatedText as String/String) for storing all translated strings of the current language. } |
| 47 |
TLanguageDictionary = {$ifdef FPC}specialize{$endif} TDictionary<String, String>; |
| 48 |
|
| 49 |
{ Procedure of a file loader called by CastleLocalization to fill the language dictionary from a file stream. } |
| 50 |
TFileLoaderAction = procedure(const AFileStream: TStream; const ALanguageDictionary: TLanguageDictionary); |
| 51 |
{ Dictionaty (FileExtension/FileLoaderAction as String/TFileLoaderAction) to connect the known file loaders with it's file extensions.} |
| 52 |
TFileLoaderDictionary = {$ifdef FPC}specialize{$endif} TDictionary<String, TFileLoaderAction>; |
| 53 |
|
| 54 |
{ Called by CastleLocalization to all subscribed procedures when a new language is set. } |
| 55 |
TOnLocalizationUpdatedEvent = procedure of object; |
| 56 |
TOnLocalizationUpdatedEventList = {$ifdef FPC}specialize{$endif} TList<TOnLocalizationUpdatedEvent>; |
| 57 |
|
| 58 |
{ Called by CastleLocalization to all subscribed components when a new language is set. } |
| 59 |
TOnUpdateLocalizationEvent = procedure(const ALocalizedText: String) of object; |
| 60 |
TOnUpdateLocalizationEventList = {$ifdef FPC}specialize{$endif} TList<TOnUpdateLocalizationEvent>; |
| 61 |
|
| 62 |
{ List (dictionary) for the localization IDs of all subscribed components. } |
| 63 |
TLocalizationIDList = {$ifdef FPC}specialize{$endif} TDictionary<TOnUpdateLocalizationEvent, String>; |
| 64 |
|
| 65 |
type |
| 66 |
{ Interface for custom user classes using the localization. |
| 67 |
In contrast to ICastleLocalization, the class doesn't need to be an inheritant from TComponent. |
| 68 |
Useful for lightweight custom classes that need localization and implement adding and removing from TCastleLocalization by themselves. } |
| 69 |
ICastleLocalizationCustom = interface |
| 70 |
['{d4cdfeb4-32c9-2409-07cc-00aa862851a4}'] |
| 71 |
procedure OnUpdateLocalization(const ALocalizedText: String); |
| 72 |
end; |
| 73 |
|
| 74 |
{ Interface for all user components using the localization. |
| 75 |
Allows to automatically localise and adjust a TComponent to language changes. } |
| 76 |
ICastleLocalization = interface (ICastleLocalizationCustom) |
| 77 |
['{4fa1cb64-f806-2409-07cc-ca1a77e5c0e4}'] |
| 78 |
procedure FreeNotification(AComponent: TComponent); |
| 79 |
end; |
| 80 |
|
| 81 |
type |
| 82 |
{ Main comonent for localization, singleton as @link(Localization). } |
| 83 |
TCastleLocalization = class (TComponent) |
| 84 |
protected |
| 85 |
FLanguageDictionary: TLanguageDictionary; |
| 86 |
FLanguageUrl: String; |
| 87 |
FFileLoaderDictionary: TFileLoaderDictionary; |
| 88 |
FLocalizationIDList: TLocalizationIDList; |
| 89 |
FOnUpdateLocalizationEventList: TOnUpdateLocalizationEventList; |
| 90 |
FOnLocalizationUpdatedEventList: TOnLocalizationUpdatedEventList; |
| 91 |
function Get(AKey: String): String; |
| 92 |
procedure LoadLanguage(const ALanguageUrl: String); |
| 93 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 94 |
function AddOrSet(AOnUpdateLocalizationEvent: TOnUpdateLocalizationEvent; const ALocalizationID: String): Boolean; overload; inline; |
| 95 |
procedure RemoveFromUpdateList(AOnUpdateLocalizationEvent: TOnUpdateLocalizationEvent); inline; |
| 96 |
public |
| 97 |
constructor Create(AOwner: TComponent); override; |
| 98 |
destructor Destroy; override; |
| 99 |
{ Returns the current system language as language code. |
| 100 |
For example: en, de, pl } |
| 101 |
function SystemLanguage(const ADefaultLanguage: String = SystemDefaultLanguage): String; inline; |
| 102 |
{ Returns the current system locale as langauge code and locale info. |
| 103 |
For example: en_US, en_GB, es_ES } |
| 104 |
function SystemLocale(const ADefaultLocale: String = SystemDefaultLocale): String; inline; |
| 105 |
{ Adds a new component to the automised localization list or, if it already is listed, updates it's localization ID. |
| 106 |
If ALocalizationID is empty, the element is removed from the localization list. } |
| 107 |
procedure AddOrSet(ALocalizationComponent: ICastleLocalization; const ALocalizationID: String); overload; |
| 108 |
{ Adds a new custom localization class to the automised localization list or, if it already is listed, updates it's localization ID. |
| 109 |
If ALocalizationID is empty, the element is removed from the localization list. } |
| 110 |
procedure AddOrSet(ALocalizationComponent: ICastleLocalizationCustom; const ALocalizationID: String); overload; |
| 111 |
public |
| 112 |
property Items[AKey: String]: String read Get; default; |
| 113 |
{ The URL to the language file that shall be loaded for localization. } |
| 114 |
property LanguageUrl: String read FLanguageUrl write LoadLanguage; |
| 115 |
{ A list (dictionary) of file loaders. |
| 116 |
You can use this to add custom file loader for new file extensions or overwrite existing ones to change the file format. } |
| 117 |
property FileLoader: TFileLoaderDictionary read FFileLoaderDictionary; |
| 118 |
{ A list of subscribed procedures of that each will be called when the langauge changes. |
| 119 |
You can add a procedure to this to localise images or such that is no descendent of TComponent. } |
| 120 |
property OnUpdateLocalization: TOnLocalizationUpdatedEventList read FOnLocalizationUpdatedEventList; |
| 121 |
end; |
| 122 |
|
| 123 |
var |
| 124 |
{ Singleton for TCastleLocalization. } |
| 125 |
Localization: TCastleLocalization; |
| 126 |
|
| 127 |
{$define read_interface} |
| 128 |
{$I castlelocalization_castlecore.inc} |
| 129 |
{$undef read_interface} |
| 130 |
|
| 131 |
implementation |
| 132 |
|
| 133 |
{$warnings off} // using deprecated unit here |
| 134 |
uses |
| 135 |
SysUtils, |
| 136 |
CastleUriUtils, CastleUtils, CastleDownload, |
| 137 |
CastleLocalizationFileLoader; |
| 138 |
{$warnings on} |
| 139 |
|
| 140 |
{$define read_implementation} |
| 141 |
{$I castlelocalization_castlecore.inc} |
| 142 |
{$undef read_implementation} |
| 143 |
|
| 144 |
////////////////////////// |
| 145 |
//Constructor/Destructor// |
| 146 |
////////////////////////// |
| 147 |
|
| 148 |
constructor TCastleLocalization.Create(AOwner: TComponent); |
| 149 |
begin |
| 150 |
inherited; |
| 151 |
|
| 152 |
FLanguageDictionary := TLanguageDictionary.Create; |
| 153 |
FFileLoaderDictionary := TFileLoaderDictionary.Create; |
| 154 |
FLocalizationIDList := TLocalizationIDList.Create; |
| 155 |
FOnLocalizationUpdatedEventList := TOnLocalizationUpdatedEventList.Create; |
| 156 |
FOnUpdateLocalizationEventList := TOnUpdateLocalizationEventList.Create; |
| 157 |
end; |
| 158 |
|
| 159 |
destructor TCastleLocalization.Destroy; |
| 160 |
begin |
| 161 |
FreeAndNil(FOnUpdateLocalizationEventList); |
| 162 |
FreeAndNil(FOnLocalizationUpdatedEventList); |
| 163 |
FreeAndNil(FLocalizationIDList); |
| 164 |
FreeAndNil(FFileLoaderDictionary); |
| 165 |
FreeAndNil(FLanguageDictionary); |
| 166 |
|
| 167 |
inherited; |
| 168 |
end; |
| 169 |
|
| 170 |
///////////////////// |
| 171 |
//Private/Protected// |
| 172 |
///////////////////// |
| 173 |
|
| 174 |
function TCastleLocalization.Get(AKey: String): String; |
| 175 |
begin |
| 176 |
if not FLanguageDictionary.TryGetValue(AKey, Result) then |
| 177 |
Result := AKey; //When no translation is found, return the key. |
| 178 |
end; |
| 179 |
|
| 180 |
procedure TCastleLocalization.LoadLanguage(const ALanguageUrl: String); |
| 181 |
var |
| 182 |
FileLoaderAction: TFileLoaderAction; |
| 183 |
Stream: TStream; |
| 184 |
LocalizedText: String; |
| 185 |
OnUpdateLocalizationEvent: TOnUpdateLocalizationEvent; |
| 186 |
OnLocalizationUpdatedEvent: TOnLocalizationUpdatedEvent; |
| 187 |
begin |
| 188 |
if FLanguageUrl = ALanguageUrl then Exit; |
| 189 |
FLanguageUrl := ALanguageUrl; |
| 190 |
|
| 191 |
FLanguageDictionary.Clear; |
| 192 |
|
| 193 |
if ALanguageUrl = '' then Exit; //If there's no language XML file, then that's it, no more localization. |
| 194 |
|
| 195 |
FFileLoaderDictionary.TryGetValue(ExtractFileExt(ALanguageUrl), FileLoaderAction); |
| 196 |
Check(Assigned(FileLoaderAction), 'There is no file loader associated with the extension of the given file.'); |
| 197 |
|
| 198 |
Stream := Download(AbsoluteURI(ALanguageUrl)); |
| 199 |
try |
| 200 |
FileLoaderAction(Stream, FLanguageDictionary); |
| 201 |
finally |
| 202 |
Stream.Free; |
| 203 |
end; |
| 204 |
|
| 205 |
//Tell every registered object to update its localization: |
| 206 |
for OnUpdateLocalizationEvent in FOnUpdateLocalizationEventList do |
| 207 |
begin |
| 208 |
FLocalizationIDList.TryGetValue(OnUpdateLocalizationEvent, LocalizedText); |
| 209 |
OnUpdateLocalizationEvent(Items[LocalizedText]); |
| 210 |
end; |
| 211 |
|
| 212 |
//Tell every custom object to update its localization: |
| 213 |
for OnLocalizationUpdatedEvent in FOnLocalizationUpdatedEventList do |
| 214 |
OnLocalizationUpdatedEvent(); |
| 215 |
end; |
| 216 |
|
| 217 |
{$NOTES OFF} //If not disabled, it will say LCastleLocalizationComponent was assigned but never used... but it IS used... |
| 218 |
procedure TCastleLocalization.Notification(AComponent: TComponent; Operation: TOperation); |
| 219 |
var |
| 220 |
LCastleLocalizationComponent: ICastleLocalization; |
| 221 |
begin |
| 222 |
if Operation = opRemove then |
| 223 |
begin |
| 224 |
AComponent.RemoveFreeNotification(Self); |
| 225 |
|
| 226 |
LCastleLocalizationComponent := AComponent as ICastleLocalization; |
| 227 |
RemoveFromUpdateList(@LCastleLocalizationComponent.OnUpdateLocalization); |
| 228 |
end; |
| 229 |
end; |
| 230 |
{$NOTES ON} |
| 231 |
|
| 232 |
function TCastleLocalization.AddOrSet(AOnUpdateLocalizationEvent: TOnUpdateLocalizationEvent; const ALocalizationID: String): Boolean; |
| 233 |
begin |
| 234 |
Result := not FLocalizationIDList.ContainsKey(AOnUpdateLocalizationEvent); |
| 235 |
FLocalizationIDList.AddOrSetValue(AOnUpdateLocalizationEvent, ALocalizationID); |
| 236 |
|
| 237 |
if Result then |
| 238 |
FOnUpdateLocalizationEventList.Add(AOnUpdateLocalizationEvent); |
| 239 |
end; |
| 240 |
|
| 241 |
procedure TCastleLocalization.RemoveFromUpdateList(AOnUpdateLocalizationEvent: TOnUpdateLocalizationEvent); |
| 242 |
begin |
| 243 |
FOnUpdateLocalizationEventList.Remove(AOnUpdateLocalizationEvent); |
| 244 |
FLocalizationIDList.Remove(AOnUpdateLocalizationEvent); |
| 245 |
end; |
| 246 |
|
| 247 |
////////////// |
| 248 |
////Public//// |
| 249 |
////////////// |
| 250 |
|
| 251 |
function TCastleLocalization.SystemLanguage(const ADefaultLanguage: String = SystemDefaultLanguage): String; |
| 252 |
begin |
| 253 |
Result := CastleSystemLanguage.SystemLanguage(ADefaultLanguage); |
| 254 |
end; |
| 255 |
|
| 256 |
function TCastleLocalization.SystemLocale(const ADefaultLocale: String = SystemDefaultLocale): String; |
| 257 |
begin |
| 258 |
Result := CastleSystemLanguage.SystemLocale(ADefaultLocale); |
| 259 |
end; |
| 260 |
|
| 261 |
procedure TCastleLocalization.AddOrSet(ALocalizationComponent: ICastleLocalization; const ALocalizationID: String); |
| 262 |
begin |
| 263 |
if ALocalizationID = '' then |
| 264 |
RemoveFromUpdateList(@ALocalizationComponent.OnUpdateLocalization) |
| 265 |
else |
| 266 |
begin |
| 267 |
if AddOrSet(@ALocalizationComponent.OnUpdateLocalization, ALocalizationID) then |
| 268 |
ALocalizationComponent.FreeNotification(Self); |
| 269 |
|
| 270 |
ALocalizationComponent.OnUpdateLocalization(Items[ALocalizationID]); |
| 271 |
end; |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TCastleLocalization.AddOrSet(ALocalizationComponent: ICastleLocalizationCustom; const ALocalizationID: String); |
| 275 |
begin |
| 276 |
if ALocalizationID = '' then |
| 277 |
RemoveFromUpdateList(@ALocalizationComponent.OnUpdateLocalization) |
| 278 |
else |
| 279 |
begin |
| 280 |
AddOrSet(@ALocalizationComponent.OnUpdateLocalization, ALocalizationID); |
| 281 |
|
| 282 |
ALocalizationComponent.OnUpdateLocalization(Items[ALocalizationID]); |
| 283 |
end; |
| 284 |
end; |
| 285 |
|
| 286 |
initialization |
| 287 |
Localization := TCastleLocalization.Create(nil); |
| 288 |
ActivateAllFileLoader; |
| 289 |
|
| 290 |
finalization |
| 291 |
FreeAndNil(Localization); |
| 292 |
|
| 293 |
end. |