Comments (15)
Thanks @haroal for your assistance.
This was bugging me so, during the weekend, I came up with an alternative and, I think, more complete version of lazy strings.
https://gist.github.com/kbairak/ee2b7d240e1784dc2c4a3463416e5497
I will give it a try with transifex-python and django's models.TextChoices
to see if it works. If it doesn't, we can fallback to your implementation.
from transifex-python.
@haroal I think it's not that simple with repr. Somehow repr's return value gets inserted into the generated migration files. I will look into it and get back to you, but my initial thought is that we may have to enhance the lazy strings with fallback values somehow. I'll let you know what I find.
from transifex-python.
@haroal That's almost exactly what I ended up doing (the only difference being is that the fallback value is optional). I will get an approval from my team and merge this.
Again, thanks so much for your help and feeback.
from transifex-python.
Hello @haroal,
The branch was just merged. Sorry this took longer than expected but the code does tricky things and required some thorough testing for us to be as confident as possible that it wouldn't introduce any bugs.
Again, thank you for bringing this issue to our attention.
from transifex-python.
@haroal The latest release (3.0.2) includes this fix. Thank you for you patience and help.
from transifex-python.
As a first hint, it looks like making LazyString
inherit from str
solves it. A bit hacky though, and I'm still curious to know if there is a known solution as using these models.TextChoices
is quite basic in Django :)
Thanks!
from transifex-python.
Hey @haroal,
Thank you for reporting this issue. I will admit that the lazy aspect of django translations is tricky and we will look into it.
I have two questions that can help us figure this out sooner:
-
What version of Python and Django are you using?
-
Will your project work if, insead of a
models.TextChoices
class, you use a sequence of 2-tuples?That is, if instead of writing:
from django.db import models from transifex.native.django import lazyt as _ class PossibleValues(models.TextChoices): VALUE_A = 'value_a', _('Value A') VALUE_B = 'value_b', _('Value B') VALUE_C = 'value_c', _('Value C') class MyModel(models.Model): field = TextField(choices=PossibleValues.choices, default=PossibleValues.VALUE_A)
You write:
from django.db import models from transifex.native.django import lazyt as _ FieldChoices = ( ('value_a', _('Value A')), ('value_b', _('Value B')), ('value_c', _('Value C')), ) class MyModel(models.Model): field = TextField(choices=FieldChoices, default='value_a')
Will it work?
This is not to say that since there is a working alternative, you should use it and we will not bother making
TextField
work, but it will help us understand the situation better.
from transifex-python.
Hi @kbairak,
Thanks for you answer.
- I gave my versions in the issue description:
Python 3.8
Django 3.2
transifex-python 3.0.1
Linux 5.14 x86_64 / Ubuntu 20.04
- Indeed using a sequence of 2-tuples works. However, I still need to use
models.TextChoices
for my project because I have methods/properties bound to this enum... :/ And I can't do the same with a sequence of 2-tuples. I mean, I can refactor my code to make it work, but it would be dirtier so it can just unblock me temporarily. Thanks for the suggestion though!
from transifex-python.
Hi @kbairak,
I don't if it would help you, but just to let you know, I forked this project and fixed this with this commit: haroal@03fe7a5
If it's OK, I can even create a PR to propose these changes into your code base.
from transifex-python.
Hey @haroal,
I packaged it into a pull request (#80 ) and tested it on a sample django project. Would you like to give it a try on your end before we move forward on our end?
Thanks!
from transifex-python.
@kbairak It works! 🎉
Just, I think you should override __repr__
too because, else, LazyString
instances are always represented by an empty string (''
) and it's quite confusing when using it in console.
Many thanks! Just waiting for this to be merged now.
from transifex-python.
Yep, as I feared. This is what the migration file looks like with my initial __repr__
implementation:
Without __repr__
, it falls back to __str__
and the migration file looks like this:
I find it risky that Django dumps the output of __repr__
into a python file and expects it to work, but what can you do?
We could go with the second solution since choices validation are in the migration file in case the database can enforce the validation, but most databases don't, but I don't like this. I would like the migration files to have the source version of the strings. Trying to figure out an elegant way to achieve this.
from transifex-python.
Indeed. It looks like your __repr__
method should return the source string to have proper migration files, so perhaps it could be great to change LazyString
signature to something like def __init__(func, source_str, *args, **kwargs):
and return source_str
value when calling __repr__
. Just a suggestion 😊
from transifex-python.
HI @kbairak,
Do you have any news about merging your new LazyString implementation? I still need it to translate my enums :)
Thanks!
from transifex-python.
HI @kbairak,
Thank you for all!
Could you give me an estimated time of release, please?
from transifex-python.
Related Issues (15)
- Python 3.12 support HOT 5
- CDS 404 Client Error HOT 1
- Error when translation None is django template
- Not working with django model field verbose_name=lazyt("Title") HOT 4
- Transifex native not working in combination with Django 3.x due to deprecated access HOT 1
- transifex_api.ResourceTranslationsAsyncDownload.download fails with missing attributes error HOT 3
- JsonApiException: (400, ["<JsonApiError: invalid - 'attributes' is a required property>"]) HOT 10
- Unable to pull from today HOT 2
- Support custom CDS handlers HOT 1
- Can't disable HTML escape when using lazy translation HOT 2
- Tests is installed as package HOT 2
- Is the OpenAPI spec available over HTTPS?
- APIv3: Bulk actions for /resource_translations_async_downloads GET and POST?
- Transifex doesn't work with Django 4.0 or later. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transifex-python.