Skip to content
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 localization support #24

Open
uncle-yura opened this issue Mar 2, 2020 · 5 comments
Open

Add localization support #24

uncle-yura opened this issue Mar 2, 2020 · 5 comments

Comments

@uncle-yura
Copy link

uncle-yura commented Mar 2, 2020

As I understand it, the error message code is not optimized to create localization. There is no great need for this, but it would be nice to add this feature too.

Just add "_ ()":

print(_("Triggered LinuxCNC E-Stop"))

Also need to be added at the beginning:

import locale 
import gettext

and something like this:

        locale.setlocale( locale.LC_ALL, '' )
        locale.bindtextdomain( "awlsim", LOCALEDIR )
        gettext.install( "awlsim", localedir = LOCALEDIR, unicode = True )
        gettext.bindtextdomain( "awlsim", LOCALEDIR )
@mbuesch
Copy link
Owner

mbuesch commented Mar 2, 2020

Yes, localisation support would be a really nice feature to have.
Unfortunately that's a lot of work that I'm currently not able to do alone.
There is a huge amount of text in awlsim.
What would have to be done is:

  • Route all texts through localisation translation function. It's not fully clear to me how that would have to look like. This has to be done in the core (No Qt support) and in the GUI (Qt support). Adding Qt dependency to the core is not really an option.
  • Add translations for all languages that should be supported.
  • Constant maintenance of new/changed texts for all new versions of awlsim.

@uncle-yura
Copy link
Author

It's not as difficult as it sounds, but you're right. Here we will need to make many changes.

  • As I understand it, you do not use "ui" files, which means that it will be enough to wrap all translated texts in python files.

  • Localizations will appear over time. If localization is unavailable, the default is English.

  • I did translations in the program "Poedit". It is quite convenient to add and edit texts.

I will try to make edits, and if it works out, I’ll throw you the code.

@uncle-yura
Copy link
Author

uncle-yura commented Mar 3, 2020

I tried to add a translation. To do this, you need to make some edits.

Add to each file in which you want to translate:
from awlsim.common.locale import _

Create file awlsim/common/locale.py:

# -*- coding: utf-8 -*-

import locale 
import gettext
import os
import sys

BASE = os.path.abspath( os.path.dirname( sys.argv[0] ) )
LOCALEDIR = os.path.join( BASE, "awlsim","locales" )

class translate():
	def __init__(self):
		_locale, _encoding = locale.getdefaultlocale()

		try:
			lang = gettext.translation("base", LOCALEDIR, [_locale])
			lang.install()
			self._ = lang.gettext

		except Exception as e:
			print(str(e))
			self._ = lambda s: s

	def __call__(self,source):
		return self._(source)

_ = translate()

Add underscore in each place of translation like this:
printInfo(_("Using %s GUI framework") % getGuiFrameworkName())
or
menu = QMenu(_("&Edit"), self)

Also in Poedit do the import of Python files and put the result of the translation into awlsim/locales/ru/LC_MESSAGES/base.mo

And we get this:
3
2
1

@mbuesch
Copy link
Owner

mbuesch commented Mar 3, 2020

Ok, yes.
Thanks for the examples.

There are a couple of things that need clarification, though:

  • How does the application find its .po files? What is the standard location? /usr/share?. It should also work, if awlsim is installed to a non-standard location. And it should work if it's not installed at all. Awlsim can run in-place. And on Windows (in-place and frozen).

  • Doing string expansion after translation is dangerous and hard to maintain:

printInfo(_("Using %s GUI framework") % getGuiFrameworkName())

That can lead to crashes, if it translates do %d.
Also it's hard to maintain, if I want to change it to %d. Changing all translations is not an option.

It should work independently of the string expansion being used. Say printf-style, format-style of f-string-style.
It must be guaranteed that wrong translations don't lead to crashes.

  • I am not going to start this development by myself. But I will take pull requests that properly implement this.

@uncle-yura
Copy link
Author

uncle-yura commented Mar 3, 2020

The application searches for files on the specified path:

BASE = os.path.abspath( os.path.dirname( sys.argv[0] ) )
LOCALEDIR = os.path.join( BASE, "awlsim","locales" )

This is just an example, this code also works:
printInfo(_("Using %s GUI framework" % getGuiFrameworkName()))
My mistake, but poedit display an error if the variables do not match in the translation.
I think in order to avoid possible errors and leave compatibility with old versions of python it is better to use str.format().

I have not worked with github before, I’ll try not to mess too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants