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

Translate form errors #10

Open
acidjunk opened this issue Jul 26, 2023 · 4 comments
Open

Translate form errors #10

acidjunk opened this issue Jul 26, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@acidjunk
Copy link
Collaborator

acidjunk commented Jul 26, 2023

I needed translated (NL/Dutch) FormValidation errors in a frontend and tried to get that working. There is a package pydantic-translations

I am not sure about the best internationalisation project for pydantic; there's also pydantic-i18n

or (https://pypi.org/project/pydantic-errors-messages-translations/)[pydantic-errors-messages-translations]

With pydantic-translations: I managed to get Dutch / NL form validations errors in the frontend, by changing some code in post_form():

# Loop through user inputs and for each input validate and update current state and validation results
while user_inputs:
    user_input = user_inputs.pop(0)
    # Validate
    try:
        form_validated_data = generated_form(**user_input)
    except ValidationError as e:
        from pydantic_translations import Translator
        tr = Translator('nl')
        errors = [tr.translate_error(error) for error in e.errors()]
        raise FormValidationError(e.model.__name__, errors) from e  # type: ignore

This works as a POC: it might be cool to add this as a feature, by adding an optional language parameter in start_form() and post_form().

Investigate:

  • is this a nice feature for this project?
  • which translation package is the best
  • what's a good format for the language parameter: "nl", "NL-nl"?
  • how can can users add custom translations for their own form fields
@Mark90 Mark90 added the enhancement New feature or request label Jul 27, 2023
@Mark90
Copy link
Contributor

Mark90 commented Jul 27, 2023

Sounds like a nice feature.

Regarding:

what's a good format for the language parameter: "nl", "NL-nl"?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language

The most common extra information is the country or region variant (like 'en-US' or 'fr-CA')

I'd vote for being thorough and using the "full" locale identifier. We've always been mixing en-US and en-GB spelling, which as you know have subtle differences. We could now actually add both languages and perhaps use some spellcheck linting to make sure we don't mess up again :)

@acidjunk
Copy link
Collaborator Author

I think the full locale is often used when you also want to set localisation stuff like date formats.
But I'm ok with the long version: it might be handy

I want to do some further investigation regarding corner cases; consider a date field; it might be nice to handle the date format in a different way for Dutch / English regions.

@acidjunk
Copy link
Collaborator Author

@Mark90 This looks promising -> https://github.com/boardpack/pydantic-i18n

That seems to have a way to acquire all pydantic2 error messages.
Will do a POC soon with it.

@Mark90
Copy link
Contributor

Mark90 commented Mar 26, 2024

@acidjunk Nice, I'm curious whether it can translate the "constant" part of the error message, so that we can generate static translation files for the pydantic-forms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants