How to determine the system language in delphi for a multilingual project?

I need to translate the program into other languages, in fact I have the same program in three languages ​​(English, Spanish, Portuguese), but I translated, recompiled, and I have 3 separate executable files. And add more languages, and save links, and adding new features is driving me crazy.

So, now I decided to save one executable file and an external language file, so adding new languages ​​does not require recompilation, just editing the language file using a text editor, and everything is in order.

I want to save all languages ​​in one external file. e.g. international.lang

 [portuguese] greeting="Bem-vindo" [spanish] greeting="Ben venido" 

if the international.lang file is not present, or your language is not in the file, the program starts in English by default, without errors. Like most programs in multiple resource-based languages.

So the question is: how to determine the Windows language in delphi? Any thoughts on my approach? Is there any way to programmatically replace all captions with dialogs?

ps: I am using delphi7 and I cannot find any free component.

+6
source share
2 answers

You can use the GetSystemDefaultLCID function to get the locale ID, and then use VerLanguageName to resolve the name associated with the language. or use the GetLocaleInfo function

Check out this sample.

 {$APPTYPE CONSOLE} uses Windows, SysUtils; procedure Test_VerLanguageName; var wLang : LangID; szLang: Array [0..254] of Char; begin wLang := GetSystemDefaultLCID; VerLanguageName(wLang, szLang, SizeOf(szLang)); Writeln(szLang); end; procedure Test_GetLocaleInfo; var Buffer : PChar; Size : integer; begin Size := GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, nil, 0); GetMem(Buffer, Size); try GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, Buffer, Size); Writeln(Buffer); finally FreeMem(Buffer); end; end; begin try Test_VerLanguageName; Test_GetLocaleInfo; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; Readln; end. 

Note. Starting with Windows Vista, there are new functions for obtaining the same locale information; check these GetLocaleInfoEx , GetUserDefaultLocaleName and GetSystemDefaultLocaleName

+9
source

I have the same problem, although I only have to deal with two languages: English (default) and Polish. I tried all the solutions listed above and none of them worked. I changed the system settings, rebooted, etc. And I always received language English. When switching to Polish, everything was displayed in Polish, all Polish locations were installed, but my application received English as the OS language. After many attempts, I came across a fairly simple and reliable workaround (I do not call this a solution), which is good if you have to deal with a small number of languages. So, the trick is to check which language the language list returns TLanguages.

 function GetLang: Integer; //lcid const lcidEnglish = $9; lcidPolish = $415; var Idx: Integer; begin Result := Languages.IndexOf(lcidPolish); if (Result > 0) and (Languages.Name[Result].StartsWith('Polski', True)) //'Polski'is the Polish name of the language then Result := lcidPolish else Result := lcidEnglish; end; 

You can do the same for your three languages. Hope this helps.

0
source

Source: https://habr.com/ru/post/902448/


All Articles