我正在使用已安装的区域设置文件夹来获取应用程序的消息. 使用包含国际字符的帐户名时,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中运行(示例区域设置/目录安装在msys2 home中).同样,它使用test Gergő
帐户失败,而使用bll
和test user
帐户工作.