ibm云上用于全球化管道的python客户端
gp-python-client-sid的Python项目详细描述
GP Python客户端
gp-python-client
是IBM Globalization Pipeline的正式python客户端。
ibm globalization pipeline是一个devops集成的应用程序转换管理服务,您可以使用它来快速地向全球客户转换和发布云和移动应用程序。通过其仪表板、restful api访问ibm全球化管道功能,或将其无缝集成到应用程序的交付管道中。
这个包扩展了gettext module(可作为python标准库的一部分使用)并为python应用程序提供使用全球化管道服务的功能。
开始
首先,您应该熟悉服务本身。一个 最好从阅读Quick Start Guide和正式的Getting Started with IBM Globalization 文档开始。
文档说明了如何在ibm cloud上找到服务;支持的身份验证机制;创建新的服务实例;创建新的捆绑包;访问转换后的消息。
演示
这个demo Bluemix app使用带有python客户端的全球化管道来显示一条简短的欢迎消息,该消息被翻译成多种语言。演示应用程序的源代码可以在demo dir中找到。
安装
要安装gp-python-client
,只需运行:
$ pip install gp-python-client
或者如果你是老学生,请跑步:
$ easy_install gp-python-client
API
包的api文档可以在here中找到。如果链接断了,您可以自己generate the documentation 。
示例
示例1-BlueMix应用程序:
此示例代码将允许您在BlueMix应用程序中开始使用全球化管道服务。该示例假设BlueMix应用程序已连接到全球化管道服务实例,并且有一个名为myBundle
的捆绑包,并且该捆绑包包含一个带有键greet
的源字符串。
>>>fromgpclientimportGPClient,GPServiceAccount,GPTranslations>>>importlocale>>>>>>acc=GPServiceAccount()>>>client=GPClient(acc)>>>>>>languages=[locale.getdefaultlocale()[0]]# languages=['fr_CA']>>>>>>t=client.translation(bundleId='myBundle',languages=languages)>>>_=t.gettext# create alias for method>>>>>>print_('greet')# 'greet' key's French translated value will be usedBonjour>>>
示例2-非BlueMix应用程序:
此示例代码将允许您在一个独立的python应用程序中开始使用全球化管道服务,该应用程序不在bluemix上托管。该示例假定一个全球化管道服务实例以一个名为^ {CD3}}的包存在,并且该包包含一个带有密钥{{*CD6}}的源字符串。
你需要service instance credentials。
>>>fromgpclientimportGPClient,GPServiceAccount,GPTranslations>>>importlocale>>>>>>acc=GPServiceAccount(url=url,instanceId=instId,userId=userId,password=passwd)# Using Globalization Pipeline authentication# Using IAM # acc = GPServiceAccount(url=url, instanceId=instanceId,# apiKey=apiKey)>>>client=GPClient(acc)>>>>>>languages=[locale.getdefaultlocale()[0]]# languages=['es-mx']>>>>>>t=client.translation(bundleId='myBundle',languages=languages)>>>_=t.gettext# create alias for method>>>>>>print_('exit')# 'exit' key's Spanish translated value will be usedAdiós>>>
您还可以通过json文件提供服务凭据,如下面的代码片段所示
>>>fromgpclientimportGPClient,GPServiceAccount>>>>>>acc=GPServiceAccount(credentialsJson="./local_credentials.json")>>>client=GPClient(acc)
获取语言/区域设置代码
此包要求在被询问时提供有效的(符合bcp47的)语言/区域设置代码;例如,在调用GPClient.translation()
时(请参见Examples)。从这些代码中,将提取语言、区域和脚本子标记。
一些示例代码是:
- 中文
- pt br
- ja
- 我们
有几种方法可以获取工作区域设置的代码。单向 使用locale module(作为python标准库的一部分可用)。
>>>importlocale>>>myLocale=locale.getdefaultlocale()>>>printmyLocale('en_US','UTF-8')>>>code=myLocale[0]>>>printcodeen_US
对于上面的示例,语言代码是en_US
,其中en
是语言子标记,而US
是区域子标记。
运行测试
请参阅test/README.md。
生成文档
文档可以使用Sphinx生成。
您必须首先安装它:
$ pip install sphinx
然后,要自动生成文档,请运行:
$ cd$BASEDIR/docs $ make clean $ make html
要浏览文档,请打开$BASEDIR/docs/_build/html/index.html
。
创建分发包
如果需要,首先更新CHANGES.txt
和setup.py
(例如,更新版本号),然后创建首选的distribution package。
车轮分布(推荐)
$ pip install wheel $ python setup.py bdist_wheel
源分发
$ python setup.py sdist
注意:源发行版也包含测试。
生成分发版
$ python setup.py bdist
新的分发文件应位于$BASEDIR/dist/
下。
社区
- 查看或文件github Issues
- 与^{a16上的开源社区连接}
贡献
许可证
阿帕奇2.0。见LICENSE.txt。
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.