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

Default culture in routes? #68

Closed mdmoura closed 7 years ago

mdmoura commented 7 years ago

I am using the following configuration:

  services
    .AddMvc(x => { x.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline))); })
    .AddTypedRouting()
.AddRouteLocalization(x => {

      x.UseCulture("pt")
        .WhereController(nameof(HomeController))
        .WhereAction(nameof(HomeController.Index))
        .TranslateAction("");       

    x.UseCulture("pt")
        .WhereController(nameof(AboutController))
        .WhereAction(nameof(AboutController.Index))
        .TranslateAction("quem-somos"); 

    })          
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();    

    services.AddSingleton(x => {
        RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions() {        
           DefaultRequestCulture = new RequestCulture("en"),
           SupportedCultures = new[] { new CultureInfo("en"), new CultureInfo("pt") },
           SupportedUICultures = new[] { new CultureInfo("en"), new CultureInfo("pt") },
        };
        requestLocalizationOptions.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
        return requestLocalizationOptions;
  });

}

So I get the following Routes:

Home
  EN: /
  PT: /pt

About
  EN: /about
  PT: /pt/quem-somos

Should the website also work with:

Home
  EN: /en

About
  EN: /en/about

English version would have /en in the route ... The default version with no culture in the route would also be english since is the default one.

What is the best approach for this?

Dresel commented 7 years ago

What about WhereUntranslated and AddDefaultTranslation?

.AddRouteLocalization(x =>
{
    x.UseCulture("en")
        .WhereUntranslated()
        .AddDefaultTranslation();

    x.UseCulture("pt")
        .WhereController(nameof(HomeController))
        .WhereAction(nameof(HomeController.Index))
        .TranslateAction("");

    x.UseCulture("pt")
        .WhereController(nameof(AboutController))
        .WhereAction(nameof(AboutController.Index))
        .TranslateAction("quem-somos");
})

Does this satisfy your needs?

mdmoura commented 7 years ago

Yes, it does. Thank you.