delphi中如何检测多语言项目的系统语言?

发布于 2024-12-18 04:34:07 字数 561 浏览 1 评论 0原文

我需要用其他语言翻译一个程序,实际上我有 3 种语言(英语、西班牙语、葡萄牙语)的相同程序,但我翻译、重新编译,并且有 3 个单独的可执行文件。添加更多语言,保留链接,添加新功能让我发疯。

所以现在我决定保留一个可执行文件和一个外部语言文件,因此添加新语言不需要重新编译,只需用文本编辑器编辑语言文件,一切就可以了。

我想将所有语言保存在一个外部文件中。例如 international.lang

[portuguese]
greeting="Bem-vindo"

[spanish]
greeting="Ben venido"

如果文件 international.lang 不存在,或者文件中没有您的语言,程序将默认以英语启动,不会出现任何错误。就像大多数基于资源的多语言程序一样。

那么问题来了,在delphi中如何检测Windows语言呢? 对我的方法有什么想法吗? 有什么方法可以以编程方式替换对话框上的所有标题?

ps:我使用的是delphi7,我找不到任何好的免费组件。

I need to translate a program in others languages, actually I have the same program in 3 languages (english, spanish, portuguese), but I translated, recompiled, and I have 3 separate executables. And add more languages, and keep links, and adding new functions is driving me crazy.

So now I decided to keep a single executable, and a external language file, so adding new languages does not need recompiling, just editing the language file with a text editor, and everything is ok.

I want to keep all languages in a single external file. like international.lang

[portuguese]
greeting="Bem-vindo"

[spanish]
greeting="Ben venido"

if the file international.lang is not there, or your language is not on the file, the program will launch in english by default, with no errors. Just like most multi-languages programas based on resources.

So the question is, how detect the Windows language in delphi?
Any thoughts on my approach?
There is any way to replace all captions on dialogs programaticly?

ps: I'm using delphi7, and I can't find any component that is free that is good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-12-25 04:34:07

您可以使用 GetSystemDefaultLCID 函数获取区域设置标识符,然后使用 VerLanguageName 函数解析语言关联名称。或使用 GetLocaleInfo< /a> 函数

检查此示例

{$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.

注意:从 Windows Vista 开始,存在获取相同区域设置信息的新函数,请检查这些函数 GetLocaleInfoExGetUserDefaultLocaleNameGetSystemDefaultLocaleName

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

Check 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 exists new functions to get the same locale information, check these functions GetLocaleInfoEx, GetUserDefaultLocaleName and GetSystemDefaultLocaleName

梦幻的味道 2024-12-25 04:34:07

我有同样的问题,尽管我只需要处理两种语言:英语(默认)和波兰语。
我尝试了上面列出的所有解决方案,但没有一个有效。我正在更改系统设置、重新启动等,并且始终收到英语语言。
当切换到波兰语时,所有内容都以波兰语显示,所有波兰语区域设置都已设置,但我的应用程序接收英语作为操作系统语言。经过多次尝试,我遇到了非常简单且可靠的解决方法(我不称之为解决方案),如果您必须处理少量语言,那么这很好。
因此,诀窍是检查 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;

您可以对三种语言执行相同的操作。
希望有帮助。

I have the same problem although I have to deal with only two languages: English (default) and Polish.
I tried all the solutions listed above and none of them was working. I was changing system setting, rebooting etc. and always receiving language English.
When switched to Polish everything was displayed in Polish, all Polish locales were set but my application was receiving English as the OS language. After many tries I came across with quite easy and reliable workaround (I do not call it solution) that is good if you have to deal with a small number of languages.
So the trick is to check in what language the language list is returned by 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 it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文