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

Add Spanish translation for messages #11

Closed
bartoszlenar opened this issue Feb 24, 2021 · 4 comments · Fixed by #18
Closed

Add Spanish translation for messages #11

bartoszlenar opened this issue Feb 24, 2021 · 4 comments · Fixed by #18
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@bartoszlenar
Copy link
Owner

Feature description

  • Spanish translation for the default Validot's message set.
  • Placeholders support where applicable.
  • Fluent api extension method to ValidatorSettings so it can be nicely used from Validator.Factory.Create.

Feature in action

var validator = Validator.Factory.Create(
    specification,
    s => s.WithSpanishTranslation()
);

validator.Settings.Translations["Spanish"] // this is the full translation dictionary

validator.Validate(model).ToString(translationName: "Spanish") // gets result error output in Spanish

Feature implementation walkthrough

  • In Validot, even English language is implemented as a translation that maps the property names from MessageKey class to the human-readable phrases.
  • Take a look at the files in src/Validot/Translations directory. This is the starting point, where the translation should be placed. Create Spanish directory inside and then SpanishTranslation.cs.
  • The content of SpanishTranslation.cs could be (and should be...) entirely based on the file EnglishTranslation.cs. Please copy-paste the content and replace English phrases with the Spanish ones.
    • Please be aware that the particular phrase could contain a placeholder. If you see the English text like "Must be equal to {value}", don't translate the text between curly brackets { and }. The Polish version for this is "Musi być równe {value}". As you can see, {value} stays as it was.
    • As a matter of fact, it would be nice if you add formatting to your phrases. Placeholders will be filled with the values coming from the rule arguments and you can adjust the culture like this: "Musi być równe {value|culture=pl-PL}". So after the variable name (value) there is a pipeline character | and then parameter (culture) with the value pl-PL - culture code, the string that goes to the CultureInfo.GetCultureInfo method. To truly understand how it works, please read the section about message arguments in the docs
  • Similarly, next to SpanishTranslation.cs, please create SpanishTranslationExtensions.cs and copy-paste the content from the EnglishTranslationExtensions.cs. Just find-and-replace English with Spanish should work just fine.
  • Creating unit tests are as simple as creating extensions from the point above. Enter TranslationTests.cs file and look for nested class English - copy-paste it and replace English with Spanish. You can notice that Polish is done in a similar way.
  • Done! Thank you for your contribution!
@restebanlm
Copy link
Contributor

Hi @bartoszlenar . I'm going to work on the adaptation to Spanish.

@bartoszlenar
Copy link
Owner Author

Thank you, @restebanlm. If anything in the issue description is not clear enough, or if you need any additional help or technical guidance, let me know in the comments.

@restebanlm
Copy link
Contributor

I hope the pull I sent you is useful.

@bartoszlenar
Copy link
Owner Author

It's great. Thank you.

Already merged in #18

@bartoszlenar bartoszlenar linked a pull request Feb 28, 2021 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants