andrew000 / FTL-Extract

FTL-Extract is a Python package that extracts Fluent keys from .py files and generates a .ftl file with extracted keys
MIT License
6 stars 0 forks source link

Using Enum in keys instead of hardcoding paths #9

Open lowton5 opened 1 week ago

lowton5 commented 1 week ago

Desc

I suppose that enums are not interpreted in keys, so they require manually entering paths, which is not very productive. It would be ideal to have a template that can be easily accessed using a class

🤏🏻 i18n.nahui.russia(_path="main.ftl")
👍🏻 i18n.nahui.russia(_path=FTLFiles.MAIN)
andrew000 commented 4 days ago

I don't think it's possible, because ast module does not execute py code, so it can't get Enum values from enums.

As you can see on screenshot - ast only sees code definition, not what's inside Enum.value. I think there should be a way to:

If choosing second option - it requires to recognize enums in the code and be sure this enum is "special".

I will be glad to hear possible ways (or even code) to solve this.


from enum import Enum

class FTLFiles(Enum):
    MAIN = "MAIN_FIELD"
from enum import Enum
from en import FTLFiles

class I18nContext:
    def get(self, key: str | Enum, **kwargs) -> str: ...

def test(i18n: I18nContext) -> None:
    i18n.get("some-key", _path=FTLFiles.MAIN)

image