Giter Site home page Giter Site logo

Comments (15)

kbairak avatar kbairak commented on August 21, 2024 1

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.

kbairak avatar kbairak commented on August 21, 2024 1

@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.

kbairak avatar kbairak commented on August 21, 2024 1

@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.

kbairak avatar kbairak commented on August 21, 2024 1

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.

kbairak avatar kbairak commented on August 21, 2024 1

@haroal The latest release (3.0.2) includes this fix. Thank you for you patience and help.

from transifex-python.

haroal avatar haroal commented on August 21, 2024

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.

kbairak avatar kbairak commented on August 21, 2024

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:

  1. What version of Python and Django are you using?

  2. 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.

haroal avatar haroal commented on August 21, 2024

Hi @kbairak,

Thanks for you answer.

  1. 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

  1. 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.

haroal avatar haroal commented on August 21, 2024

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.

kbairak avatar kbairak commented on August 21, 2024

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.

haroal avatar haroal commented on August 21, 2024

@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.

kbairak avatar kbairak commented on August 21, 2024

Yep, as I feared. This is what the migration file looks like with my initial __repr__ implementation:

image

Without __repr__, it falls back to __str__ and the migration file looks like this:

image

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.

haroal avatar haroal commented on August 21, 2024

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.

haroal avatar haroal commented on August 21, 2024

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.

haroal avatar haroal commented on August 21, 2024

HI @kbairak,

Thank you for all!
Could you give me an estimated time of release, please?

from transifex-python.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.