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

fix: General fixing and clean-up on translate() #1364

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

phorward
Copy link
Member

@phorward phorward commented Jan 9, 2025

This pull request summarizes several problems:

  • Turn tr_key into name
  • Make name part of the db.Key for faster retrieval
  • Let all ViUR-core related translations have a viur.core-name-prefix
  • Make name (tr_key) required on creation (otherwise, it is kept readonly)
  • Provide a way for conf.i18n.add_missing_translations to only add or ignore a specific pattern (fnmatch)
  • Provide translation.dump() function for JSON-based retrieval of both public and private translations and to handle multiple patterns
    • Provide a canView-like hook (conf.i18n.dump_can_view)
  • SelectBone with auto-create-translation-flag
  • Implement default_variables Implement default_variables in i18n.translate #1379
  • In translate-Module a standard view for System-translations + Public translations
  • Don't add translation for error messages
  • Don't do a str-cast on an existing translate to use it as key for the new object

This pull request summarizes several problems:

1. Let all ViUR-core related translations have a `viur.core`-tr_key-prefix
2. Make `tr_key` required
3. Provide a way for `conf.i18n.add_missing_translations` to only add or ingore a specific pattern (fnmatch)
@phorward phorward added bug(fix) Something isn't working or address a specific issue or vulnerability annoying labels Jan 9, 2025
@phorward phorward added this to the ViUR-core v3.7 milestone Jan 9, 2025
- SelectBone now allowes for a separate `add_missing_translations`-parameter
- `conf.i18n.add_missing_translations` can now be also an iterable of fnmatch-patterns
- `translate`-class allowes for individual `add_missing`-flag
@phorward phorward added the Priority: High After critical issues are fixed, these should be dealt with before any further issues. label Jan 23, 2025
)
if self.key not in systemTranslations:
# either the translate()-object has add_missing set
if not (add_missing := self.add_missing):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this behaviour confusing.

  • So I set add_missing set to False, but if conf.i18n.add_missing_translations is True, it is added anyway.
    If add_missing is set to True, it doesn't matter what is in conf.i18n.add_missing_translations and it's added too.

I would see this as a common condition:
If self.add_missing and conf.i18n.add_missing_translations are both True, add, otherwise not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sveneberth, sorry for the confusion. The initial consideration about this flag was, that either the translate-object has the add_missing-flag on first order, and when this is not True, the globally defined logic of conf.i18n.add_missing_translations takes place, which can either be just True, but also one or many fnmatch-style pattern to match the given translation key.

Copy link
Member

@sveneberth sveneberth Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we have different use cases for this and other expections (you want an or logic, I an and). Let's talk about this in a meet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed yesterday, I have implemented Always and Never in 58d2e30.

@sveneberth sveneberth linked an issue Jan 25, 2025 that may be closed by this pull request
@phorward phorward added Priority: Medium This issue may be useful, and needs some attention. and removed Priority: High After critical issues are fixed, these should be dealt with before any further issues. labels Feb 5, 2025
@phorward phorward removed the Priority: Medium This issue may be useful, and needs some attention. label Feb 17, 2025
@phorward phorward added proposal Priority: High After critical issues are fixed, these should be dealt with before any further issues. refactoring Pull requests that refactor code but do not change its behavior. labels Feb 17, 2025
@phorward phorward requested a review from sveneberth February 17, 2025 20:28
@phorward phorward marked this pull request as ready for review February 17, 2025 20:28
@phorward
Copy link
Member Author

phorward commented Feb 17, 2025

@sveneberth I'm done with my requests.

Can you please add changes for your requests:

  • Don't add translation for error messages --> 6ecdfa8
  • Don't do a str-cast on an existing translate to use it as key for the new object --> 5fc22d9

This issue is urgent, waiting for approval.

sveneberth and others added 3 commits February 22, 2025 01:47
For non-static errors with variables like
```py
raise errors.NotFound(f"No valid root node found for {node=}")
```
a new `TranslationSkel` for each `node`-key would be added.
@phorward
Copy link
Member Author

@sveneberth thanks, this looks fine so far. Can you please approve the PR, I cannot self-approve.


# Create the key from the name on initial write!
if not skel["key"]:
skel["key"] = db.Key(KINDNAME, skel["name"].lower())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would overwrite data.

If there's already a complete skeleton with the key "foo", and someone clicks on add and use the same key, it would overwrite the other skel without any warning etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't tested this, and this is kinda hakkish, but this might solve this issue:

    @classmethod
    def write(cls, skel, **kwargs):

        def write_with_claimed_key():
            skel["key"] = db.Key(KINDNAME, skel["name"].lower())
            if db.Get(skel["key"]):
                raise ValueError(f'An TranslationSkel with {skel["name"]=} exists already!')
            return super().write(skel, **kwargs)

        # Create the key from the name on initial write!
        if not skel["key"]:
            if db.IsInTransaction():
                return write_with_claimed_key()
            else:
                return db.RunInTransaction(write_with_claimed_key)

        return super().write(skel, **kwargs)

@@ -799,7 +799,7 @@ def start(self):
return self._user_module.render.edit(
self.OtpSkel(),
params={
"name": i18n.translate(self.NAME),
"name": i18n.translate("viur.core.modules.user." + self.NAME),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"name": i18n.translate("viur.core.modules.user." + self.NAME),
"name": i18n.translate(f"viur.core.modules.user.{self.NAME}"),

and for others please use f-string too

)
if self.key not in systemTranslations:
# either the translate()-object has add_missing set
if not (add_missing := self.add_missing):
Copy link
Member

@sveneberth sveneberth Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we have different use cases for this and other expections (you want an or logic, I an and). Let's talk about this in a meet.

@phorward phorward added the viur-meeting Issues to discuss in the next ViUR meeting label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
annoying bug(fix) Something isn't working or address a specific issue or vulnerability Priority: High After critical issues are fixed, these should be dealt with before any further issues. proposal refactoring Pull requests that refactor code but do not change its behavior. viur-meeting Issues to discuss in the next ViUR meeting
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Implement default_variables in i18n.translate
2 participants