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

Localization #292

Closed
serwizz opened this issue Mar 29, 2022 · 11 comments
Closed

Localization #292

serwizz opened this issue Mar 29, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@serwizz
Copy link
Contributor

serwizz commented Mar 29, 2022

Hi!

Thank you for your great job with xmlschema! I use xmlschema for xml validation but I need to localize error messages. Unfortunately right now it is impossible. What do you think if I will add gettext support? I can make some pull requests with base localization code, e.g. python gettext initialization and wrap error messages.

Something like this:

init.py

folder = pathlib.Path(__file__).parent.resolve()
translation = gettext.translation(
    'xmlschema',
    pathlib.Path.joinpath(folder, 'locale'),
    fallback=True,
)
translation.install()

validators/exceptions.py

reason = _("Unexpected child with tag %r at position %d.") % (child_tag, index + 1)
...
reason += _(" Tag %r expected.") % expected_tags[0]

Of course existing English messages will remain the same.

Thanks.

@brunato
Copy link
Member

brunato commented Apr 1, 2022

Hi,

this can be very useful, maybe gettext/ngettext could be needed in other library modules (not only in validators/exceptions.py) where error messages are defined.

The init.py part should be left at the application level I suppose. Isn't it?

Thanks

@brunato brunato added the enhancement New feature or request label Apr 5, 2022
@serwizz
Copy link
Contributor Author

serwizz commented Apr 6, 2022

Hi,
I'm added some code in first draft by #293
Just for example and for further discussion.

Thanks

@brunato
Copy link
Member

brunato commented Apr 8, 2022

Hi,
i added some comments on your PR.

I don't want to install translation into package __init__.py because someone might not want to activate translations at all or might want to use a custom translation. I propose to add an utility function for activating translations (into helpers module or into a new module).

Thanks

@serwizz
Copy link
Contributor Author

serwizz commented Apr 8, 2022

Hi,
I agree it is not the best solution. I will make some wrapper for translation function or so.

Thanks

@brunato
Copy link
Member

brunato commented May 14, 2022

Hi,
in the version v1.11.0 I have included translations for parsing/validation error messages.
For activating the translation you can use:

import xmlschema
xmlschema.translation.activate()   # or xmlschema.translation.activate(languages=['ru'])

Translation can be also deactivated:

xmlschema.translation.deactivate()

Translations are not installed in builtins for default (provide activate(install=True)).

At schema level I've removed the proposed options because currently are not manageable at instance level with coherence.

I have built a new xmlschema.POT template, that can be considered completed (I don't want to extend it to error
messages not related with validation/parsing).

The version v1.11.0 includes a full Italian translation and a partial Russian translation derived from your PR. Complete
with a full Russian translation with another PR and then i will publish a new release.

@brunato
Copy link
Member

brunato commented Jun 6, 2022

Hi @serwizz,
any progress for including a full Russian translation based on v1.11.0 release?

I will publish soon a new release with fixes, and I'm uncertain that keeping heavily incomplete translations in the published package is a good thing (in any case one can create and use it's own custom translation locally for his installations).

Thank you

@serwizz
Copy link
Contributor Author

serwizz commented Jun 7, 2022

Hi, I'm working on it, I'll try to finish it in the next few days. Sorry for delay.

@serwizz
Copy link
Contributor Author

serwizz commented Jun 10, 2022

Hi,
I create PR with russian locale #303

@brunato
Copy link
Member

brunato commented Jun 11, 2022

I have merged the PR #303 but I report you that one message has not been translated:

#: xmlschema/validators/elements.py:365
#, python-format
msgid "head element %r can't be substituted by an element that has a derivation of its type"
msgstr "head element %r can't be substituted by an element that has a derivation of its type"

If you post me a translation for this i will modify for you, otherwise you will make another PR in the future.

Thank you

@serwizz
Copy link
Contributor Author

serwizz commented Jun 11, 2022

Hi,
I fixed the untranslated message and updated russian .po file from the .pot file in #304.

Thank you

@brunato
Copy link
Member

brunato commented Jun 11, 2022

Russian translation included in release v1.11.2.

Best

@brunato brunato closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants