我正在使用已安装的区域设置文件夹来获取应用程序的消息. 使用包含国际字符的帐户名时,getText库无法加载和转换文本字符串.

以前有没有人遇到过这种情况?有变通的办法吗?

我在使用msys2中的UCRT64环境.

创建名为:test Gergő的用户帐户并设置msys2.

test Gergő帐户中:

cd "/c/Users/test Gergő"
mkdir -p locale/nl/LC_MESSAGES
cp /usr/share/locale/nl/LC_MESSAGES/sed.mo locale/nl/LC_MESSAGES/
cd 

使用以下测试代码:(我将其命名为tt.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <wchar.h>
#include <windows.h>

#define DOM "sed"

int
main (int argc, char *argv [])
{
  wchar_t *wtmp;
  char    buff [512];
  char    tbuff [512];
  char    *tmp;
  size_t  len;


  wtmp = _wgetenv (L"USERPROFILE");
  // wtmp = _wgetenv (L"HOME");
  len = WideCharToMultiByte (CP_UTF8, 0, wtmp, -1, NULL, 0, NULL, NULL);
  WideCharToMultiByte (CP_UTF8, 0, wtmp, -1, buff, len + 1, NULL, NULL);
  wtmp [len] = L'\0';

  sprintf (tbuff, "%s/locale", buff);
  // or vice-versa. fails both ways.
  // just to be sure the path is consistent.
  for (int i = 0; i < strlen (tbuff); ++i) {
    if (tbuff [i] == '\\') {
      tbuff [i] = '/';
    }
  }
  tmp = bindtextdomain (DOM, tbuff);
  fprintf (stderr, "bind: %s\n", tmp);

  tmp = textdomain (DOM);
  fprintf (stderr, "text-dom: %s\n", tmp);

  if (setlocale (LC_MESSAGES, tbuff) == NULL) {
    fprintf (stderr, "set-locale failed\n");
  }

  _wputenv_s (L"LC_MESSAGES", L"nl.UTF-8");

  tmp = gettext ("No match");
  if (strcmp (tmp, "No match") == 0) {
    fprintf (stderr, "NG %s\n", tmp);
  } else {
    fprintf (stderr, "OK %s\n", tmp);
  }

  return 0;
}

编译:

cc -o tt tt.c -static -lintl -liconv

在cmd.exe窗口中运行该程序:

C:\msys64\home\test Gergő\tt.exe

找不到sed.mo中的字符串"No Match".

bind: C:\Users\test Gergő\locale
text-dom: sed
NG No match

这在使用帐户名blltest user时有效.

还可以编译该程序以检索HOME环境变量并在msys2中运行(示例区域设置/目录安装在msys2 home中).同样,它使用test Gergő帐户失败,而使用blltest user帐户工作.

推荐答案

原来,libintl包含一个wbindtextdomain函数.我翻遍了GetText的源代码,发现了这个.手册页中没有记录,但在getText信息文件中有记录.

为了让cmake找到这个函数,我判断了libintl_wbindtextdomain函数名.

C++相关问答推荐

字符串令牌化xpath表达式

在32位处理器上优化53—32位模计算>

为什么写入系统调用打印的字符数不正确?

自定义应用程序上的日志(log)轮换问题

难以理解Makefile隐含规则

为什么GCC在每次循环迭代时都会生成一个数组的mov&S使用[]访问数组?(-03,x86)

为什么STM32G474RE上没有启用RCC PLL

轮询libusb_pollfd struct 列表的正确方式是什么?

解决S随机内存分配问题,实现跨进程高效数据共享

如何有效地编写代码来判断两个元素数量相同的数组即使在不同的位置也具有相同的元素?

防止C++中递归函数使用堆栈内存

For循环中的变量行为不符合预期.[C17]

在for循环中指向数组开头之前

安全倒计时循环

错误:字符串在C中获得意外输出

带有数组指针的 struct 在print_stack()函数中打印随机数

Malloc和对齐

将char*数组深度复制到 struct 中?

C 中从 Unix 纪元时间转换的损坏

Zig 中 C 的system函数的惯用替代方案