Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

How to add translations to other controllers in core branch #65

Closed c0y0tex closed 6 years ago

c0y0tex commented 7 years ago

Hi, first of all thanks for your work and effort on this awesome addon, I've been trying the asp.net core branch in core 2.0 and it seams to be working fine.

I have one question, since documentation isn't available yet can you please elaborate a little on how should I go about adding other controllers apart from Home, for example translating the Account controller for Identity.

I have tried various variations without success, I think the closest I've gotten is using: [Route("[controller]/[action]")] for the controller and [Route("[controller]/Login")] for the Login action for example

And adding the following inside the AddRouteLocalization call right after the home controller configuration:

setup.UseCulture("es")
.WhereController(nameof(AccountController))
.TranslateController("Cuenta/[action]")
.WhereAction(nameof(AccountController.Login))
.TranslateAction("[controller]/Iniciar");

The problem with this approach is that on startup the program throws the error: InvalidOperationException: Any of ActionModels is completely translated. RouteLocalization.AspNetCore.ModelExtensions.ThrowIfAnyCompletelyTranslated(ICollection localizedActionModels, ILocalizer localizer)

I would really appreciate any light you can cast over my problem.

Thanks in advanced

Dresel commented 7 years ago

Can you show the complete route configuration?

This exception occurs if you try to translate something that is already translated. Could it be that you call

setup.UseCultures(new[] { "es", "de" })
    .WhereUntranslated()
    .AddDefaultTranslation();

or similiar before?

c0y0tex commented 7 years ago

Sure, here it goes:

.AddTypedRouting()
.AddViewLocalization()
.AddDataAnnotationsLocalization(opts =>
{
          opts.DataAnnotationLocalizerProvider = (type, factory) =>
          factory.Create(typeof(SharedResource));
})
.AddRouteLocalization(setup =>
                {
                    setup.UseCulture("es")
                        .WhereController(nameof(HomeController))
                        .WhereAction(nameof(HomeController.Index))
                        .TranslateAction("Bienvenido")
                        .WhereAction(nameof(HomeController.About))
                        .TranslateAction("Acerca")
                        .WhereAction(nameof(HomeController.Contact))
                        .TranslateAction("Contacto")
                        .WhereAction(nameof(HomeController.Error))
                        .TranslateAction("Error");

                    setup.UseCulture("es")
                        .WhereController(nameof(AccountController))
                        .TranslateController("Cuenta/[action]")
                        .WhereAction(nameof(AccountController.Login))
                        .TranslateAction("[controller]/Iniciar");

                    setup.UseCultures(new[] { "en", "es" })
                        .WhereUntranslated()
                        .Filter<HomeController>(controller => controller.Start())
                        .AddDefaultTranslation();

                    setup.UseCultures(new[] { "en", "es" })
                        .WhereTranslated()
                        .RemoveOriginalRoutes();
                });

I'm using English and Spanish as cultures.

I've come to realize that the problem starts showing as soon as I add any [Route] or [HttpGet] to any new controller, even if I comment out all configuration related to that controller in startup. By the way I don't think it has anything to do with the problem but I'm using core 2,

Thanks for the help and the response.

Dresel commented 7 years ago

Thanks for your feedback. I think I've found the issue, FilterRouteSelector does not work correctly, I'm going to publish a fixed package asap.

Dresel commented 6 years ago

Sorry, took me more time than expected. I have uploaded a new NuGet package, please try

Install-Package RouteLocalization.AspNetCore -Version 1.0.0-alpha2 

and let me know if it works for you.