LCL, LCLTranslator: allow specifying custom translation files subdirectory when calling SetDefaultLang, bug #26688

git-svn-id: trunk@47312 -
This commit is contained in:
maxim 2015-01-06 00:14:56 +00:00
parent b4de0f46fc
commit cb3d2173e1
2 changed files with 47 additions and 10 deletions

View File

@ -35,6 +35,6 @@ implementation
initialization initialization
//It is safe to place code here as no form is initialized before unit //It is safe to place code here as no form is initialized before unit
//initialization is made //initialization is made
SetDefaultLang(''); SetDefaultLang('', '', false);
end. end.

View File

@ -72,7 +72,7 @@ type
PropInfo: PPropInfo; var Content: string); override; PropInfo: PPropInfo; var Content: string); override;
end; end;
procedure SetDefaultLang(Lang: string); procedure SetDefaultLang(Lang: string; Dir: string = ''; ForceUpdate: boolean = true);
implementation implementation
@ -82,12 +82,12 @@ uses
type type
TPersistentAccess = class(TPersistent); TPersistentAccess = class(TPersistent);
function FindLocaleFileName(LCExt: string; Lang: string): string; function FindLocaleFileName(LCExt: string; Lang: string; Dir: string): string;
var var
T: string; T: string;
i: integer; i: integer;
function GetLocaleFileName(const LangID, LCExt: string): string; function GetLocaleFileName(const LangID, LCExt: string; Dir: string): string;
var var
LangShortID: string; LangShortID: string;
AppDir,LCFileName,FullLCFileName: String; AppDir,LCFileName,FullLCFileName: String;
@ -98,6 +98,14 @@ var
LCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), LCExt); LCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), LCExt);
FullLCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), '.' + LangID) + LCExt; FullLCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), '.' + LangID) + LCExt;
if Dir<>'' then
begin
Result := AppDir + Dir + DirectorySeparator + LangID +
DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then
exit;
end;
//ParamStrUTF8(0) is said not to work properly in linux, but I've tested it //ParamStrUTF8(0) is said not to work properly in linux, but I've tested it
Result := AppDir + LangID + DirectorySeparator + LCFileName; Result := AppDir + LangID + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
@ -126,6 +134,15 @@ 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);
if Dir<>'' then
begin
Result := AppDir + Dir + DirectorySeparator +
LangShortID + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then
exit;
end;
//At first, check all was checked //At first, check all was checked
Result := AppDir + LangShortID + DirectorySeparator + LCFileName; Result := AppDir + LangShortID + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
@ -149,9 +166,17 @@ var
//Full language in file name - this will be default for the project //Full language in file name - this will be default for the project
//We need more careful handling, as it MAY result in incorrect filename //We need more careful handling, as it MAY result in incorrect filename
try try
if Dir<>'' then
begin
Result := AppDir + Dir + DirectorySeparator + FullLCFileName;
if FileExistsUTF8(Result) then
exit;
end;
Result := AppDir + FullLCFileName; Result := AppDir + FullLCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
//Common location (like in Lazarus) //Common location (like in Lazarus)
Result := AppDir + 'locale' + DirectorySeparator + FullLCFileName; Result := AppDir + 'locale' + DirectorySeparator + FullLCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
@ -172,6 +197,13 @@ var
FullLCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), '.' + LangShortID) + LCExt; FullLCFileName := ChangeFileExt(ExtractFileName(ParamStrUTF8(0)), '.' + LangShortID) + LCExt;
if Dir<>'' then
begin
Result := AppDir + Dir + DirectorySeparator + FullLCFileName;
if FileExistsUTF8(Result) then
exit;
end;
Result := AppDir + FullLCFileName; Result := AppDir + FullLCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
@ -204,7 +236,7 @@ begin
if Lang = '' then if Lang = '' then
LCLGetLanguageIDs(Lang, T); LCLGetLanguageIDs(Lang, T);
Result := GetLocaleFileName(Lang, LCExt); Result := GetLocaleFileName(Lang, LCExt, Dir);
if Result <> '' then if Result <> '' then
exit; exit;
@ -417,8 +449,13 @@ begin
end; end;
end; end;
procedure SetDefaultLang(Lang: string); procedure SetDefaultLang(Lang: string; Dir: string = ''; ForceUpdate: boolean = true);
{ Arguments:
Lang - language (e.g. 'ru', 'de'); empty argument is default language.
Dir - custom translation files subdirectory (e.g. 'mylng'); empty argument means searching only in predefined subdirectories.
ForceUpdate - true means forcing immediate interface update. Only should be set to false when the procedure is
called from unit Initialization section. User code normally should not specify it.
}
var var
Dot1: integer; Dot1: integer;
LCLPath: string; LCLPath: string;
@ -429,7 +466,7 @@ begin
LocalTranslator := nil; LocalTranslator := nil;
// search first po translation resources // search first po translation resources
try try
lcfn := FindLocaleFileName('.po', Lang); lcfn := FindLocaleFileName('.po', Lang, Dir);
if lcfn <> '' then if lcfn <> '' then
begin begin
Translations.TranslateResourceStrings(lcfn); Translations.TranslateResourceStrings(lcfn);
@ -451,7 +488,7 @@ begin
begin begin
// try now with MO traslation resources // try now with MO traslation resources
try try
lcfn := FindLocaleFileName('.mo', Lang); lcfn := FindLocaleFileName('.mo', Lang, Dir);
if lcfn <> '' then if lcfn <> '' then
begin begin
GetText.TranslateResourceStrings(UTF8ToSys(lcfn)); GetText.TranslateResourceStrings(UTF8ToSys(lcfn));
@ -479,7 +516,7 @@ begin
// Do not update the translations when this function is called from within // Do not update the translations when this function is called from within
// the unit initialization. // the unit initialization.
if (Lang<>'') then if ForceUpdate=true then
begin begin
for i := 0 to Screen.CustomFormCount-1 do for i := 0 to Screen.CustomFormCount-1 do
LocalTranslator.UpdateTranslation(Screen.CustomForms[i]); LocalTranslator.UpdateTranslation(Screen.CustomForms[i]);