-
Notifications
You must be signed in to change notification settings - Fork 45
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
placeholder_delimiter config is ignored #29
Comments
Hi @mbideau and thanks for the feedback I also updated the readme (of the same branch) to reflect how you should edit the config, as I did it a bit differently than what you suggested See you! |
Hi @ZRunner, thank you for taking the time to read and find a solution 😉 Sadly I couldn't make it work ... ❯ bin/pip install git+https://github.com/ZRunner/python-i18n.git@custom-delimiter-fix
Collecting git+https://github.com/ZRunner/python-i18n.git@custom-delimiter-fix
Cloning https://github.com/ZRunner/python-i18n.git (to revision custom-delimiter-fix) to /tmp/pip-req-build-3nkcq3ys
Running command git clone -q https://github.com/ZRunner/python-i18n.git /tmp/pip-req-build-3nkcq3ys
Running command git checkout -b custom-delimiter-fix --track origin/custom-delimiter-fix
Basculement sur la nouvelle branche 'custom-delimiter-fix'
La branche 'custom-delimiter-fix' est paramétrée pour suivre la branche distante 'custom-delimiter-fix' depuis 'origin'.
Using legacy 'setup.py install' for python-i18n, since package 'wheel' is not installed.
Installing collected packages: python-i18n
Running setup.py install for python-i18n ... done
Successfully installed python-i18n-0.3.12
❯ bin/python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import i18n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/testvenv/lib/python3.9/site-packages/i18n/__init__.py", line 3, in <module>
from .translator import t
File "/opt/testvenv/lib/python3.9/site-packages/i18n/translator.py", line 1, in <module>
from string import Template, _TemplateMetaclass
ImportError: cannot import name '_TemplateMetaclass' from 'string' (/usr/lib/python3.9/string.py) I search for the class in the Python source code, but couldn't find it either in version 3.9 nor 3.10, and the only commits mentioning it are from 2007 😅 Do you have an idea on how I can make it work ? Thank you again. |
Ok I think I found the issue and fixed it. For some reasons my computer used Python 3.8 during the tests so this specific import wasn't compatible with newer Python versions. If I'm right the issue is now fixed (in a new commit on the |
Hi @ZRunner, ❯ bin/pip install git+https://github.com/ZRunner/python-i18n.git@custom-delimiter-fix
Collecting git+https://github.com/ZRunner/python-i18n.git@custom-delimiter-fix
Cloning https://github.com/ZRunner/python-i18n.git (to revision custom-delimiter-fix) to /tmp/pip-req-build-07oht2q7
Running command git clone -q https://github.com/ZRunner/python-i18n.git /tmp/pip-req-build-07oht2q7
Running command git checkout -b custom-delimiter-fix --track origin/custom-delimiter-fix
Basculement sur la nouvelle branche 'custom-delimiter-fix'
La branche 'custom-delimiter-fix' est paramétrée pour suivre la branche distante 'custom-delimiter-fix' depuis 'origin'.
Using legacy 'setup.py install' for python-i18n, since package 'wheel' is not installed.
Installing collected packages: python-i18n
Running setup.py install for python-i18n ... done
Successfully installed python-i18n-0.3.12
❯ bin/python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import i18n
>>> i18n.set('error_on_missing_placeholder', True)
>>> import string
>>> class MyTranslationFormatter(string.Template):
... delimiter = '!'
...
>>> i18n.set('translation_formatter', MyTranslationFormatter)
>>> i18n.add_translation('hash', 'Hash: 11 !{toto}')
>>> i18n.t('hash', toto='tata')
'Hash: 11 !{toto}'
>>> i18n.set('placeholder_delimiter', '!')
>>> i18n.t('hash', toto='tata')
'Hash: 11 tata'
>>> May be I am using it wrong ? |
Woah thanks a lot, today I discovered a thing. Well anyway I tested it and my fix seems to work, at least for your above code. Don't hesitate to tell me if I forgot something, and thanks for your support! |
Hey @ZRunner now it is working. ❯ bin/python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import i18n
>>> i18n.set('error_on_missing_placeholder', True)
>>> i18n.add_translation('hash', 'Hash: 11 !{toto}')
>>> i18n.t('hash', toto='tata')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/testvenv/lib/python3.9/site-packages/i18n/translator.py", line 34, in t
return translate(key, locale=locale, **kwargs)
File "/opt/testvenv/lib/python3.9/site-packages/i18n/translator.py", line 55, in translate
TranslationFormatter: ModelTranslationFormatter = get_translation_formatter()
File "/opt/testvenv/lib/python3.9/site-packages/i18n/translator.py", line 73, in get_translation_formatter
if issubclass(custom_class, (Template, ModelTranslationFormatter)):
TypeError: issubclass() arg 1 must be a class
>>> import string
>>> class MyTranslationFormatter(string.Template):
... delimiter = '!'
...
>>> i18n.set('translation_formatter', MyTranslationFormatter)
>>> i18n.t('hash', toto='tata')
'Hash: 11 tata'
>>> Anyway, thank you very much for the fix 😉 |
Yup, I found the issue again I also took some time to fix the coverage verification CI, might make sure to add some other tests later (currently they "only" cover 94% of the code) EDIT: branch merged on master |
Hey @ZRunner, |
Hi @danhper and @ZRunner,
Thank you both (and other contributors) for this great piece of software.
I am facing an issue when attempting to change the placeholder delimiter (because I have data containing the default placeholder delimiter
%
)First I looked at the units tests, but none where testing that config feature 😅
So I looked at the source code, and see that it uses the Template feature of the string module.
And the documentation state :
Which is confirmed by the use of
__init_subclass__(cls)
in the Template source codeTo my comprehension, because your module defines the class itself, despite it contains the following code, it will always get the value of the default delimiter as the moment of its definition (never get the chance to call
i18n.set()
before definition) :So I said, OK, I am just going to extend it with another class setting up my own delimiter (like this Template example), but it is impossible since you use that class hardcoded.
In @danhper code for i18n/translator.py:
In @ZRunner code for i18n/translator.py:
So I recommend, as a solution, to allow in the config to specify a class, to be used by the translate() function.
In i18n/config.py
In @danhper code for i18n/translator.py:
And in In @ZRunner code for i18n/translator.py:
this way I would be able to use it like this :
Maybe that would be a little less efficient in terms of perfomance, but that would be way more flexible.
What do you think ?
Best regards.
The text was updated successfully, but these errors were encountered: