LCL, LCLTranslator: when searching for translation files, use properly extracted language code (do not assume that it has fixed two-symbol length)

This commit is contained in:
Maxim Ganetsky 2023-06-12 16:29:08 +03:00
parent f2ce2e18e3
commit 9c8663e0f6

View File

@ -92,9 +92,8 @@ type
var var
DefaultLang: String = ''; DefaultLang: String = '';
procedure FindLang(var Lang: string); function FindLang(var Lang: string): TLanguageID;
var var
LangID: TLanguageID;
CurParam: string; CurParam: string;
i: integer; i: integer;
begin begin
@ -117,14 +116,16 @@ begin
if Lang = '' then if Lang = '' then
begin begin
LangID := GetLanguageID; Result := GetLanguageID;
Lang := LangID.LanguageID; Lang := Result.LanguageID;
end; end
else
Result := GetLanguageIDFromLocaleName(Lang);
end; end;
function FindLocaleFileName(LCExt, LangID, Dir, LocaleFileName: string; out FoundLang: string): string; function FindLocaleFileName(LCExt, LangID, Dir, LocaleFileName: string; out FoundLang: string): string;
var var
LangShortID: string; LangData: TLanguageID;
AppDir, LCFileName, FullLCFileName: string; AppDir, LCFileName, FullLCFileName: string;
function GetLCFileName: string; function GetLCFileName: string;
@ -137,7 +138,7 @@ var
begin begin
Result := ''; Result := '';
FindLang(LangID); LangData := FindLang(LangID);
FoundLang := LangID; FoundLang := LangID;
@ -188,33 +189,32 @@ begin
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
{$ENDIF} {$ENDIF}
//Let us search for short id files //Let us search for files with short language code (without country code)
LangShortID := copy(LangID, 1, 2); FoundLang := LangData.LanguageCode;
FoundLang := LangShortID;
if Dir<>'' then if Dir<>'' then
begin begin
Result := Dir + LangShortID + DirectorySeparator + LCFileName; Result := Dir + LangData.LanguageCode + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
end; end;
//At first, check all was checked //At first, check all was checked
Result := AppDir + LangShortID + DirectorySeparator + LCFileName; Result := AppDir + LangData.LanguageCode + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
Result := AppDir + 'languages' + DirectorySeparator + Result := AppDir + 'languages' + DirectorySeparator +
LangShortID + DirectorySeparator + LCFileName; LangData.LanguageCode + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
Result := AppDir + 'locale' + DirectorySeparator Result := AppDir + 'locale' + DirectorySeparator
+ LangShortID + DirectorySeparator + LCFileName; + LangData.LanguageCode + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
Result := AppDir + 'locale' + DirectorySeparator + LangShortID + Result := AppDir + 'locale' + DirectorySeparator + LangData.LanguageCode +
DirectorySeparator + 'LC_MESSAGES' + DirectorySeparator + LCFileName; DirectorySeparator + 'LC_MESSAGES' + DirectorySeparator + LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
@ -240,13 +240,13 @@ begin
exit; exit;
{$IFDEF UNIX} {$IFDEF UNIX}
Result := '/usr/share/locale/' + LangShortID + '/LC_MESSAGES/' + Result := '/usr/share/locale/' + LangData.LanguageCode + '/LC_MESSAGES/' +
LCFileName; LCFileName;
if FileExistsUTF8(Result) then if FileExistsUTF8(Result) then
exit; exit;
{$ENDIF} {$ENDIF}
FullLCFileName := ChangeFileExt(GetLCFileName, '.' + LangShortID) + LCExt; FullLCFileName := ChangeFileExt(GetLCFileName, '.' + LangData.LanguageCode) + LCExt;
if Dir<>'' then if Dir<>'' then
begin begin
@ -556,7 +556,7 @@ end;
function SetDefaultLang(Lang: string; Dir: string = ''; LocaleFileName: string = ''; ForceUpdate: boolean = true): string; function SetDefaultLang(Lang: string; Dir: string = ''; LocaleFileName: string = ''; ForceUpdate: boolean = true): string;
{ Arguments: { Arguments:
Lang - language (e.g. 'ru', 'de'); empty argument is default language. Lang - language (e.g. 'ru', 'de', 'zh_CN'); empty argument is default language.
Dir - custom translation files subdirectory (e.g. 'mylng'); empty argument means searching only in predefined subdirectories. Dir - custom translation files subdirectory (e.g. 'mylng'); empty argument means searching only in predefined subdirectories.
LocaleFileName - custom translation file name; empty argument means that the name is the same as the one of executable. LocaleFileName - custom translation file name; empty argument means that the name is the same as the one of executable.
ForceUpdate - true means forcing immediate interface update. Only should be set to false when the procedure is ForceUpdate - true means forcing immediate interface update. Only should be set to false when the procedure is