LCL: translator: comments and published GetDefaultLang, issue #27440, from wp

git-svn-id: trunk@47683 -
This commit is contained in:
mattias 2015-02-10 15:59:42 +00:00
parent 6c8327066e
commit c2545d0502
2 changed files with 26 additions and 7 deletions

View File

@ -7,7 +7,9 @@
filename it must be relative to the location of the exe. filename it must be relative to the location of the exe.
Select the option to automatically update the po file. Select the option to automatically update the po file.
- Add DefaultTranslator to uses clause of main form - Add DefaultTranslator or LCLTranslator to uses clause of main form
(DefaultTranslator determines the default language automatically,
LCLTranslator does not).
- If the project contains several forms that need translation: - If the project contains several forms that need translation:
- Copy LocalizedForms.* (to be found in this project) to the folder of - Copy LocalizedForms.* (to be found in this project) to the folder of
@ -23,7 +25,7 @@
- Declare each string that needs to be translated as a resourcestring. This - Declare each string that needs to be translated as a resourcestring. This
is not absolutely necessary for component properties "Caption", "Text" or is not absolutely necessary for component properties "Caption", "Text" or
"Hint" which are transparently handled by DefaultTranslator. "Hint" which are transparently handled by Default/LCLTranslator.
Explicitly declared resource strings are required for stringlist items, Explicitly declared resource strings are required for stringlist items,
such as those of comboboxes, radiogroups etc. such as those of comboboxes, radiogroups etc.
@ -116,7 +118,7 @@ begin
{ Another comment: The strings used in "MessageDlg" can be translated by { Another comment: The strings used in "MessageDlg" can be translated by
copying the files "lclstrconsts.*.po" to the languages folder. copying the files "lclstrconsts.*.po" to the languages folder.
DefaultTranslater then includes these strings as well. Please note that LCL/DefaultTranslater then includes these strings as well. Please note that
we did not copy these files in this demo project to avoid duplication of we did not copy these files in this demo project to avoid duplication of
Lazarus files. } Lazarus files. }
end; end;
@ -148,6 +150,8 @@ begin
{ Lets start the program with English translation by default. You could also { Lets start the program with English translation by default. You could also
store language in a configuration file and apply that selection here. } store language in a configuration file and apply that selection here. }
SelectLanguage('en'); SelectLanguage('en');
{ OR: Start the program with system's default language:
SelectLanguage(GetDefaultLang); }
end; end;
{ Another example how to combine translated strings, in this case for a { Another example how to combine translated strings, in this case for a
@ -171,7 +175,7 @@ var
i, p: Integer; i, p: Integer;
lang: String; lang: String;
begin begin
// Switch language - this is in DefaultTranslator // Switch language - this is in LCLTranslator
SetDefaultLang(ALang); SetDefaultLang(ALang);
// Switch default settings by calling the procedure provided in BasicLocalizedForm.pas. // Switch default settings by calling the procedure provided in BasicLocalizedForm.pas.
@ -204,20 +208,20 @@ begin
end; end;
{ This method is inherited from LocalizedForm and manually inserts translated { This method is inherited from LocalizedForm and manually inserts translated
strings in cases where DefaultTranslator cannot do this. } strings in cases where LCL/DefaultTranslator cannot do this. }
procedure TMainForm.UpdateTranslation(ALang: String); procedure TMainForm.UpdateTranslation(ALang: String);
begin begin
inherited; inherited;
{ The items of the radiogroup are not automatically handled by { The items of the radiogroup are not automatically handled by
DefaultTranslator. Therefore, we have to assign the strings to the LCL/DefaultTranslator. Therefore, we have to assign the strings to the
translated versions explicitly. } translated versions explicitly. }
RgDrinks.Items[0] := rsBeer; RgDrinks.Items[0] := rsBeer;
RgDrinks.Items[1] := rsWine; RgDrinks.Items[1] := rsWine;
RgDrinks.Items[2] := rsWater; RgDrinks.Items[2] := rsWater;
{ The label LblCurrentSelection is created by a Format statement. Since { The label LblCurrentSelection is created by a Format statement. Since
DefaultTranslator does not execute code we have to update the translation LCL/DefaultTranslator does not execute code we have to update the translation
of the label here. It is sufficient to call RgDrinksClick here where the of the label here. It is sufficient to call RgDrinksClick here where the
caption is re-composed by means of the Format statement. } caption is re-composed by means of the Format statement. }
RgDrinksClick(nil); RgDrinksClick(nil);

View File

@ -73,6 +73,7 @@ type
end; end;
procedure SetDefaultLang(Lang: string; Dir: string = ''; ForceUpdate: boolean = true); procedure SetDefaultLang(Lang: string; Dir: string = ''; ForceUpdate: boolean = true);
function GetDefaultLang: String;
implementation implementation
@ -82,6 +83,9 @@ uses
type type
TPersistentAccess = class(TPersistent); TPersistentAccess = class(TPersistent);
var
DefaultLang: String = '';
function FindLocaleFileName(LCExt: string; Lang: string; Dir: string): string; function FindLocaleFileName(LCExt: string; Lang: string; Dir: string): string;
var var
T: string; T: string;
@ -92,6 +96,8 @@ var
LangShortID: string; LangShortID: string;
AppDir,LCFileName,FullLCFileName: String; AppDir,LCFileName,FullLCFileName: String;
begin begin
DefaultLang := LangID;
if LangID <> '' then if LangID <> '' then
begin begin
AppDir := ExtractFilePath(ParamStrUTF8(0)); AppDir := ExtractFilePath(ParamStrUTF8(0));
@ -134,6 +140,7 @@ var
{$ENDIF} {$ENDIF}
//Let us search for reducted files //Let us search for reducted files
LangShortID := copy(LangID, 1, 2); LangShortID := copy(LangID, 1, 2);
Defaultlang := LangShortID;
if Dir<>'' then if Dir<>'' then
begin begin
@ -218,6 +225,7 @@ var
end; end;
Result := ''; Result := '';
DefaultLang := '';
end; end;
begin begin
@ -245,6 +253,7 @@ begin
exit; exit;
Result := ''; Result := '';
DefaultLang := '';
end; end;
function GetIdentifierPath(Sender: TObject; function GetIdentifierPath(Sender: TObject;
@ -524,6 +533,12 @@ begin
end; end;
end; end;
function GetDefaultLang: String;
begin
if DefaultLang = '' then SetDefaultLang('');
GetDefaultLang := DefaultLang;
end;
finalization finalization
LRSTranslator.Free; LRSTranslator.Free;