Last active
October 12, 2019 12:06
-
-
Save PickledChair/defb0c7f7829185f32553d8df9b9f8ca to your computer and use it in GitHub Desktop.
The script to translate sentences by using the web api created with Google Apps Script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"url": "https://url.to.your.webapp", | |
"source": "en", | |
"target": "ja", | |
"langs": [ | |
"en", | |
"ja", | |
"zh-cn", | |
"de" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ui | |
import appex | |
import dialogs | |
import console | |
from objc_util import * | |
import json | |
import urllib3 | |
from urllib3.exceptions import InsecureRequestWarning | |
urllib3.disable_warnings(InsecureRequestWarning) | |
setting = None | |
http = None | |
v = None | |
def main(): | |
global setting, http, v | |
setting = get_setting() | |
http = urllib3.PoolManager() | |
text = appex.get_text() | |
v = ui.load_view() | |
ui_initialize(text) | |
v.present('sheet') | |
def translate(text): | |
query = { | |
'text': text, | |
'source': setting['source'], | |
'target': setting['target'], | |
} | |
r = http.request('GET', setting['url'], fields=query) | |
if r.status == 200: | |
return r.data.decode('utf-8') | |
else: | |
return None | |
def ui_initialize(text): | |
global v | |
v['button3'].title = setting['source'] | |
v['button4'].title = setting['target'] | |
if text is not None: | |
v['textview1'].text = text | |
translated = translate(text) | |
if translated: | |
v['textview2'].text = translated | |
def get_setting(): | |
with open('setting.json', 'r') as f: | |
setting = json.load(f) | |
return setting | |
def save_setting(): | |
with open('setting.json', 'w') as f: | |
json.dump(setting, f, indent=4, ensure_ascii=False) | |
def switch_source_target(sender): | |
global setting, v | |
setting['source'], setting['target'] = setting['target'], setting['source'] | |
save_setting() | |
v['button3'].title = setting['source'] | |
v['button4'].title = setting['target'] | |
def switch_tv_1_2(sender): | |
global v | |
v['textview1'].text, v['textview2'].text = v['textview2'].text, v['textview1'].text | |
def push_translate(sender): | |
global v | |
text = v['textview1'].text | |
translated = translate(text) | |
if translated is not None: | |
v['textview2'].text = translated | |
def push_source(sender): | |
global setting, v | |
source = dialogs.list_dialog( | |
'Source を選択', | |
setting['langs'] | |
) | |
if source: | |
setting['source'] = source | |
save_setting() | |
v['button3'].title = setting['source'] | |
def push_target(sender): | |
global setting, v | |
target = dialogs.list_dialog( | |
'Target を選択', | |
setting['langs'] | |
) | |
if target: | |
setting['target'] = target | |
save_setting() | |
v['button4'].title = setting['target'] | |
def add_lang(sender): | |
global setting | |
lang = console.input_alert( | |
'言語を追加', | |
'追加する言語を入力してください', | |
'', '追加' | |
) | |
if lang: | |
setting['langs'].append(lang) | |
save_setting() | |
class MyView (ui.View): | |
def __init__(self): | |
pass | |
def touch_began(self, touch): | |
# Called when a touch begins. | |
for subv in self.subviews: | |
if isinstance(subv, ui.TextView): | |
subvo = ObjCInstance(subv) | |
if subvo.isFirstResponder(): | |
subvo.resignFirstResponder() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"nodes" : [ | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{122, 183}, {103, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"uuid" : "7AC2C5BA-AD24-4311-AD01-BD9DC88DA443", | |
"name" : "button5", | |
"font_size" : 15, | |
"corner_radius" : 0, | |
"frame" : "{{80, 224}, {80, 32}}", | |
"tint_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"border_width" : 0, | |
"title" : "上下の文を入替", | |
"action" : "switch_tv_1_2", | |
"class" : "Button", | |
"flex" : "LRTB" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{104, 6}, {33, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"font_size" : 15, | |
"flex" : "LR", | |
"action" : "switch_source_target", | |
"frame" : "{{80, 104}, {80, 32}}", | |
"tint_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"title" : "", | |
"uuid" : "B4106DF0-ED67-436B-977C-1B445BACBDE3", | |
"class" : "Button", | |
"name" : "button1", | |
"image_name" : "iob:arrow_swap_24" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{6, 46}, {47, 32}}", | |
"class" : "Label", | |
"attributes" : { | |
"name" : "label1", | |
"frame" : "{{45, 104}, {150, 32}}", | |
"uuid" : "3E5AD784-7D70-49A7-9A29-D29C4A22D849", | |
"class" : "Label", | |
"alignment" : "left", | |
"text" : "原文:", | |
"font_size" : 16, | |
"font_name" : "<System>" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{20, 86}, {205, 90}}", | |
"class" : "TextView", | |
"attributes" : { | |
"uuid" : "1A2CD453-9CAF-4B9C-B95B-192BBD91C1C3", | |
"font_size" : 17, | |
"corner_radius" : 5, | |
"frame" : "{{20, 20}, {200, 200}}", | |
"border_color" : "RGBA(0.840531,0.840531,0.840531,1.000000)", | |
"editable" : true, | |
"border_width" : 1, | |
"alignment" : "left", | |
"autocorrection_type" : "default", | |
"font_name" : "<System>", | |
"spellchecking_type" : "default", | |
"class" : "TextView", | |
"name" : "textview1", | |
"flex" : "WHB" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{6, 183}, {47, 32}}", | |
"class" : "Label", | |
"attributes" : { | |
"name" : "label2", | |
"flex" : "TB", | |
"frame" : "{{45, 104}, {150, 32}}", | |
"uuid" : "8CCF6367-09C9-49AA-9669-8371F4418DD7", | |
"class" : "Label", | |
"alignment" : "left", | |
"text" : "翻訳:", | |
"font_size" : 16, | |
"font_name" : "<System>" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{20, 223}, {205, 120}}", | |
"class" : "TextView", | |
"attributes" : { | |
"uuid" : "2BA59F1B-9ED7-46C9-BB05-712A80D34599", | |
"font_size" : 17, | |
"corner_radius" : 5, | |
"frame" : "{{20, 20}, {200, 200}}", | |
"border_color" : "RGBA(0.842228,0.842228,0.842228,1.000000)", | |
"editable" : true, | |
"border_width" : 1, | |
"alignment" : "left", | |
"autocorrection_type" : "default", | |
"font_name" : "<System>", | |
"spellchecking_type" : "default", | |
"class" : "TextView", | |
"name" : "textview2", | |
"flex" : "WHTB" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{84, 183}, {72, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"font_size" : 15, | |
"flex" : "LRTB", | |
"action" : "push_translate", | |
"frame" : "{{80, 224}, {80, 32}}", | |
"tint_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)", | |
"title" : "翻訳", | |
"uuid" : "05B8D6F5-48CE-45A0-ADF5-B305C1097599", | |
"class" : "Button", | |
"background_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"name" : "button2", | |
"image_name" : "iob:arrow_down_a_24" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{16, 6}, {80, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"uuid" : "652AF94D-5113-45C5-828C-21F279F4F79F", | |
"font_size" : 15, | |
"corner_radius" : 0, | |
"frame" : "{{80, 224}, {80, 32}}", | |
"tint_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"border_width" : 0, | |
"title" : "Button", | |
"action" : "push_source", | |
"class" : "Button", | |
"name" : "button3", | |
"flex" : "LR" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{145, 6}, {80, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"uuid" : "8153F20A-32D8-4787-8240-67DB708C3613", | |
"font_size" : 15, | |
"corner_radius" : 0, | |
"frame" : "{{80, 224}, {80, 32}}", | |
"tint_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"border_width" : 0, | |
"title" : "Button", | |
"action" : "push_target", | |
"class" : "Button", | |
"name" : "button4", | |
"flex" : "LR" | |
}, | |
"selected" : false | |
}, | |
{ | |
"nodes" : [ | |
], | |
"frame" : "{{76, 46}, {89, 32}}", | |
"class" : "Button", | |
"attributes" : { | |
"uuid" : "B917833A-811A-4EAC-8AAE-DBB7A9D5F359", | |
"font_size" : 15, | |
"corner_radius" : 5, | |
"frame" : "{{80, 224}, {80, 32}}", | |
"tint_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"border_width" : 1, | |
"border_color" : "RGBA(0.000000,0.474510,1.000000,1.000000)", | |
"title" : "言語を追加", | |
"action" : "add_lang", | |
"class" : "Button", | |
"name" : "button6", | |
"flex" : "LR" | |
}, | |
"selected" : false | |
} | |
], | |
"frame" : "{{0, 0}, {240, 480}}", | |
"class" : "View", | |
"attributes" : { | |
"flex" : "", | |
"custom_class" : "MyView", | |
"enabled" : true, | |
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)", | |
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", | |
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)", | |
"name" : "Google翻訳" | |
}, | |
"selected" : false | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet(e) { | |
var p = e.parameter; | |
var translatedText = LanguageApp.translate(p.text, p.source, p.target); | |
return ContentService.createTextOutput(translatedText); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment