gettext 多语言翻译

// myapp.c

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

#define _(str)  gettext(str)
#define TEXT_DOMAIN "myapp"

int main() {
    setlocale(LC_ALL, "");
    bindtextdomain(TEXT_DOMAIN, "locale");

    return 0;


$ gcc myapp.c -o myapp


$ xgettext -a myapp.c -o myapp.pot


$ msginit -l zh_CN -i myapp.pot -o zh_CN.po


"Content-Type: text/plain; charset=UTF-8\n"
msgid "hello"
msgstr "你好"

生成mo(Machine Object)文件

$ msgfmt zh_CN.po -o


mkdir -p locale/zh_CN/LC_MESSAGES
cp locale/zh_CN/LC_MESSAGES/


$ export LANGUAGE=zh_CN
$ ./myapp
$ 你好
ithewei CSDN认证博客专家 c/c++ Qt libhv
<div class="post-text" itemprop="text"> <p>I am currently developing a website in PHP and I decided to go with <a href="" rel="nofollow">gettext</a> to manage the translations. I set up a nice <a href="" rel="nofollow">Pootle</a> server so that I can easily manage the translations and a bash script that runs with cron that extracts all of the values from the PHP files to be translated and creates the <code>.pot</code> translation template file.</p> <p>So far, so good. However, I just remembered that part of the text of the site is stored in a database. Let's call it "products" for simplicity. I want the product description, name, and a few other fields to be translatable, but it would be great if I could have a centralized way to translate them without having to create a separate interface just to translate the database entries. Since Pootle is already set up, it would be nice to be able to use that.</p> <p>I thought of two solutions:</p> <ol> <li>Forget using a database and use only PHP arrays</li> <li>Write a script that will extract all of the values from the database and generate a file that will then be scanned by the aforementioned bash script and add the values to the <code>pot</code> file, and another script that will run just after the bash script to re-update all of the values in the DB.</li> </ol> <p>Neither of these solutions really seem to be ideal. The first one would be easy to set up and easy to use with Pootle, but I lose all flexibility that comes with using a DBMS and I would have to import the entire array every time I want to use it. Loss of functionality isn't really that bad, because I (currently) am not performing any advanced calculations on the rows, basically just <code>SELECT</code>s and that's it. The second one, could work, but would take significantly more planning (and coding) to set up correctly.</p> <p>Are there any other ways that I'm missing that would give me the flexibility of a database, but allow me to easily translate it in a centralized place along with the rest of the site, like Pootle?</p> </div>
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页