Reputation: 21
I am putting together a client in Python 3.5 that can be used to retrieve completed translation jobs from a site. I decided to use buttons that copy the translated content to the clipboard for usability reasons. Depending on whether the translation is a plain text or saved in a file, the buttons display either "Download" or "Copy to clipboard".
It is working for the most part - I can copy and paste texts in English with no problem by clicking the button and then pasting the contents to a text editor.
Unfortunately, I get some funky characters when dealing with German (stuff like ä, ö and ü) and Japanese turns into mush.
def retrieveJobs(self):
availableJobIDs = gengo.getTranslationJobs(status='reviewable')['response']
text_count = 0
text_dict = {}
file_count = 0
file_dict = {}
for i in range(len(availableJobIDs)):
job = gengo.getTranslationJob(id=availableJobIDs[i]['job_id'])['response']['job']
title = job['slug']
titleLabel = tk.Label(self, text=title)
titleLabel.grid(row=i+3, sticky=tk.E)
if 'file_url_tgt' in job.keys():
link = job['file_url_tgt']
file_dict[file_count] = link
linkButton = tk.Button(self, text='ダウンロード',
command=lambda file_count=file_count: urllib.request.urlopen(url=file_dict[file_count]))
linkButton.grid(row=i+3, column=1, sticky=tk.W+tk.E)
file_count += 1
else:
text = job['body_tgt']
text_dict[text_count] = text
copyButton = tk.Button(self, text='訳文をコピーする',
command=lambda text_count=text_count: tk.clipboard_append(text_dict[text_count]))
copyButton.grid(row=i+3, column=1, sticky=tk.W)
text_count += 1
The head of the script is marked with
# -*- coding: utf-8 -*-
However, when I press one of the buttons to copy a text in Japanese, like
これはどのように動作し、すべての順序のどこに表示する短いテキストです。
(don't mind the meaning, it's gibberish) and copy it to a Word file, the resulting text looks like this:
これはどのように動作し、すべての順序のどこに表示する短いテキストです。
How can I modify the code to display Japanese characters properly? As seen in the code, the text is stored in a dictionary and, at that point, is still intact. Only when pulling it out with pyperclip does it turn all funky.
I hope the explanation is sufficient.
Thank you in advance!
Upvotes: 1
Views: 911
Reputation: 21
Turns out I only had to make a small adjustment here:
copyButton = tk.Button(self,
text='訳文をコピーする',
command=lambda text_count=text_count: tk.clipboard_append(text_dict[text_count]))
The functioning version looks like this:
copyButton = tk.Button(self,
text='訳文をコピーする',
command=lambda text_count=text_count: root.clipboard_append(text_dict[text_count]))
No more funky characters in either German or Japanese. Other languages such as French and Chinese are now functional, as well.
Thank you again for the input! It helped a lot to get a new perspective on the code.
Upvotes: 1