-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add translations for any hard-coded text within widgets #144
Comments
@NewbieXvwu, thank you for submitting. This is a good catch. Can you try running this code and post the results? I've read that this tcl/tk package can do the translations automatically... that would be nice. import tkinter as tk
def translate(text):
return root.tk.eval("namespace eval ::tk {::msgcat::mc %s}" % text)
root = tk.Tk()
tk.Button(root, text=translate("retry")).pack()
root.mainloop() |
My operating system is in Chinese, but it doesn't seem to translate... Maybe the translation function only supports some languages. |
Another possibility is that the translation function calls Google translation. Chinese mainland can not visit Google translation. |
Maybe only Linux system supports this function? I don't have a Linux environment. |
@NewbieXvwu I forced my system to german swiss and it worked on Windows 11. For some reason, I can't get it to work with the Chinese locale codes though. What result do you get if you run this? from gettext import gettext
result = gettext('Retry')
print(result) |
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32 |
Maybe there is no Chinese translation in tk? |
My idea is this: add a file similar to "en_US.json". When the program needs to read hard coded data, read it from the file, and automatically read the file similar to "zh_CN.json" by judging the system language setting to realize translation. This may be a supplement to TK's own translation function. |
@NewbieXvwu, I think this is something that is important to fix, but I'll need some time to consider the best way to implement something like this. At a minimum I could probably add a module that includes translations of standard labels used in custom widgets and dialogs. |
@NewbieXvwu, It looks like this mapping can be done within tcl/tk. I used google translate so hopefully it is right. I used the first command listed below, the 2nd one could be used to map multiple translations at once for a single locale.
If you will compile a list of all the translations you think I will need, I'll add it in a test module and we can see how it works in the application. Then we can test how it might scale to the other languages that are not supported (apparently) by tkinter. |
I think your idea is very good. What can I do for you now? |
I hope to separate the translation file from the code (similar to "zh_CN.json"), so that even people without Python basics can participate in the translation. |
I heard TK offered a "zh_cn.msg" file can help with translation. |
I don't see one. But we can definitely use one of these as a template. https://github.com/tcltk/tk/tree/main/library%2Fmsgs Would you mind taking one of these msg files and converting it to Chinese? Otherwise, if it's too big a task, I might be able to run a script to look them up. |
Wait a moment. I'm trying translate from en.msg. |
zh_cn.zip |
When I am free, I may be able to help translate your documents. |
If possible also add translation for widgets. weblate offers a free hosting plan, I don't know if it's compatible with tk inter https://docs.weblate.org/en/latest/index.html |
@NewbieXvwu, two things... I created a check that loads the custom msg file if it's in a list of locales... which there is only one currently. The other thing is that the msg file has to map between English and Chinese as you can see from this Russian msg file. I added it in manually for retry in the msg file, but we'll have to add the Chinese translation after the English word for all of them. |
@antrrax I tried weblate, but it doesn't seem compatible with msg format. |
@israel-dryer I'm finished. |
@israel-dryer What should be "langdir" in your code? |
That's the directory that contains the msg file |
I searched for the terms to be translated. Here is the sample in Brazilian Portuguese. pt_br.msg:
on the recommended site to generate the language code: uses hyfem to separate language from country (pt-br) The windows title cannot be translated. It shows an error there try to use this From what I've observed, you search for the translation by the term and not by a code. In your Readme file, list some repeated terms. The question remains, how does the code deal with this? Using the term to search for a translation is a serious problem, as there are some words that in English can have more than one meaning depending on the context. Ex: glass, can Another example, "Slant" the literal translation would be "inclinado". However, in fluent Portuguese, no one would use this term to refer to the style of a font. ("Slant" - "inclinado") would be used in the context of a "sloping" street (rua "inclinada") Reusing terms is problematic, as translation depends on context. Complement;Another problem, in English some words do not have gender or plural variation. term: NewEnglish example: New Folder English example: New Document English example (plural): New Documents Portuguese translation: 'Novo' (masculine) In fluent Portuguese 'Weight' refers to the mass of an object. So the literal translation would be correct in the mass context, but wrong in the font property context. Reusing English terms will have translation context problems. The correct thing is that each sentence has its own translation. That's why it's important to use code to search for a term/phrase |
I think the MessageCatalog component has a very limited application... localizing button text and labels and other standard application elements. The 'msg' file acts almost like a python dictionary where it looks the term used and attempts to find a match. It's also case sensitive from what I can tell, so it treats "ok", "Ok", and "OK" as 3 separate tokens. This is why you may see a repeated term with different capitalization applied. It's a simplistic mechanism for translating your buttons and labels via the use of msg files instead of putting that business logic in your code. When looking for a translation, I would go for the meaning and not necessarily a literal translation. I'm guessing there is a typical way that different localities handle ok, cancel, new, etc... So, if you are familiar with that culture, I would leave it up to you to convey the correct meaning with the mapped term. Can you show the error you get when translating the title? |
Dialog titles are in the class initializer definition. file: ..../ttkbootstrap/dialogs/dialogs.py wrong way to try to translate the title: file: ..../ttkbootstrap/dialogs/colorchooser.py |
In the new README file that is on github, all repeated terms are identical. the term 'Family' is repeated, and is identical: it has the same letters and case. |
Ok. Then it must is have been an oversight on my part, copying and pasting. |
You can translate the term above the super() and then pass the translated term into the call to super() |
With this modification it worked:
|
I did the translation of the terms. In ColorChooserDialog In DateEntry, I didn't find a way to translate the name of the months and the abbreviation of the days of the week. FontDialog all ok In the tableview there are many symbols next to the names, at first I chose to include the symbols in the translation. But the symbol "🞨" of the "Delete column" and "Delete selected rows" did not accept the translation. So I would have to format these strings in another way. f'''🞨 {MessageCatalog.translate("Delete selected rows")}''' See the files as they were after this initial modification, once you decide on which string formatting pattern I will redo: |
|
Posting for reference on getting localized date info |
Using the tip from the link above, I changed the dialogs file to display the month name in the default language of the user's system. I tested it here on linux on the real machine, and on win 10 on a virtual machine in the pt_br language and it worked. I don't know how it would behave in other languages. See if you agree with the resolution: line 11: line 609: line 746
View the modified file: |
@israel-dryer How can I translate the toast documation? |
The API docs are tricky because the documentation is in the docstring. My best solution is to copy the docstring from the source document and then put it into this format using markdown. This version is live, so you can see what it looks like online. I was not able to replicate all of the look & feel of the original without resorting to HTML, which would be too tedious to maintain. The markdown What it looks like online |
On the documentation page, would it be possible to restrict the search to the selected language only? For example, search for the term:
|
Unfortunately, it doesn't look like that feature is supported yet. https://github.com/ultrabug/mkdocs-static-i18n#compatibility-with-the-search-plugin However, once the documents are fully translated, it should not be an issue since the word 'Utility' will not be shown in English characters. |
@israel-dryer Can I read another file according to the language selection when reading the string in the file? |
I'm not sure what you mean. Can you give an example? |
Can I store descriptions in two different languages in the source file and read them on demand? |
@israel-dryer You may not understand what I mean... I mean that I can't translate the contents of all "style guide" sections because they are rendered by reading data from py source files. Can I store text in two languages in py source files for translation? |
Oh. I'm not sure yet. Because we are talking about the contents of the python files, we might be better off approaching this with the pot/po files and the gettext module, or babel library. However, I'd have to do some research on that personally. I've not done large-scale translation projects before, and that will require some setup for it to work smoothly. |
@israel-dryer Perhaps you can use a GitHub Action script to automatically generate a markdown file from the py file at each commit. I know a little bit about GitHub Action. |
Chinese translation was added to tk 8.6 |
@antrrax Thanks! |
Some custom windows (such as themed dialog boxes) are used in your program, but it seems that some of their contents are in English and cannot be changed. Would you mind providing an way to translate it to other languages? I am willing to help translate it into Chinese.
The text was updated successfully, but these errors were encountered: