### 本地化
不管有沒有登陸,當前用戶的 locale 設置可以通過兩種方式訪問到:請求處理器的 `self.locale` 對象、以及模板中的 `locale` 值。Locale 的名稱(如 `en_US`)可以 通過 `locale.name` 這個變量訪問到,你可以使用 `locale.translate` 來進行本地化 翻譯。在模板中,有一個全局方法叫 `_()`,它的作用就是進行本地化的翻譯。這個 翻譯方法有兩種使用形式:
```
_("Translate this string")
```
它會基于當前 locale 設置直接進行翻譯,還有一種是:
```
_("A person liked this", "%(num)d people liked this", len(people)) % {"num": len(people)}
```
這種形式會根據第三個參數來決定是使用單數或是復數的翻譯。上面的例子中,如果 `len(people)` 是 `1` 的話,就使用第一種形式的翻譯,否則,就使用第二種形式 的翻譯。
常用的翻譯形式是使用 Python 格式化字符串時的“固定占位符(placeholder)”語法,(例如上面的 `%(num)d`),和普通占位符比起來,固定占位符的優勢是使用時沒有順序限制。
一個本地化翻譯的模板例子:
```
<html>
<head>
<title>FriendFeed - {{ _("Sign in") }}</title>
</head>
<body>
<form action="{{ request.path }}" method="post">
<div>{{ _("Username") }} <input type="text" name="username"/></div>
<div>{{ _("Password") }} <input type="password" name="password"/></div>
<div><input type="submit" value="{{ _("Sign in") }}"/></div>
{{ xsrf_form_html() }}
</form>
</body>
</html>
```
默認情況下,我們通過 `Accept-Language` 這個頭來判定用戶的 locale,如果沒有, 則取 `en_US` 這個值。如果希望用戶手動設置一個 locale 偏好,可以在處理請求的 類中復寫 `get_user_locale` 方法:
```
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
user_id = self.get_secure_cookie("user")
if not user_id: return None
return self.backend.get_user_by_id(user_id)
def get_user_locale(self):
if "locale" not in self.current_user.prefs:
# Use the Accept-Language header
return None
return self.current_user.prefs["locale"]
```
如果 `get_user_locale` 返回 `None`,那么就會再去取 `Accept-Language` header 的值。
你可以使用 `tornado.locale.load_translations` 方法獲取應用中的所有已存在的翻 譯。它會找到包含有特定名字的 CSV 文件的目錄,如 `es_GT.csv` `fr_CA.csv` 這 些 csv 文件。然后從這些 CSV 文件中讀取出所有的與特定語言相關的翻譯內容。典型的用例 里面,我們會在 Tornado 服務器的 `main()` 方法中調用一次該函數:
```
def main():
tornado.locale.load_translations(
os.path.join(os.path.dirname(__file__), "translations"))
start_server()
```
你可以使用 `tornado.locale.get_supported_locales()` 方法得到支持的 locale 列表。Tornado 會依據用戶當前的 locale 設置以及已有的翻譯,為用戶選擇 一個最佳匹配的顯示語言。比如,用戶的 locale 是 `es_GT` 而翻譯中只支持了 `es`, 那么 `self.locale` 就會被設置為 `es`。如果找不到最接近的 locale 匹配,`self.locale` 就會就會取備用值 `es_US`。
查看 [`locale` 模塊](http://github.com/facebook/tornado/blob/master/tornado/locale.py) 的代碼文檔以了解 CSV 文件的格式,以及其它的本地化方法函數。