OpenStack I18N

OpenStack I18N

官方文檔:
https://docs.openstack.org/oslo.i18n/latest/user/usage.html
https://docs.openstack.org/oslo.i18n/latest/user/guidelines.htmlhtml

使用前準備

準備 .mo 文件

1. 運行 Python 安裝目錄下的 Tools/i18n/pygettext.py,生成 .pot 文件模版
messages.pot:linux

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2019-10-23 13:37+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"

2. 修改 .pot 文件:寫入翻譯,保存爲 .po 文件app

messages.po:dom

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2019-10-23 13:37+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"


msgid "Hello world"
msgstr "你好,世界"

3. 運行 Python 安裝目錄下的 Tools/i18n/msgfmt.py,經過 .po 文件生成 .mo 文件
4. 將 .mo 文件放入文件夾 <localedir>/<language>/LC_MESSAGES/<domain>.mo (默認爲: /usr/share/locale/zh_CN/LC_MESSAGES/),文件名爲程序名ide

在項目中使用

實例化對象

首先須要實例化一個 oslo_i18n.TranslatorFactory 對象
以後將 .primary 封裝成 (_)
在報錯的字符串前使用 _ 函數對字符串進行處理函數

import oslo_i18n as i18n


DOMAIN = 'my_app'
_translators = i18n.TranslatorFactory(domain='my_app')

# The primary translation function using the well-known name "_"
_ = _translators.primary

# The contextual translation function using the name "_C"
# requires oslo.i18n >=2.1.0
_C = _translators.contextual_form
 
# The plural translation function using the name "_P"
# requires oslo.i18n >=2.1.0
_P = _translators.plural_form

# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical


def translate(value, user_locale):
    return i18n.translate(value, user_locale)


def get_available_languages():
    return i18n.get_available_languages(DOMAIN)

翻譯

將須要翻譯的字符串使用 _() 處理ui

LOG.error('There was an error.')
raise LocalExceptionClass(_('An error occurred.'))

方法

_()

在使用 _() 函數時須要注意的是:_() 是用來標記代碼中的可翻譯字符串,因此應該直接傳入字符串,而不是變量名翻譯

_C()

_C() 是用來進行上下文翻譯的日誌

_P()

_P() 是用來進行負數翻譯的code

Log Translation

官方再也不支持日誌翻譯,若是須要翻譯使用 _ 便可
以前支持的
LOG.INFO: _LI()
LOG.WARNING: _LW()
LOG.ERROR: _LE()
LOG.CRITICAL: _LC()

linux 系統語言

I18N 模塊會根據 Linux 系統語言的設定,去尋找相應的語言包

查看和設置 Linux 系統語言

經過 echo $LANG 進行查看
經過 export LANG=en_US.UTF-8 進行臨時修改 (即時生效,重啓失效)
經過修改配置文件 /etc/locale.conf 能夠永久生效,須要 source /etc/locale.conf 使其當即生效

相關文章
相關標籤/搜索