gettext 不适用于带有国际字符的帐户名

问题描述 投票:0回答:1

我正在使用已安装的区域设置文件夹来获取我的应用程序的消息。 当使用带有国际字符的帐户名时,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

这适用于帐户名称

bll
test user

该程序还可以编译以检索 HOME 环境变量并在 msys2 中运行(示例 locale/ 目录安装在 msys2 主目录中)。使用

test Gergő
帐户再次失败,但使用
bll
test user
帐户则可以。

c windows gettext msys2
1个回答
0
投票

原来 libintl 包含一个

wbindtextdomain
函数。我正在挖掘 gettext 源代码并发现了这个。它没有记录在手册页中,而是记录在 gettext 信息文件中。

为了让 cmake 找到这个函数,我检查了

libintl_wbindtextdomain
函数名称。

© www.soinside.com 2019 - 2024. All rights reserved.