需求很簡單,翻譯官網的操作文檔
下載ceph代碼luminous版本
這個只用來編譯doc的,我們只需要最新的這個分支即可,拉最少的代碼
git clone -b v12.2.13 --single-branch --depth 1 git://github.com/ceph/ceph.git ceph12
cd ceph12
git checkout -b v12.2.13
代碼就下好了
根據官網的文檔可以知道,官網的文檔的語法是Sphinx寫的文檔,然后通過工具可以渲染成固定的網頁
而Sphinx寫的文檔是支持國際化的,也就是我們只需要通過固定的文件去修改翻譯文本就可以實現整個文檔的翻譯,而翻譯的過程是可以通過發送到google,然后拿到結果再填寫回去的方式來處理
安裝依賴包
cd admin
./build-doc
根據提示安裝依賴包,有一個ditaa安裝不上
wget ftp://ftp.pbone.net/mirror/archive.fedoraproject.org/fedora-secondary/releases/20/Everything/ppc64/os/Packages/d/ditaa-0.9-10.r74.fc20.noarch.rpm
wget https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/20/Everything/x86_64/os/Packages/j/jericho-html-3.2-6.fc20.noarch.rpm
rpm -ivh jericho-html-3.2-6.fc20.noarch.rpm
yum localinstall ditaa-0.9-10.r74.fc20.noarch.rpm
修改build-doc腳本的60行
virtualenv修改為
virtualenv-3
生成的文檔在
[root@lab101 admin]# ls ../build-doc/output/html/
執行
./serve-doc
訪問主機的8080端口
就可以看到一個原版的文檔了
我們把這個備份下,方便后面比對翻譯的效果
[root@lab101 admin]# unalias cp
[root@lab101 admin]# cp -ra ../build-doc/output/html/* /usr/share/nginx/html/
[root@lab101 admin]# systemctl start nginx
腳本里面還有其它的一些操作,實際上主要的編譯命令是這個命令
/root/ceph/ceph12/build-doc/virtualenv/bin/sphinx-build -a -b dirhtml -d doctrees /root/ceph/ceph12/doc /root/ceph/ceph12/build-doc/output/html
我們的版本
[root@lab101 admin]# /root/ceph/ceph12/build-doc/virtualenv/bin/sphinx-build --version
sphinx-build 2.1.2
安裝國際化的處理軟件
/root/ceph/ceph12/build-doc/virtualenv/bin/pip3 install sphinx-intl
提取pot文件
/root/ceph/ceph12/build-doc/virtualenv/bin/sphinx-build -b gettext /root/ceph/ceph12/doc locale
根據pot文件生成zh的po文件
/root/ceph/ceph12/build-doc/virtualenv/bin/sphinx-intl update -p locale/ -l zh
然后需要修改翻譯就修改po文件
會在本地生成locales文件夾,把文件夾拷貝到doc目錄下面
修改doc/conf.py文件
添加三個設置配置
locale_dirs = ['locales/']
gettext_compact = False
language= 'zh_CN'
然后編譯的就會取翻譯中文的html
處理po文件
下一步處理po文件,通過python處理
安裝軟件
pip-3 install polib
po文件的完整路徑
ll /root/ceph/ceph12/doc/locales/zh_CN/LC_MESSAGES/
獲取po文件的完整列表
polib的使用方法
這里用翻譯po文件的代碼舉例
#! /usr/bin/python3
# -* coding:UTF-8 -*-
import time
import sys
import polib
import sys
import add_space
sys.path.append('../')
from google_translate import translate_google
#translate_google.translate_text_with_glossary("mozha,hello world")
def translate_po(file_path):
po = polib.pofile(file_path)
for entry in po:
print("原始字符串:")
print(entry.msgid)
mystringa=entry.msgid
newmsg=translate_google.translate_text_with_glossary("%s" %(mystringa))
print("翻譯后未處理的字符串:")
print(newmsg)
newmsg=add_space.add_string_space(newmsg)
entry.msgstr = newmsg
print("翻譯后的字符串:")
print(entry.msgstr)
time.sleep(0.1)
po.save(file_path)
translate_po(sys.argv[1])
這里用的比較少,流程是讀取po文件,拿到原始字符串,然后翻譯,再回寫po文件,再進行保存,就完成一個po文件的處理了
google 的translate的用法
在憑據里面的服務賬號里面創建一個密鑰用於訪問翻譯的api
保存下來是一個json的文件
設置環境變量
我的是
export GOOGLE_APPLICATION_CREDENTIALS="/root/ceph/ceph12/admin/translate/3-google-translate/zphj1987-translate-797fe0ff0849.json"
[root@lab101 google_translate]# cat /etc/bashrc |tail -n 1
export GOOGLE_APPLICATION_CREDENTIALS="/root/ceph/ceph12/admin/translate/google_translate/zphj1987-translate-797fe0ff0849.json"
可以寫到bashrc文件里面,登錄命令行就直接可以使用了
安裝python的翻譯庫
我們使用的是高級版本的
構建自己的術語表,需要上傳到google的存儲上面,然后告訴google翻譯,去存儲上面拿最新的存儲的術語的文件,然后處理好了以后,返回成功
安裝存儲
官網推薦的是如下
pip-3 install --upgrade google-cloud-storage
上面的版本有問題嘗試用之前的版本
pip-3 install google-cloud-translate==2.0.2
官網的文檔沒有更新到最新,匹配的是上面的2版本的
上傳代碼
[root@lab101 3-google-translate]# cat uoload-csv.py
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)
upload_blob('mytranslate','./ceph-glo.csv', 'ceph-glo.csv')
上傳好自己的詞庫,還要告訴Google 翻譯進行更新,字庫的處理需要寫腳本去讀取po文件,然后保留一些固定的格式
安裝gloud
tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM
yum install google-cloud-sdk
設置一個request文件來指定需要更新到哪個詞庫文件,然后先刪除再更新即可
翻譯的代碼
#! /usr/bin/python3
# -* coding:UTF-8 -*-
from google.cloud import translate_v3 as translate
def translate_text_with_glossary(
text="YOUR_TEXT_TO_TRANSLATE",
project_id="zphj1987-translate",
glossary_id="myceph-en-zh",
):
client = translate.TranslationServiceClient()
parent = client.location_path(project_id, "us-central1")
glossary = client.glossary_path(
project_id, "us-central1", glossary_id # The location of the glossary
)
glossary_config = translate.types.TranslateTextGlossaryConfig(
glossary=glossary)
response = client.translate_text(
contents=[text],
target_language_code="zh",
source_language_code="en",
parent=parent,
glossary_config=glossary_config,
)
for translation in response.glossary_translations:
print(u"{}".format(translation.translated_text))
translate_text_with_glossary("mozha,hello world")
可以去google 官網查詢api的用法
詞庫的提取方法
[root@lab101 glocsv]# cat get_po_link_single.py
#! /usr/bing/python3
#-*- coding:UTF-8 -*-
#這個是處理`xxxx`_鏈接的
import polib
import re
import sys
po = polib.pofile(sys.argv[1])
for entry in po:
ms_string=entry.msgid
for rst_link in re.findall(r"`(.*?)`_",ms_string):
if '`' in rst_link:
if rst_link.split('`')[-1] == "":
pass
else:
print("\"`"+rst_link.split('`')[-1]+"`_\""+","+"\"`"+rst_link.split('`')[-1]+"`_\"" )
elif rst_link == "":
pass
else:
print("\"`"+rst_link+"`_\""+","+"\"`"+rst_link+"`_\"")
上面是處理單個po文件的,可以做好po文件的list,然后腳本去處理
[root@lab101 glocsv]# cat get_po_link_single.sh
#! /bin/sh
rm -rf po_link.csv
for pofile in `cat polist-do`
do
echo $pofile
python3 get_po_link_single.py $pofile >> po_link.csv.tmp
done
cat po_link.csv.tmp|sort|uniq > po_link.csv
sed -i '1 s/^/\xef\xbb\xbf&/' po_link.csv
rm -rf po_link.csv.tmp
上面的是其中一個例子的情況
需要特殊的處理哪些情況
翻譯回來的中文會有問題,需要處理一些情況,主要的代碼如下:
[root@lab101 translate_po]# cat add_space.py
#! /usr/bin/python3
# -* coding:UTF-8 -*-
import re
##處理雙引號無空格的問題
# 處理 **xxxxx**
def add_string_space(mystring):
for rst_link in re.findall(r"\*\*(.*?)\*\*",mystring):
newstring="**"+rst_link+"**"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
# 處理`xxxx`_
for rst_link in re.findall(r"`(.*?)`_",mystring):
# print(rst_link)
if '`' in rst_link:
if rst_link.split('`')[-1] == "":
pass
else:
newstring='`'+rst_link.split('`')[-1]+"`_"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
elif rst_link == "":
pass
else:
newstring="`"+rst_link+"`_"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
#print(mystring)
# 處理 :term:`xxx`
for rst_link in re.findall(r":term:`(.*?)`",mystring):
newstring=":term:`"+rst_link+"`"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
# 處理``xxxx``
for rst_link in re.findall(r"``(.*?)``",mystring):
newstring="``"+rst_link+"``"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
# 處理:doc:`xxxx`
for rst_link in re.findall(r":doc:`(.*?)`",mystring):
newstring=":doc:`"+rst_link+"`"
mystring=mystring.replace('%s' %(newstring),' %s ' %(newstring))
# 處理其它情況
mystring=mystring.replace('。','.')
mystring=mystring.replace(',',',')
mystring=mystring.replace('”','')
mystring=mystring.replace('“','')
mystring=mystring.replace('(','(')
mystring=mystring.replace(')',')')
mystring=mystring.replace('\'\'','')
mystring=mystring.replace(' :',':')
mystring=mystring.replace('_ _','_ ')
mystring=mystring.replace('的Ubuntu','Ubuntu')
mystring=mystring.replace('CentOS的','CentOS')
mystring=mystring.replace(':項:` ','')
mystring=mystring.replace(' 。','. ')
mystring=mystring.replace(':',':')
mystring=mystring.replace(',',',')
mystring=mystring.replace('/ ``',' ``')
mystring=mystring.replace('Bug Tracker_',' Bug Tracker_ ')
mystring=mystring.replace('ceph-users@ceph.com',' ceph-users@ceph.com ')
mystring=mystring.replace('http://ceph.com/docs',' http://ceph.com/docs ')
mystring=mystring.replace('ceph-devel@vger.kernel.org',' ceph-devel@vger.kernel.org ')
mystring=mystring.replace('ceph-commit@ceph.com',' ceph-commit@ceph.com ')
mystring=mystring.replace('http://github.com',' http://github.com ')
mystring=mystring.lstrip()
return(mystring)
流程總結
第一次處理全局翻譯
1、獲得一個google cloud translate的api,能夠通過api進行翻譯,這個也可以嘗試其它的api,能夠給英文返回中文及即可
2、提取ceph doc的po文件,能夠修改po文件來實現翻譯
3、維護一個字庫,通過分析po文件的內容,腳本提取自己需要保留的固定格式或者固定的翻譯,處理好了以后,提交到google 翻譯里面更新
4、用腳本處理翻譯返回的結果,然后再保留到po文件里面去
需要更新的情況
1、更新字庫
2、更新處理腳本
3、翻譯更新po文件
基於以上就可以打造一個自動翻譯文檔的系統了,當然這個翻譯本身是需要一個校對的過程,所以使用過程中應該是不斷晚上后面說的部分,就是維護好字庫,維護好更新處理腳本,這個打造好了以后,再來新的版本,直接進行翻譯即可,而校對的過程可以邊學習文檔邊處理
翻譯的api高級功能一般都是需要收費的,這個通常來說通過人工一個個去翻譯,肯定花費的時間功夫是超過了api處理的,並且來新版本是能夠很快處理完成的
更詳細的代碼
更詳細的代碼存放地址:
https://github.com/zphj1987/translate-ceph-doc-2020
由於對賬戶的依賴性比較高,上面的文章只記錄了相關的思路,和重要的處理點,具體的可以自己實現
變更記錄
Why | Who | When |
---|---|---|
創建 | 武漢-運維-磨渣 | 2020-09-04 |