PACKAGE, LOCALEDIR是啥意思

本文介绍了Linux下程序国际化(i18n)的基本概念和技术实现,包括如何使用setlocale设置程序的locale,bindtextdomain和textdomain指定消息域及路径,以及如何通过gettext函数实现多语言界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资料出处:http://www.cnblogs.com/super119/archive/2011/03/26/1996149.html

从来看linux下程序的internationalization

在看lftp的源代码的时候,看到了程序的一开头有这样几句代码: 

setlocale (LC_ALL, ""); 
setlocale (LC_NUMERIC, "C"); 
bindtextdomain (PACKAGE, LOCALEDIR); 
textdomain (PACKAGE); 

注:PACKAGE, LOCALEDIR都是宏,在autoconf的时候自动生成,PACKAGE宏就是lftp, LOCALEDIR就是/usr/share/locale 

不明白有什么用,于是查了一下资料,写了一个测试程序,搞明白了。 

setlocale是用来设置程序的locale的。程序可以设置任何自己喜欢的locale,但是一般linux下的程序都写成 setlocale(LC_ALL, "");这样的形式,这表示:程序的locale从环境变量中继承。setlocale这个函数本来是用来设置程序的locale的,第一个参数 LC_ALL表示要修改所有的locale变量(LANG, LC_CTYPE...),第二个参数就是要设置成什么locale。这里第二个参数是"",就表示locale信息从环境变量中继 承,setlocale以此检查LC_ALL环境变量是否设置了,如果没有依次往下检查,直到最后检查LANG环境变量。所以,简言之,这个函数的调用表 示程序继承环境变量中的locale设置。这样就可以保证,英文的linux系统将来看到的界面是英文的,中文的linux系统运行这个程序就能看到中文 的界面(当然,需要后面继续做工作)。 

bindtextdomain用来设置一个给定的msgdomain,在哪个目录下查找msg domain file。这个可以通过gettext编程来理解。linux下的i18n其实和java的差不多,就是代码中写的字符串,可以用xgettext这个工 具摘出来,然后形成pot文件(portable object template),然后用命令msginit加工成po文件,最后用msgfmt将po变成mo(二进制的文件),mo就保存所有的文本资源信息,就像 java里面的properties文件一样。我们需要做的就是在po文件中,将对应英文的字符串翻译成中文的,最后程序运行,根据locale的设置, 去显式不同的字符串出来,这样就实现i18n了,很简单,right? 

textdomain是用来设定当前的msgdomain的,msgdomain就可以理解成本程序使用的资源文件(mo文件)的名字,只不过需 要.mo扩展名而已。比如我们的程序资源文件是hello.mo,放在了/usr/share/locale/zh_CN/LC_MESSAGES目录 下,那么: 

#define PACKAGE "hello" 
#define LOCALEDIR "/usr/share/locale" 

bindtextdomain(PACKAGE, LOCALEDIR); 
textdomain(PACKAGE); 

那么,bindtextdomain首先就是表示遇到要查找hello这个msgdomain,要到/usr/share/locale下去找; 而textdomain就显式的设定当前的msgdomain是hello,程序启动的时候,msgdomain是一个名为message的字符串,而 且,多个程序互相调用的时候(一个程序调用另外一个程序),他们的msgdomain都不一样,所以需要用textdomain来显式的指出本程序使用的 msgdomain。 

这样就清楚了吧?做到了上面的内容,也有了mo文件了,那么,将来代码中只需要把字符串前面都套上一个gettext这个函数就可以实现i18n了,比如: 

cout << "Hello, world" << endl; 

改成: 

cout << gettext("Hello, world") << endl; 

这样,我们只需要准备不同locale的mo文件,放在恰当的/usr/share/locale的子目录下,然后设置好locale的环境变量,就可以让hello world变成不同语言的hello world了! 

详细的gettext的做法有一篇文章,放在附件中(名为GuideArticle.txt)。此外,我写的gettext的一个测试程序也在附件中,可以参阅。
C:\Users\陈柯嘉>pip install pandas Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pandas Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/d6/9f8431bacc2e19dca897724cd097b1bb224a6ad5433784a44b587c7c13af/pandas-2.2.3.tar.gz (4.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 20.4 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 2 ╰─> [32 lines of output] + meson setup C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4 C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4\.mesonpy-bg46k2qm\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4\.mesonpy-bg46k2qm\build\meson-python-native-file.ini Traceback (most recent call last): File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\mesonmain.py", line 194, in run return options.run_func(options) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\msetup.py", line 358, in run app.generate() File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\msetup.py", line 178, in generate env = environment.Environment(self.source_dir, self.build_dir, self.options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\environment.py", line 552, in __init__ config = coredata.parse_machine_files(self.coredata.config_files) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 1030, in parse_machine_files parser = MachineFileParser(filenames) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 972, in __init__ self.parser.read(filenames) File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 959, in read return super().read(filenames, encoding) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Programs\Python\Python312-32\Lib\configparser.py", line 684, in read self._read(fp, filename) File "C:\Users\陈柯嘉\AppData\Local\Programs\Python\Python312-32\Lib\configparser.py", line 999, in _read for lineno, line in enumerate(fp, start=1): ^^^^^^^^^^^^^^^^^^^^^^ File "<frozen codecs>", line 322, in decode UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 33: invalid start byte ERROR: Unhandled python exception This is a Meson bug and should be reported! [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. 怎么解决
最新发布
05-22
在 Windows 系统上使用 `pip` 安装 Pandas 时,可能会遇到诸如 `metadata-generation-failed` 或者 `UnicodeDecodeError` 的错误。这些问题通常与构建环境、Python 版本以及依赖项的兼容性有关。以下是详细的分析和解决方案: ### 错误原因 1. **Metadata Generation Failed**: 此错误表明在安装过程中,Pip 尝试生成元数据时失败了。这通常是由于缺少必要的编译工具链或依赖项引起的[^3]。 2. **UnicodeDecodeError**: 在 Python 3.12 中,这种错误可能源于 Meson 构建系统的不兼容性问题。Meson 是一些现代 Python 包(包括 Pandas)用于构建扩展模块的工具之一。当其处理文件编码时出现问题,就会引发此类异常。 --- ### 解决方案 #### 方法一:降级 Python 版本 目前,部分第三方库尚未完全适配 Python 3.12,因此建议暂时使用更稳定的版本(如 Python 3.10 或 3.11)。可以通过以下方式重新安装较低版本的 Python: ```bash winget install -e --id=Python.Python.3.11 ``` 或者访问 [Python官网](https://www.python.org/downloads/) 下载对应版本并完成安装[^4]。 #### 方法二:升级 Pip 和 Setuptools 确保使用的 Pip 和 Setuptools 已经是最新版本,以便更好地支持复杂的构建过程: ```bash python -m pip install --upgrade pip setuptools wheel ``` #### 方法三:更换国内镜像源 有时国外服务器连接不稳定也会导致类似的错误发生。可以尝试切换成清华或其他稳定镜像站点来获取资源: ```bash pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple ``` #### 方法四:预编译轮子 (Pre-built Wheels) 如果本地环境中缺乏 C++ 编译器而导致无法正常构建,则可以直接利用已经准备好的 whl 文件代替源码安装。前往 [Unofficial Windows Binaries for Python Extension Packages](https://www.lfd.uci.edu/~gohlke/pythonlibs/#pandas) 找到适合您操作系统位数及 Python 版本对应的 `.whl` 文件后下载下来并通过如下命令加载进去: ```bash pip install path_to_downloaded_file.whl ``` #### 方法五:修正 UnicodeEncode/Decode Errors 对于由高版本 Python 引发的具体字符集解析冲突情况,可以在调用脚本之前设置环境变量强制指定 UTF-8 编码模式试试看效果如何改善: ```cmd set PYTHONUTF8=1 python -m pip install pandas ``` 另外一种思路是从源头规避潜在风险因素——即明确告知 meson 使用 utf8 格式的输入输出流操作符: ```ini [build-options] default-library='shared' b_ndebug=true c_args=['-fPIC', '-std=c99'] cpp_args=['-fPIC'] backend=ninja wrap_mode=nofallback option('buildtype','release') option('werror',false) option('warning_level','2') option('strip',true) option('unity',false) option('default_library','shared') option('layout','flat') option('prefix','%s'%sysconfig.get_config_var('base')) option('libdir','${prefix}/Lib/site-packages/%s.libs'%(name)) option('bindir','${prefix}') option('includedir','${prefix}/include') option('mandir','${prefix}/share/man') option('datadir','${prefix}/share') option('localedir','${prefix}/share/locale') option('sysconfdir','${prefix}') option('localstatedir','${prefix}') ``` 以上配置片段可被写入项目根目录下的 `meson.build` 文件之中以覆盖默认行为设定从而避免不必要的麻烦[^5]。 --- ### 注意事项 尽管上述措施能有效缓解大部分场景中的难题,但考虑到不同用户的实际开发条件差异较大,仍可能存在个别例外情形需要额外调试排查才能彻底解决根本症结所在。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值