SlideShare una empresa de Scribd logo
1 de 31
Descargar para leer sin conexión
Django, i18N, l10N
今天
• 什麼是 i18N 跟 l10N
• Python 怎麼做
• Django 怎麼做
i18n, 國際化
• internationalization
• 太⻑⾧長了, 就改成 iXXn, XX 就是⻑⾧長度
l10N 本地化
• localization ⽐比照辦理
詳細的意義
• (From Wiki)
• 國際化(i18n) 意味著產品有適⽤用於任何地⽅方的「潛
⼒力」
• 在地化(l10N)則是為了更適合於「特定」地⽅方的使
⽤用
簡化版 i18N + l10N
simple.py
python simple.py
Hi 世界
LANG=en_US.UTF-8 python simple.py
Hello World
簡化版 i18N + l10N
實務上需要的功能
• 單數複數?
• May 是五⽉月天還是五⽉月花? Order 是順序還是訂
單?
• ⼯工作流程如何標準化?
• 數字,⽇日期的格式
更難的問題
• ⽂文化,圖⽚片,習慣
• ⽂文字⽅方向
Gnu gettext
• gnu 的 gettext 可以幫助我們做 i18N, l10N。並且
可以⽀支援 C, Java, Python, PHP… 等等語⾔言
• 使⽤用 gettext 可以⾃自已建⽴立 i18N, l10 的流程
• django 基於 gettext,有⼀一套很簡潔的流程
Gettext 包含
• xgettext 讀取你的程式,產⽣生訊息檔
• msgfmt 把訊息檔變成 2 進位格式,供你的程式
讀取
訊息檔
使⽤用 gettext 的流程
• 寫好程式,程式內使⽤用字串的地⽅方,請⽤用 _() 圍住
• 執⾏行 xgettext 新增/更新訊息檔
• 使⽤用 msgfmt 編譯訊息檔
• 執⾏行程式看看
xgettext and python
example.py
訊息以語系做分別
.
├── example.py
└── zh_TW.UTF-8
└── LC_MESSAGES
└── example.mo
指令
• 產⽣生訊息 xgettext -p zh_TW.UTF-8/LC_MESSAGES/ -d
example example.py
• 加⼊入新字串
• 更新訊息 xgettext -j -p zh_TW.UTF-8/LC_MESSAGES/
-d example example.py
• 編譯訊息 msgfmt zh_TW.UTF-8/LC_MESSAGES/
example.po -o zh_TW.UTF-8/LC_MESSAGES/
example.mo
DEMO
• 程式碼是 pure_python/example.py
xgettext 的貼⼼心
• 若是有漏翻的,會先以 key 擋著
• 可以簡易做出訊息檔
Used In Django
• 剛剛有點苦, Django 會幫你簡化使⽤用⽅方式
Django
Django
locale
settings
LocaleMiddleWare
存放訊息檔
LOCALE_PATHS
LANGUAGE_CODE
Request
get user
language
preference
基本 setting
LOCALE_PATHS:
每個 app 可以有⾃自已的訊息檔,我們也可以⾃自已增加額外
的訊息檔的位置。 設定⽅方式跟 TEMPLATE_DIR 很像
LANGUAGE_CODE:
如果你的網站的使⽤用者沒有設定語⾔言,就以這個做預設值。
在 1.8 之後 中⽂文是 zh-Hant ,規則是 語⾔言-字體
1.7 以前,是⽤用 zh-TW (可以不⽤用管⼤大⼩小寫 )
這個設定 dash 是擺中間
Middleware
 session middle ware 要擺第⼀一,因為才能知道 user 的選擇
locale middle ware 會分析 accept langue request header
Template 實做
記得 load i18n
trans 後⾯面加 key
Used With View
import ugettext as _
習慣使⽤用 _ , ⽤用 ugettext 也是可以的
Used With Model
再把 admin.py 補起來。
你會發現 Order 沒有如你所願翻成功
要改⽤用 ugettext_lazy
Why
• Python 的運作模式,是⼀一開始⼀一定會執⾏行程式⼀一
次。
• verbose_name 在⼀一開始啟動,就被決定了...
• lazy 結尾的版本,會每⼀一次存取都取值
DEMO
• demo flow
• demo with Browser language order
• Make Message:
• django-admin.py makemessages -l zh_TW
• Compile Message
• django-admin.py compilemessages
Used With javaScript
• ⼤大家已經有概念,所有的字串都要即時取值。
• django 可以幫你產⽣生合適的 js code. 讓你使⽤用⽅方
式跟剛剛講的幾呼⼀一樣。
• django-admin makemessages -d djangojs -l
zh_TW
• djangojs 很重要,是指定 domain. 除了產⽣生的訊
息檔案不同之外,他不會去掃描 python 檔了,⽽而
是掃 javaScript 檔
DEMO
• Demo of JS
Further Reading
• 單複數的處理
• django-statici18n

Más contenido relacionado

Destacado

Pharma customer experience: A conversation
Pharma customer experience: A conversationPharma customer experience: A conversation
Pharma customer experience: A conversationCOUCH Health
 
How to Use HTP to Complete the Close Process Puzzle
How to Use HTP to Complete the Close Process PuzzleHow to Use HTP to Complete the Close Process Puzzle
How to Use HTP to Complete the Close Process PuzzleAlithya
 
Yellowfin 7.3 launch presentation slides
Yellowfin 7.3 launch presentation slidesYellowfin 7.3 launch presentation slides
Yellowfin 7.3 launch presentation slidesYellowfin
 
Enterprise Performance Management - HFM
Enterprise Performance Management - HFMEnterprise Performance Management - HFM
Enterprise Performance Management - HFMCodec-dss UK
 
Thermodynamics - Experiment 2
Thermodynamics - Experiment 2Thermodynamics - Experiment 2
Thermodynamics - Experiment 2syar2604
 
Capgemini Kpn Presentation Obug 2009
Capgemini Kpn Presentation Obug 2009Capgemini Kpn Presentation Obug 2009
Capgemini Kpn Presentation Obug 2009mdekam
 
Presentation & communication skills
Presentation & communication skills Presentation & communication skills
Presentation & communication skills gihan aboueleish
 
Service Chaining Current and Future
Service Chaining Current and FutureService Chaining Current and Future
Service Chaining Current and FutureKentaro Ebisawa
 

Destacado (10)

Pharma customer experience: A conversation
Pharma customer experience: A conversationPharma customer experience: A conversation
Pharma customer experience: A conversation
 
How to Use HTP to Complete the Close Process Puzzle
How to Use HTP to Complete the Close Process PuzzleHow to Use HTP to Complete the Close Process Puzzle
How to Use HTP to Complete the Close Process Puzzle
 
Yellowfin 7.3 launch presentation slides
Yellowfin 7.3 launch presentation slidesYellowfin 7.3 launch presentation slides
Yellowfin 7.3 launch presentation slides
 
Enterprise Performance Management - HFM
Enterprise Performance Management - HFMEnterprise Performance Management - HFM
Enterprise Performance Management - HFM
 
Thermodynamics - Experiment 2
Thermodynamics - Experiment 2Thermodynamics - Experiment 2
Thermodynamics - Experiment 2
 
Capgemini Kpn Presentation Obug 2009
Capgemini Kpn Presentation Obug 2009Capgemini Kpn Presentation Obug 2009
Capgemini Kpn Presentation Obug 2009
 
2 Team Dynamics
2 Team Dynamics2 Team Dynamics
2 Team Dynamics
 
Presentation & communication skills
Presentation & communication skills Presentation & communication skills
Presentation & communication skills
 
SIKOLOHIYANG PILIPINO
SIKOLOHIYANG PILIPINOSIKOLOHIYANG PILIPINO
SIKOLOHIYANG PILIPINO
 
Service Chaining Current and Future
Service Chaining Current and FutureService Chaining Current and Future
Service Chaining Current and Future
 

Más de Tim (文昌)

Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影Tim (文昌)
 
Frontend django, Django Web 前端探索
Frontend django, Django Web 前端探索Frontend django, Django Web 前端探索
Frontend django, Django Web 前端探索Tim (文昌)
 
Introduction to protocol buffer
Introduction to protocol bufferIntroduction to protocol buffer
Introduction to protocol bufferTim (文昌)
 
Tainan.py, Experience about package
Tainan.py, Experience about packageTainan.py, Experience about package
Tainan.py, Experience about packageTim (文昌)
 
Performance Enhancement Tips
Performance Enhancement TipsPerformance Enhancement Tips
Performance Enhancement TipsTim (文昌)
 
pygame sharing pyhug
pygame sharing pyhugpygame sharing pyhug
pygame sharing pyhugTim (文昌)
 

Más de Tim (文昌) (9)

Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影
 
Frontend django, Django Web 前端探索
Frontend django, Django Web 前端探索Frontend django, Django Web 前端探索
Frontend django, Django Web 前端探索
 
Profile django
Profile djangoProfile django
Profile django
 
Introduction to protocol buffer
Introduction to protocol bufferIntroduction to protocol buffer
Introduction to protocol buffer
 
Mock Introduction
Mock IntroductionMock Introduction
Mock Introduction
 
Ml weka
Ml wekaMl weka
Ml weka
 
Tainan.py, Experience about package
Tainan.py, Experience about packageTainan.py, Experience about package
Tainan.py, Experience about package
 
Performance Enhancement Tips
Performance Enhancement TipsPerformance Enhancement Tips
Performance Enhancement Tips
 
pygame sharing pyhug
pygame sharing pyhugpygame sharing pyhug
pygame sharing pyhug
 

I18n