How can I read native text of current locale?

本文介绍了一种使用 Windows API 中的 VerLanguageName 函数来读取当前系统所在地区的本地语言名称的方法。通过简单的 Delphi 代码示例,展示了如何获取如英语、俄语等语言的本地名称,并提供了几个具体的例子。

How can I read native text of current locale?
Sometimes in application we want to show a text with description of current used locale.
For example, "english", "russian", "ukrainian", "chinese" etc

For this task you can use VerLanguageName function from Windows API:

var
  ID: LangID;
  LanguageName: array[0..255] of Char;
begin
  {read current system locale}
  ID := GetSystemDefaultLangID;

  {convert an ID into text}
  VerLanguageName(ID, LanguageName, SizeOf(LanguageName));

  ShowMessage(LanguageName);
end;

Of course, you can expand this code and to translate a locale as parameter

For example, if ID is $419, you'll receive a "russian". For $$0A - the "Spanish (Traditional Sort)", for $0C0C - the "French (Canadian)", for $407 - "German (Standard)" etc


Important

if ID is incorrect, you'll receive an "Language Neutral" as result
text in result will be translated in the current language. I mean that text will be not in English if you have localized version of MS Windows 

To translate the names of image files in a folder into English using Qt, you can follow these steps: 1. **Include necessary libraries**: First, make sure you have included the `QtWidgets` and `QtGui` libraries, as they provide file handling and translation support. ```cpp #include <QApplication> #include <QDir> #include <QTranslator> #include <QTextCodec> ``` 2. **Create a translator object**: Instantiate a `QTranslator` instance and load an English translation resource: ```cpp QTranslator translator; QFile langFile(QStringLiteral("en_US.qm")); // Replace "en_US" with your desired language code if (langFile.open(QIODevice::ReadOnly | QIODevice::Text)) { translator.load(langFile); } ``` 3. **Detect current locale**: Use `QLocale` to determine the user's system language if needed, or force it to English: ```cpp QLocale locale; if (!translator.loadFromResource(QStringLiteral("qt"), QStringLiteral("en"))) { // Forcing English locale = QLocale(QLatin1String("en")); } ``` 4. **Iterate over files and translate their names**: Loop through the images in the folder, get the original name, apply the translation, and update the file name: ```cpp QDir directory("path/to/folder"); foreach (const QString &fileName, directory.entryList()) { if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) { QString untranslatedName = QDir::toNativeSeparators(fileName); QString translatedName = translator.translate(untranslatedName); if (!translatedName.isEmpty()) { QString newFileName = translatedName + QLatin1String("."); // Add period back for file extension QFile source(untranslatedName); QFile target(newFileName); if (source.exists() && !target.exists()) { source.rename(target); } } } } ``` 5. **Handle exceptions**: Don't forget to handle any errors that might occur during file operations. 6. **Run the application**: Initialize your `QApplication` and run the main loop. ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); // ... Rest of your program logic return app.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值