SlideShare a Scribd company logo
1 of 27
Download to read offline
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 VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2014-06-04 12:36+0800n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>n"
"Language-Team: LANGUAGE <LL@li.org>n"
"Language: n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=CHARSETn"
"Content-Transfer-Encoding: 8bitn"
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 %sn"
msgstr ""
從pot檔產生中文po檔案
● msginit --locale=zh_TW --input=test_gettext.
pot --no-translator
未翻譯前po系統和手動更新部份
“POT-Creation-Date: 2014-05-30 21:18+0800n"
"PO-Revision-Date: 2014-05-30 21:18+0800n"
"Language-Team: Chinese (traditional) <zh-
l10n@linux.org.tw>n"
"Language: zh_TWn"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
更新中文po檔案
● 隨著程式持續開發會新增新的訊息,此時舊的
翻譯還在,所以需要比對新的pot檔並且合併
到原本翻譯的po檔內。
● msgmerge zh_TW.po test_gettext.pot
翻譯po檔
msgid "Taipei"
msgstr "台北"
msgid "bus"
msgstr "公車"
msgid "I will go to %s by %sn"
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 Related Content

Viewers also liked

Hello world在那邊?背景說明
Hello world在那邊?背景說明Hello world在那邊?背景說明
Hello world在那邊?背景說明Wen Liao
 
GNU AS簡介
GNU AS簡介GNU AS簡介
GNU AS簡介Wen Liao
 
UPnP 1.0 簡介
UPnP 1.0 簡介UPnP 1.0 簡介
UPnP 1.0 簡介Wen Liao
 
面向模式的软件体系架构
面向模式的软件体系架构面向模式的软件体系架构
面向模式的软件体系架构Weijun Zhong
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介Wen Liao
 
JCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternJCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternSteven Wang
 
Scenario movie script example
Scenario movie script example Scenario movie script example
Scenario movie script example NTUST
 
軟體組裝心得分享
軟體組裝心得分享軟體組裝心得分享
軟體組裝心得分享Wen Liao
 
以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界Amazon Web Services
 
Business model canvas 2016 yef boot camp
Business model canvas 2016 yef boot campBusiness model canvas 2016 yef boot camp
Business model canvas 2016 yef boot campPei-Ying Wang
 
Let's talk about Web Design
Let's talk about Web DesignLet's talk about Web Design
Let's talk about Web DesignAbby Chiu
 
以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端Amazon Web Services
 
無伺服器架構和Containers on AWS入門
無伺服器架構和Containers on AWS入門 無伺服器架構和Containers on AWS入門
無伺服器架構和Containers on AWS入門 Amazon Web Services
 
IBM SoftLayer intro
IBM SoftLayer introIBM SoftLayer intro
IBM SoftLayer introQmo Lin
 
Parallel program design
Parallel program designParallel program design
Parallel program designZongYing Lyu
 

Viewers also liked (15)

Hello world在那邊?背景說明
Hello world在那邊?背景說明Hello world在那邊?背景說明
Hello world在那邊?背景說明
 
GNU AS簡介
GNU AS簡介GNU AS簡介
GNU AS簡介
 
UPnP 1.0 簡介
UPnP 1.0 簡介UPnP 1.0 簡介
UPnP 1.0 簡介
 
面向模式的软件体系架构
面向模式的软件体系架构面向模式的软件体系架构
面向模式的软件体系架构
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介
 
JCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternJCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design Pattern
 
Scenario movie script example
Scenario movie script example Scenario movie script example
Scenario movie script example
 
軟體組裝心得分享
軟體組裝心得分享軟體組裝心得分享
軟體組裝心得分享
 
以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界以Device Shadows與Rules Engine串聯實體世界
以Device Shadows與Rules Engine串聯實體世界
 
Business model canvas 2016 yef boot camp
Business model canvas 2016 yef boot campBusiness model canvas 2016 yef boot camp
Business model canvas 2016 yef boot camp
 
Let's talk about Web Design
Let's talk about Web DesignLet's talk about Web Design
Let's talk about Web Design
 
以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端
 
無伺服器架構和Containers on AWS入門
無伺服器架構和Containers on AWS入門 無伺服器架構和Containers on AWS入門
無伺服器架構和Containers on AWS入門
 
IBM SoftLayer intro
IBM SoftLayer introIBM SoftLayer intro
IBM SoftLayer intro
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 

Similar to GNU gettext簡介 - 以C語言為範例

版本控制 使用Git & git hub
版本控制   使用Git & git hub版本控制   使用Git & git hub
版本控制 使用Git & git hub維佋 唐
 
Sitcon 2016 我的自由軟體之路
Sitcon 2016 我的自由軟體之路Sitcon 2016 我的自由軟體之路
Sitcon 2016 我的自由軟體之路政嘉 曾
 
2010 08-14 web-sitei18n
2010 08-14 web-sitei18n2010 08-14 web-sitei18n
2010 08-14 web-sitei18ntsunghaolee
 
FreeBSD documentation translation
FreeBSD documentation translationFreeBSD documentation translation
FreeBSD documentation translationRaycherng Yu
 
Customize snipmate
Customize snipmateCustomize snipmate
Customize snipmateRhythm Sun
 
[精华回顾]讲座:Linux及常用软件简介
[精华回顾]讲座:Linux及常用软件简介[精华回顾]讲座:Linux及常用软件简介
[精华回顾]讲座:Linux及常用软件简介NJU OPEN
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshellNelson Tai
 
Debian & Packaging
Debian & PackagingDebian & Packaging
Debian & PackagingLI Daobing
 
2016-04-07-清大-國際化開源專案技術實務與經驗分享
2016-04-07-清大-國際化開源專案技術實務與經驗分享2016-04-07-清大-國際化開源專案技術實務與經驗分享
2016-04-07-清大-國際化開源專案技術實務與經驗分享Jen Yee Hong
 
[精彩回顾]Linux新手教程
[精彩回顾]Linux新手教程[精彩回顾]Linux新手教程
[精彩回顾]Linux新手教程NJU OPEN
 
GNOME3 延伸套件教學
GNOME3 延伸套件教學GNOME3 延伸套件教學
GNOME3 延伸套件教學Yuren Ju
 
2010 07-29-version control use git
2010 07-29-version control use git2010 07-29-version control use git
2010 07-29-version control use gitKang-Min Wang
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战icy leaf
 
Firefox OS Overview
Firefox OS OverviewFirefox OS Overview
Firefox OS OverviewYan-ren Tsai
 
How to Install Debian GNU/Linux
How to Install Debian GNU/LinuxHow to Install Debian GNU/Linux
How to Install Debian GNU/LinuxShau-Hung Hsieh
 

Similar to GNU gettext簡介 - 以C語言為範例 (20)

版本控制 使用Git & git hub
版本控制   使用Git & git hub版本控制   使用Git & git hub
版本控制 使用Git & git hub
 
Sitcon 2016 我的自由軟體之路
Sitcon 2016 我的自由軟體之路Sitcon 2016 我的自由軟體之路
Sitcon 2016 我的自由軟體之路
 
2010 08-14 web-sitei18n
2010 08-14 web-sitei18n2010 08-14 web-sitei18n
2010 08-14 web-sitei18n
 
FreeBSD documentation translation
FreeBSD documentation translationFreeBSD documentation translation
FreeBSD documentation translation
 
Go
GoGo
Go
 
Customize snipmate
Customize snipmateCustomize snipmate
Customize snipmate
 
Github簡介
Github簡介Github簡介
Github簡介
 
[精华回顾]讲座:Linux及常用软件简介
[精华回顾]讲座:Linux及常用软件简介[精华回顾]讲座:Linux及常用软件简介
[精华回顾]讲座:Linux及常用软件简介
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 
Debian & Packaging
Debian & PackagingDebian & Packaging
Debian & Packaging
 
2016-04-07-清大-國際化開源專案技術實務與經驗分享
2016-04-07-清大-國際化開源專案技術實務與經驗分享2016-04-07-清大-國際化開源專案技術實務與經驗分享
2016-04-07-清大-國際化開源專案技術實務與經驗分享
 
[精彩回顾]Linux新手教程
[精彩回顾]Linux新手教程[精彩回顾]Linux新手教程
[精彩回顾]Linux新手教程
 
Using vim
Using vimUsing vim
Using vim
 
GNOME3 延伸套件教學
GNOME3 延伸套件教學GNOME3 延伸套件教學
GNOME3 延伸套件教學
 
2010 07-29-version control use git
2010 07-29-version control use git2010 07-29-version control use git
2010 07-29-version control use git
 
Git簡介
Git簡介Git簡介
Git簡介
 
Git 入门实战
Git 入门实战Git 入门实战
Git 入门实战
 
Build Your Own Android Toolchain from scratch
Build Your Own Android Toolchain from scratchBuild Your Own Android Toolchain from scratch
Build Your Own Android Toolchain from scratch
 
Firefox OS Overview
Firefox OS OverviewFirefox OS Overview
Firefox OS Overview
 
How to Install Debian GNU/Linux
How to Install Debian GNU/LinuxHow to Install Debian GNU/Linux
How to Install Debian GNU/Linux
 

More from Wen Liao

Hello world 的一生
Hello world 的一生Hello world 的一生
Hello world 的一生Wen Liao
 
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
Notes on oracle solaris 11.3 linkers  and libraries guide  chapter oneNotes on oracle solaris 11.3 linkers  and libraries guide  chapter one
Notes on oracle solaris 11.3 linkers and libraries guide chapter oneWen Liao
 
A successful git branching model 導讀
A successful git branching model 導讀A successful git branching model 導讀
A successful git branching model 導讀Wen Liao
 
Trace 程式碼之皮
Trace 程式碼之皮Trace 程式碼之皮
Trace 程式碼之皮Wen Liao
 
淺談Debian套件打包
淺談Debian套件打包淺談Debian套件打包
淺談Debian套件打包Wen Liao
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command lineWen Liao
 

More from Wen Liao (6)

Hello world 的一生
Hello world 的一生Hello world 的一生
Hello world 的一生
 
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
Notes on oracle solaris 11.3 linkers  and libraries guide  chapter oneNotes on oracle solaris 11.3 linkers  and libraries guide  chapter one
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
 
A successful git branching model 導讀
A successful git branching model 導讀A successful git branching model 導讀
A successful git branching model 導讀
 
Trace 程式碼之皮
Trace 程式碼之皮Trace 程式碼之皮
Trace 程式碼之皮
 
淺談Debian套件打包
淺談Debian套件打包淺談Debian套件打包
淺談Debian套件打包
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 

GNU gettext簡介 - 以C語言為範例