一个简单的gettext示例不适用于linux

问题描述

我是C语言的gettext的新手,正尝试从simple example开始。

但是我没有得到结果。这是我的工作结构和源代码。我正在使用Linux。

gettext
 |- hello.c
 |- hello (executable)
 |- po
    |- hello.pot
    |- fr
       |- hello.po
       |- hello.mo
 |- fr
    |- LC_MESSAGES
        |- hello.mo

hello.c

#include <stdio.h>
#include <stdlib.h>

#include <libintl.h>
#include <locale.h>

#define _(STRING) gettext(STRING)

int main()
{
    setlocale(LC_ALL,"");
    bindtextdomain("hello",getenv("PWD"));
    textdomain("hello");

    printf(_("Hello!\n"));

    return 0;
}

po / hello.pot

# SOME DESCRIPTIVE TITLE.
# copyright (C) 2020 hardboiled65
# This file is distributed under the same license as the PACKAGE package.
# FirsT AUTHOR <EMAIL@ADDRESS>,YEAR.
#
#,fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Hello 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-16 19:11+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-transfer-encoding: 8bit\n"

#: hello.c:15
#,c-format
msgid "Hello!\n"
msgstr ""

po / fr / hello.po

# french translations for Hello package.
# copyright (C) 2020 hardboiled65
# This file is distributed under the same license as the Hello package.
# hardboiled65 <hardboiled65@probook>,2020.
#
msgid ""
msgstr ""
"Project-Id-Version: Hello 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-16 19:11+0900\n"
"PO-Revision-Date: 2020-09-16 19:14+0900\n"
"Last-Translator: hardboiled65 <hardboiled65@probook>\n"
"Language-Team: french <Traduc@Traduc.org>\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-transfer-encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: hello.c:15
#,c-format
msgid "Hello!\n"
msgstr "Bonjour!\n"

问题是,当我执行LANG=fr_FR ./hello时,它显示的是英语而不是法语。

这是strace结果(LANG=fr_FR strace ./hello)。

execve("./hello",["./hello"],0x7ffe98223800 /* 67 vars */) = 0
brk(NULL)                               = 0x55f5d6a8c000
arch_prctl(0x3001 /* ARCH_??? */,0x7fff277f8300) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload",R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD,"/etc/ld.so.cache",O_RDONLY|O_CLOEXEC) = 3
fstat(3,{st_mode=S_IFREG|0644,st_size=239979,...}) = 0
mmap(NULL,239979,PROT_READ,MAP_PRIVATE,3,0) = 0x7f7a399d5000
close(3)                                = 0
openat(AT_FDCWD,"/usr/lib/libc.so.6",O_RDONLY|O_CLOEXEC) = 3
read(3,"\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\202\2\0\0\0\0\0"...,832) = 832
pread64(3,"\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"...,784,64) = 784
pread64(3,"\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0",32,848) = 32
pread64(3,"\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\364[g\253(\257\25\201\313\250\344q>\17\323\262"...,68,880) = 68
fstat(3,{st_mode=S_IFREG|0755,st_size=2159552,8192,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0) = 0x7f7a399d3000
pread64(3,64) = 784
mmap(NULL,1868448,MAP_PRIVATE|MAP_DENYWRITE,0) = 0x7f7a3980a000
mmap(0x7f7a39830000,1363968,PROT_READ|PROT_EXEC,MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE,0x26000) = 0x7f7a39830000
mmap(0x7f7a3997d000,311296,0x173000) = 0x7f7a3997d000
mmap(0x7f7a399c9000,24576,0x1be000) = 0x7f7a399c9000
mmap(0x7f7a399cf000,12960,MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,0) = 0x7f7a399cf000
close(3)                                = 0
mmap(NULL,0) = 0x7f7a39808000
arch_prctl(ARCH_SET_FS,0x7f7a399d4540) = 0
mprotect(0x7f7a399c9000,12288,PROT_READ) = 0
mprotect(0x55f5d5e4d000,4096,PROT_READ) = 0
mprotect(0x7f7a39a3c000,PROT_READ) = 0
munmap(0x7f7a399d5000,239979)          = 0
openat(AT_FDCWD,"/usr/lib/locale/locale-archive",st_size=4383248,4383248,0) = 0x7f7a393d9000
close(3)                                = 0
brk(NULL)                               = 0x55f5d6a8c000
brk(0x55f5d6aad000)                     = 0x55f5d6aad000
openat(AT_FDCWD,"/usr/share/locale/locale.alias",st_size=2998,...}) = 0
read(3,"# Locale name alias data base.\n#"...,4096) = 2998
read(3,"",4096)                       = 0
close(3)                                = 0
openat(AT_FDCWD,"/usr/lib/locale/fr_FR/LC_IDENTIFICATION",O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD,"/usr/lib/locale/fr/LC_IDENTIFICATION",O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
fstat(1,{st_mode=S_IFCHR|0600,st_rdev=makedev(0x88,0xa),...}) = 0
write(1,"Hello!\n",7Hello!
)                 = 7
exit_group(0)                           = ?
+++ exited with 0 +++

似乎没有搜索.mo放置的目录(在此示例中为PWD)。

我的尝试有什么问题?甚至我也将.mo文件放置到/usr/local/share/locale/fr/LC_MESSAGES并将语言环境更改为fr_FR,但程序仍显示“ Hello!”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)