自动翻译 android/res/values/strings.xml
一、问题
(图片来源网络,侵删)
自动翻译res/values/strings.xml
二、解决
本来想注册google 翻译api,不通过。(信用卡通过,身份证验证不通过,谁有办法?)
现在用百度翻译api,官网https://fanyi-api.baidu.com/product/113
如何使用通用翻译API?
- 使用您的百度账号登录百度翻译开放平台;
- 注册成为开发者,获得 APPID ;
- 进行开发者认证(如仅需标准版可跳过);
- 开通通用翻译API服务:开通链接;
- 参考技术文档和 Demo 编写代码。
三、例子
E:\DOWNLOADS\API_DEMO_BAIDU_TEXT_TRANSAPI_V1.0.0.0
│ test_xml_translate_main.py
│
├─out
│ strings.xml
│
└─translate_api
Baidu_Text_transAPI.py
test_xml_translate_main.py
#!/usr/bin/env python3
# zhy 20240620
'''
translate xml string
'''
import xml.etree.ElementTree as ET
import time
import sys
import translate_api.Baidu_Text_transAPI
import json
def print_string(root):
for string in root:
print(string.attrib)
print(string.text)
query = string.text
# query = 'Hello World! This is 1st paragraph.'
print(query)
result_json_dict = translate_api.Baidu_Text_transAPI.Baidu_Text_transAPI(query)
print(result_json_dict)
string.text = result_json_dict['trans_result'][0]['dst']
# with open ('test_translate_result_json.json', encoding='utf-8') as f:
# data = json.load(f)
# print(type(data))
# print(data)
# print(data['trans_result'][0]['dst'])
# stringaa = data['trans_result'][0]['dst']
# print(stringaa)
# # string.text = "中文内容".encode("utf-8")
# # string.text = "中文内容"
# # string.text = "aaaabbbb"
# string.text = stringaa
def write_out(tree):
tree.write("out/strings_translate.xml", encoding='utf-8', xml_declaration=True)
def translate_string(file_path_strings_xml):
tree = ET.parse(file_path_strings_xml)
root = tree.getroot()
print_string(root)
write_out(tree)
def main():
if len(sys.argv)
Baidu_Text_transAPI.py
# -*- coding: utf-8 -*-
# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install `requests` to run this code: pip install requests
# Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document
import requests
import random
import json
from hashlib import md5
import time
# Set your own appid/appkey.
appid = 'INPUT_YOUR_APPID'
appkey = 'INPUT_YOUR_APPKEY'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
query = 'Hello World! This is 1st paragraph.\nThis is 2nd paragraph.'
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
def Baidu_Text_transAPI(query):
# 免费版 Qps=1
time.sleep(1)
# Generate salt and sign
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result_json_dict = r.json()
# Show response
result_json_str= json.dumps(result_json_dict, indent=4, ensure_ascii=False)
print(result_json_str)
return result_json_dict
strings.xml
Hello World!
Hello China!
Hello Python!
申请百度翻译账号,填写
appid = 'INPUT_YOUR_APPID'
appkey = 'INPUT_YOUR_APPKEY'
python.exe .\test_xml_translate_main.py .\out\strings.xml
生成 .\out\strings_translate.xml
英语字符,被翻译成中文字符
例子简单粗糙,记录一下,备忘。
四、参考
为什么要使用谷歌云的翻译接口
http://www.tadke.com/wangzhan-jiaocheng/wordpress-jiqiao/02/26552.html
百度翻译开发平台
https://fanyi-api.baidu.com/product/113
Python数据处理(一):处理 JSON、XML、CSV 三种格式数据
https://juejin.cn/post/7047386538266755103
菜鸟教程 Python3 XML 解析
https://www.runoob.com/python3/python3-xml-processing.html
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
