{"id":32985,"date":"2023-10-19T11:35:03","date_gmt":"2023-10-19T03:35:03","guid":{"rendered":"https:\/\/www.deepin.org\/?p=32985"},"modified":"2023-10-20T09:56:14","modified_gmt":"2023-10-20T01:56:14","slug":"gettext","status":"publish","type":"post","link":"https:\/\/www.deepin.org\/en\/gettext\/","title":{"rendered":"gettext Translation Introduction and Simple Usage"},"content":{"rendered":"<h1><img loading=\"lazy\" src=\"https:\/\/storage.deepin.org\/thread\/202310191341038635_en.png\" alt=\"\" width=\"900\" height=\"383\" \/><\/h1>\n<h1>Introduction to the Basics<\/h1>\n<h2>Introduction to poedit<\/h2>\n<p>poedit is a popular cross-platform translation editor for editing .po files. It provides an intuitive interface and a rich set of features that make it easy for translators to edit and manage software localized translations.<\/p>\n<h2>gettext Introduction<\/h2>\n<ul>\n<li>Gettext Toolset: Gettext provides a set of command-line tools for managing and processing translation-related tasks. This includes the following tools:<br \/>\nxgettext: used to extract text strings from source code and generate .pot files (template files).<br \/>\nmsginit: used to create new .po files and provide a basic translation environment.<br \/>\nmsgmerge: used to merge updated .pot files into existing .po files, preserving existing translations.<br \/>\nmsgfmt: used to compile .po files into binary .mo files for programs to load at runtime.<\/li>\n<\/ul>\n<h2>.pot file<\/h2>\n<p>.pot File (Portable Object Template): A .pot file is a template file used to create a .po file. It contains text in the source language, but no translation into the target language. Developers typically use a .pot file as a base to extract all the text strings in an application and convert them into a .pot file. Translators can then use the .pot file to create the corresponding .po file and work on the translation.<\/p>\n<h2>po files<\/h2>\n<p>A .po file is a text file that contains the source language (usually English) and the corresponding target language translation. Each translation entry includes a unique identifier, source text and target text. .po files are editable and can be modified using a text editor or specialized translation tool.<\/p>\n<h2>.mo files<\/h2>\n<p>A .mo file is a binary file that is a compiled version of a .po file. It contains a binary representation of the translated entries in the .po file. Software typically uses .mo files to load the correct translated text at runtime. The .mo file is machine-readable and therefore improves the efficiency and performance of the translation at runtime.<\/p>\n<h1>Environment Preparation<\/h1>\n<h2>Software Installation<\/h2>\n<p><em>sudo apt <span class=\"hljs-keyword\">install<\/span> gettext <\/em><\/p>\n<p><em>sudo apt <span class=\"hljs-keyword\">install<\/span> poedit<\/em><\/p>\n<h2>sample program<\/h2>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.deepin.org\/thread\/202310191129368250_\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_16976850375079.png\" alt=\"\" width=\"700\" height=\"406\" \/><\/p>\n<h2>compiling<\/h2>\n<p><em><span class=\"hljs-attribute\">gcc<\/span> myapp.c -o myapp<\/em><\/p>\n<h2>Generate pot file<\/h2>\n<p><em><span class=\"hljs-selector-tag\">xgettext<\/span> <span class=\"hljs-selector-tag\">-a<\/span> <span class=\"hljs-selector-tag\">myapp<\/span><span class=\"hljs-selector-class\">.c<\/span> <span class=\"hljs-selector-tag\">-o<\/span> <span class=\"hljs-selector-tag\">myapp<\/span><span class=\"hljs-selector-class\">.pot<\/span><\/em><\/p>\n<p>Note the deletion of redundant translation lines<\/p>\n<h2>Generate po file<\/h2>\n<p><em><span class=\"hljs-selector-tag\">msginit<\/span> <span class=\"hljs-selector-tag\">-l<\/span> <span class=\"hljs-selector-tag\">zh_CN<\/span> <span class=\"hljs-selector-tag\">-i<\/span> <span class=\"hljs-selector-tag\">myapp<\/span><span class=\"hljs-selector-class\">.pot<\/span> <span class=\"hljs-selector-tag\">-o<\/span> <span class=\"hljs-selector-tag\">zh_CN<\/span><span class=\"hljs-selector-class\">.po<\/span><\/em><\/p>\n<p>To translate po files, use poedit.<\/p>\n<h2>Generate mo file<\/h2>\n<p><em><span class=\"hljs-selector-tag\">msgfmt<\/span> <span class=\"hljs-selector-tag\">zh_CN<\/span><span class=\"hljs-selector-class\">.po<\/span> <span class=\"hljs-selector-tag\">-o<\/span> <span class=\"hljs-selector-tag\">zh_CN<\/span><span class=\"hljs-selector-class\">.mo<\/span><\/em><\/p>\n<h2>mounting<\/h2>\n<p><em><span class=\"hljs-attribute\">mkdir<\/span> -p locale\/zh_CN\/LC_MESSAGES <\/em><\/p>\n<p><em>cp zh_CN.mo locale\/zh_CN\/LC_MESSAGES\/myapp.mo<\/em><\/p>\n<h2>(of a computer) run<\/h2>\n<p><em>.\/myapp<\/em><\/p>\n<h1>Troubleshooting Translation Issues<\/h1>\n<ol>\n<li>Check if the path of the translated file is correct<br \/>\nTo check if the translation content is correct, convert mo to a po file and use poedit to open the pot file.<\/li>\n<\/ol>\n<p><em><span class=\"hljs-selector-tag\">msgunfmt<\/span> <span class=\"hljs-selector-tag\">example<\/span><span class=\"hljs-selector-class\">.mo<\/span> <span class=\"hljs-selector-tag\">-o<\/span> <span class=\"hljs-selector-tag\">example<\/span><span class=\"hljs-selector-class\">.po<\/span><\/em><\/p>\n<ol>\n<li>Check if the program language related environment variables are printed properly before the initialization function<\/li>\n<\/ol>","protected":false},"excerpt":{"rendered":"<p>Introduction to the Basics Introduction to poedit poedit is a popular cross-platform translation editor for editing .po files. It provides an intuitive interface and a rich set of features that make it easy for translators to edit and manage software localized translations. gettext Introduction Gettext Toolset: Gettext provides a set of command-line tools for managing and processing translation-related tasks. This includes the following tools: xgettext: used to extract text strings from source code and generate .pot files (template files). msginit: used to create new .po files and provide a basic translation environment. msgmerge: used to merge updated .pot files into ...<a href=https:\/\/www.deepin.org\/en\/gettext\/>Read more<\/a><\/p>\n","protected":false},"author":11164,"featured_media":32988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[155,93],"tags":[],"_links":{"self":[{"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/posts\/32985"}],"collection":[{"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/users\/11164"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/comments?post=32985"}],"version-history":[{"count":2,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/posts\/32985\/revisions"}],"predecessor-version":[{"id":32987,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/posts\/32985\/revisions\/32987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/media\/32988"}],"wp:attachment":[{"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/media?parent=32985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/categories?post=32985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deepin.org\/en\/wp-json\/wp\/v2\/tags?post=32985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}