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

Localizable presenter #513

Merged
merged 12 commits into from
Jan 23, 2018
Merged

Localizable presenter #513

merged 12 commits into from
Jan 23, 2018

Conversation

exyi
Copy link
Member

@exyi exyi commented Dec 8, 2017

Added LocalizablePresenter class - a utility for localizing specific route based on route parameter or query string

public static Func<LocalizablePresenter> BasedOnParameter(string name)
{
var presenter = new LocalizablePresenter(
context => context.Parameters.TryGetValue(name, out var value) && !string.IsNullOrEmpty(value as string) ? new CultureInfo((string)value) : null,
Copy link
Contributor

Choose a reason for hiding this comment

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

if value is not a valid culture name the statement new CultureInfo((string)value) will throw CultureNotFoundException. We should put the statement into a try block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I don't think so, silently eating the error may be pretty nasty. I think that displaying Error 500 when you access non-existent culture is not that bad option. Unfortunately, in the presenter it's too late for a "not found" response, you can only do that in a middleware, but you could catch the CultureNotFound exception and rewrite it to 404 response elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, silently eating the error is pretty nasty, but IMHO 500 Internal Server Error is worse. Error 500 should indicate that something is bad with your code. We can add culture constraint but that doesn't solve the problem. Returning 404 would be nice, but IMHO we shouldn't require for user to implement this behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe I could do a redirect to a default culture. What would you think about that?

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds good.

…nvalid culture is specified

I also has to fix a bug DotvvmQueryCollection and add a missing Add(virtualPath, presenter) overload to the RouteTable.
/// A DotVVM Presenter that reads culture by <see cref="getCulture" />,
/// sets the thread culture and invokes the default dotvvm presenter (obtained from IServiceProvider)
/// </summary>
public class LocalizablePresenter : IDotvvmPresenter
Copy link
Member Author

Choose a reason for hiding this comment

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

To be honest, I don't like that at all. I don't want to mess the codebase with VisualStudio hacks. The summary element is simply totally ugly and it's not required.

  • By specification - the specification actually does not force any rules here, the comment just has to be valid XML. There are just recommended tags. (that also means that VS behavior is correct, but in the same way notepad is valid C# IDE)
  • By all sane tools I know - Omnisharp and JetBrains rider work.
    image
    image
    And if it does not work with any other sane IDE, it can't be a problem to write a plugin or fork and compile :P

@exyi exyi merged commit 4d3a4b3 into master Jan 23, 2018
@tomasherceg
Copy link
Member

Is there any way of telling DotVVM to use this presenter on all routes by default?
If not, we should add this - maybe something like config.Routes.DefaultPresenterFactory = ....

@exyi
Copy link
Member Author

exyi commented Jan 23, 2018 via email

@tomasherceg
Copy link
Member

Is there any type you can register for the default presenter? I don't think services.AddTransient<DefaultDotvvmPresenter, LocalizablePresenter>() will work.
And registering it for just IDotvvmPresenter is not wise because of custom presenters.

@exyi
Copy link
Member Author

exyi commented Jan 23, 2018

Is there any type you can register for the default presenter? I don't think services.AddTransient<DefaultDotvvmPresenter, LocalizablePresenter>() will work.

IDotvvmPresenter

And registering it for just IDotvvmPresenter is not wise because of custom presenters.

I don't understand what is the problem with it. This way you can register your custom presenter as a default one, or you can use it just locally, I don't see any problem.

@exyi exyi deleted the localizable-presenter branch January 23, 2018 11:11
@tomasherceg
Copy link
Member

Oh, I understand - custom presenters can be registered as specific types and the default one will be registered as IDotvvmPresenter. OK, we can use it like this, but we should document this behavior.

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

Successfully merging this pull request may close these issues.

None yet

3 participants