netstat 输出中的语言

netstat 输出中的语言

我这里有一个程序,它取决于netstat.更具体:netstat -apn.

这是正常输出的示例。

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -

我的一个客户有一些其他输出(作为示例):

Proto Recv-Q Send-Q Endereço Local          Endereço Remoto         Estado      PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               OUÇA       -

我想使用更改后的输出netstat(例如客户端的输出)来测试我的软件。

所以问题是:我应该如何更改 的输出语言netstat,以便我可以重现客户端收到的错误?

我已经尝试使用 更改语言export LANG=pt_PT,但输出没有改变。

答案1

首先,您需要确保您的计算机已设置必要的区域设置。您可以使用 来查看可用的区域设置locale -a。例如,在我的系统上:

$ locale -a
C
C.UTF-8
el_GR.utf8
en_US.utf8
fr_FR.utf8
POSIX

正如您在上面看到的,我没有葡萄牙语区域​​设置。在我的 Debian 上,我可以通过运行sudo dpkg-reconfigure locales并选择相关区域设置来创建它:

封装配置

 ┌──────────────────────────┤ Configuring locales ├──────────────────────────┐  
 │ Locales are a framework to switch between multiple languages and allow    │  
 │ users to use their language, country, characters, collation order, etc.   │  
 │                                                                           │  
 │ Please choose which locales to generate. UTF-8 locales should be chosen   │  
 │ by default, particularly for new installations. Other character sets may  │  
 │ be useful for backwards compatibility with older systems and software.    │  
 │                                                                           │  
 │ Locales to be generated:                                                  │  
 │                                                                           │  
 │    [ ] pt_BR.UTF-8 UTF-8                                              ↑   │  
 │    [ ] pt_PT ISO-8859-1                                               ▒   │  
 │    [*] pt_PT.UTF-8 UTF-8                                              ▮   │  
 │    [ ] pt_PT@euro ISO-8859-15                                         ▒   │  
 │    [ ] quz_PE UTF-8                                                   ↓   │  
 │                                                                           │  
 │                                                                           │  
 │                    <Ok>                        <Cancel>                   │  
 │                                                                           │  
 └───────────────────────────────────────────────────────────────────────────┘  

点击 后Enter,您应该得到:

$ sudo dpkg-reconfigure locales
Generating locales (this might take a while)...
  el_GR.UTF-8... done
  en_US.UTF-8... done
  fr_FR.UTF-8... done
  pt_PT.UTF-8... done
Generation complete.

如果您不使用基于 Debian 的发行版,则需要手动执行此操作:

  1. 将相关区域设置名称添加到/etc/locale.gen.例如:

    pt_PT.UTF-8 UTF-8
    
  2. 跑步locale-gen

经过上述步骤(手动方法或单独方法dpkg-reconfigre locales)后,locale -a还将列出葡萄牙语区域​​设置:

$ locale -a
C
C.UTF-8
el_GR.utf8
en_US.utf8
fr_FR.utf8
POSIX
pt_PT.utf8

现在,您可以选择输出的显示语言:

$ LC_ALL=el_GR.utf8 date
Τετ 14 Οκτ 2015 12:34:28 μμ EEST
$ LC_ALL=fr_FR.utf8 date
mercredi 14 octobre 2015, 12:35:07 (UTC+0300)
$ LC_ALL=pt_PT.utf8 date
Qua Out 14 12:35:11 EEST 2015

然而,特定程序是否能够在所选语言环境中打印输出将取决于它是否已被翻译;软件包是否附带相关.mo翻译文件。在 的情况下netstat,这是/usr/share/locale/${LANG%_*}/LC_MESSAGES/net-tools.mo。在我的 Debian 上,法语语言环境有它,但葡萄牙语语言环境没有:

$ ls /usr/share/locale/{fr,pt}/LC_MESSAGES/net*
ls: cannot access /usr/share/locale/pt/LC_MESSAGES/net*: No such file or directory
/usr/share/locale/fr/LC_MESSAGES/net-tools.mo

这就是为什么我可以netstat用法语跑步:

$ LC_ALL=fr_FR.utf8 netstat -apn | head -n2
(Tous les processus ne peuvent être identifiés, les infos sur les processus
non possédés ne seront pas affichées, vous devez être root pour les voir toutes.)
Connexions Internet actives (serveurs et établies)
Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat        PID/Program name

但葡萄牙语则不然:

$ LC_ALL=pt_PT.utf8 netstat -apn | head -n2
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

答案2

您需要安装您选择的语言的语言包,否则文本翻译实用程序(获取文本在 GNU 系统上)无法将消息翻译成您选择的语言:

$ strace -fe trace=open sh -c 'LANGUAGE=fr_FR.utf8 netstat -tnlp'
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
Process 14847 attached
[pid 14847] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[pid 14847] open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[pid 14847] open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
[pid 14847] open("/proc/3690/cmdline", O_RDONLY) = 5
[pid 14847] open("/proc/8579/cmdline", O_RDONLY) = 5
[pid 14847] open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3
[pid 14847] open("/usr/share/locale/fr_FR.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale/fr_FR/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale/fr.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale/fr/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale-langpack/fr_FR.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale-langpack/fr_FR/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale-langpack/fr.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 14847] open("/usr/share/locale-langpack/fr/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
....
[pid 14847] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14847, si_status=0, si_utime=0, si_stime=2} ---
+++ exited with 0 +++

您可以看到该过程尝试查找/usr/share/locale/usr/share/locale-langpack获取net-tools包(所属包netstat)的翻译文本。

现在,安装语言包就可以了。在Ubuntu系统上:

sudo apt-get install language-pack-fr

其他系统,软件包的相关语言包可以由软件包本身包含,至少在 Debian 和 Centos 上:

$ apt-file search /usr/share/locale/fr/LC_MESSAGES/net-tools.mo
net-tools: /usr/share/locale/fr/LC_MESSAGES/net-tools.mo

(在 Centos 上你可以使用yum whatprovides

然后:

$ strace -fe trace=open sh -c 'LANGUAGE=fr_FR.utf8 netstat -tnlp'
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
Process 15155 attached
[pid 15155] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[pid 15155] open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[pid 15155] open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
[pid 15155] open("/proc/3690/cmdline", O_RDONLY) = 5
[pid 15155] open("/proc/8579/cmdline", O_RDONLY) = 5
[pid 15155] open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3
[pid 15155] open("/usr/share/locale/fr_FR.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale/fr_FR/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale/fr.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale/fr/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale-langpack/fr_FR.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale-langpack/fr_FR/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale-langpack/fr.utf8/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 15155] open("/usr/share/locale-langpack/fr/LC_MESSAGES/net-tools.mo", O_RDONLY) = 3
[pid 15155] open("/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3
(Tous les processus ne peuvent être identifiés, les infos sur les processus
non possédés ne seront pas affichées, vous devez être root pour les voir toutes.)
Connexions Internet actives (seulement serveurs)
Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat       PID/Program name
....
[pid 15155] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=15155, si_status=0, si_utime=0, si_stime=3} ---
+++ exited with 0 +++

/usr/share/locale-langpack/fr/LC_MESSAGES/net-tools.mo存在,gettext()成功翻译消息。

请注意,并非所有单词都可以翻译,vi_VN即使我安装了language-pack-vi.


您应该使用LC_ALL覆盖所有其他本地化设置,GNU 系统中的 LANGUAGE 除外如上。

相关内容