Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GNU gettext簡介 - 以C語言為範例
Search
Wen_Liao
June 04, 2014
Technology
810
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GNU gettext簡介 - 以C語言為範例
Wen_Liao
June 04, 2014
Transcript
27 slides
GNU gettext簡介 以C語言為範例 Wen Liao
目標 簡介GNU gettext 以及其用法,以C語言為範例
Outline • 關於GNU gettext • 使用gettext處理多國語言訊息 • 參考資料 • Q
& A
關於gettext • 1990年代Sun推出的軟體,用於處理Unix下面 程式訊息的多國語言問題。 • 後來GNU協會也推出了GNU gettext
Outline • 關於GNU gettext • 使用gettext處理多國語言訊息 • 參考資料 • Q
& A
流程 • 修改程式碼 ◦ 告訴系統使用環境變數的語言相關設定 ◦ 告訴系統要吃那個目錄下面的哪個語文訊息相關檔案 ▪ 目錄:/aa/bb/cc/LC_MESSAGES ▪
檔案:my_prog.mo ◦ 使用gettext API描述程式訊息 • 使用工具產生portable object tempate檔案 (pot)
流程 • 透過pot檔案,使用工具產生需要語言版本訊 息portable object (po)檔如zh_TW.po, fr.po, ja.po • 開始翻譯
• 把翻譯好的po轉成平台可辨識的machine object (mo)檔案
http://en.wikipedia.org/wiki/File:Gettext.svg
範例 (印出訊息,支援中文和英文) #include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h>
#include <libintl.h> #define MAX_CHAR (32) int main(int argc, char **argv) { char dest[MAX_CHAR]; char transport[MAX_CHAR]; gettext和設定語系 用到的header file
範例 (印出訊息,支援中文和英文) setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); strncpy(dest, gettext("Taipei"), MAX_CHAR);
strncpy(transport, gettext("bus"), MAX_CHAR); printf(gettext("I will go to %s by %s.\n"), dest, transport); return 0; } 使用環境變數設定的 語言環境 指定要吃那個目錄 的哪個語言檔案c 載入語言檔案 gettext API
編譯 $ gcc -Wall -Werror -g -DPACKAGE=\" test_gettext\" -DLOCALEDIR=\" /home/user/gettext/po\"
test_gettext.c -o test_gettext 使用者自行設定語言檔 目錄和檔案。正式使用 可放在/usr/share/locale 下面對應的語系目錄。
po 目錄 tree view $ tree po/ po/ └── zh_TW
└── LC_MESSAGES └── test_gettext.mo
產生pot檔 xgettext -o test_gettext.pot --add-comments=- - k_ test_gettext.c
pot內容節錄 # SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE
PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
pot內容節錄 msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-06-04 12:36+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <
[email protected]
>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n"
pot內容節錄 #: test_gettext.c:40 msgid "Taipei" msgstr "" #: test_gettext.c:41 msgid
"bus" msgstr ""
pot內容節錄 #: test_gettext.c:44 #, c-format msgid "I will go to
%s by %s\n" msgstr ""
從pot檔產生中文po檔案 • msginit --locale=zh_TW --input=test_gettext. pot --no-translator
未翻譯前po系統和手動更新部份 “POT-Creation-Date: 2014-05-30 21:18+0800\n" "PO-Revision-Date: 2014-05-30 21:18+0800\n" "Language-Team: Chinese (traditional)
<zh-
[email protected]
>\n" "Language: zh_TW\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n"
更新中文po檔案 • 隨著程式持續開發會新增新的訊息,此時舊的 翻譯還在,所以需要比對新的pot檔並且合併 到原本翻譯的po檔內。 • msgmerge zh_TW.po test_gettext.pot
翻譯po檔 msgid "Taipei" msgstr "台北" msgid "bus" msgstr "公車" msgid
"I will go to %s by %s\n" msgstr "我搭乘%2$s到%1$s。\n" 名詞順序會隨語言不同 而改變,gettext可以在翻 譯時更改順序。
產生mo檔案 msgfmt zh_TW.po -o /home/user/gettext/po/zh_TW/LC_MESSAGES/ test_gettext.mo
驗收成果 $ LC_ALL=en_US.utf8 ./test_gettext I will go to Taipei by
bus $ LC_ALL=zh_TW.utf8 ./test_gettext 我搭乘公車到台北。 en_US.uft8等資料可以 由locale -a取得
同場加映, fuzzy #, fuzzy msgid "Taipei" msgstr "台北" • 產生mo後執行結果
$ LC_ALL=zh_TW.utf8 ./test_gettext 我搭乘公車到Taipei。 , fuzzy是gettext工具語法,表 示該翻譯尚未定案,所以執行 時不會使用該翻譯。
Outline • 關於GNU gettext • 使用gettext處理多國語言訊息 • 參考資料 • Q
& A
參考資料 • Wikipedia: gettext ◦ http://en.wikipedia.org/wiki/Gettext • gettext手冊 ◦ http://www.gnu.org/software/gettext/manual/gettext.
html • C語言中使用gettext ◦ http://wen00072-blog.logdown.com/posts/202230- study-on-gettext
Outline • 關於GNU gettext • 使用gettext處理多國語言訊息 • 參考資料 • Q
& A
More Decks by Wen_Liao
See All by Wen_Liao
COSCUP-2020-Linux 軟體組裝工和他的工具們
wen_liao
2
1.5k
開放街圖 自助旅行的好幫手
wen_liao
0
1.4k
GNU AS簡介
wen_liao
0
1.3k
UPnP 1.0 簡介
wen_liao
0
1.2k
自由軟體和 Richard Stallman
wen_liao
0
2.4k
Hello world在那邊?背景說明 0
wen_liao
0
850
A successful Git branching model 導讀
wen_liao
0
780
GNU ld的linker script簡介
wen_liao
0
1.5k
Trace 程式碼之皮
wen_liao
0
990
Other Decks in Technology
See All in Technology
みん強Lightning! データ基盤ひとりでできるかな
endonanana
0
220
OpenTelemetryで変わった 新時代のCloudWatchメトリクス監視
tsukuboshi
0
510
実測データからサービスの成長余力を測る。負荷シナリオ・テストデータ・限界値の設計
kazatohiei
1
190
現場に渡し、構造で守る AI エージェントを活用したセルフサーブのデータ基盤の運用
legalontechnologies
PRO
0
140
AWS Well-Arhitected Frameworkセキュリティの柱 について
miu_crescent
PRO
2
260
Platform Engineering をベースとした Agentic Software Factory の実践
110y
3
1k
IVRy における AI 時代のデータプロダクトと事業開発
ivry_presentationmaterials
0
130
認知の質も文化醸成も。数年取り組んで見えてきた技術広報のKPI設計 / Tech PR KPIs: Lessons on Awareness Quality and Culture After Several Years
bitkey
PRO
0
260
VTuberになってみた話
yoshimura_datam
0
110
OWASP AI Agentic Top10に準拠するクラウドネイティブなガードレールの実践(CNDW 2026)
keitah
1
460
成長するID管理基盤における運用もコストも犠牲にしないオブザーバビリティ向上の実践 / Improving Observability for a Growing Identity Platform Without Adding Operational Burden or Cost
kaminashi
1
140
AIが学べる子供の夏休みの自由研究教材を作ったが空振りだった話
yoshimura_datam
0
110
Featured
See All Featured
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.8k
Prompt Engineering for Job Search
mfonobong
0
610
Amusing Abliteration
ianozsvald
1
400
Mobile First: as difficult as doing things right
swwweet
225
10k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.9k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
500
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
1k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
480