mattbrailsford / umbraco-headrest

A REST based headless approach for Umbraco
MIT License
83 stars 19 forks source link

V8 multilingual setup #13

Closed tyradeveloper closed 5 years ago

tyradeveloper commented 5 years ago

I am trying to map a multilingual project with headrest where hostnames are e.g

English: www.mywebsite.com Danish: www.mywebsite.com/da

English is set as the default language in umbraco. For english i am doing the following and its working fine

_headRest.ConfigureEndpoint(new HeadRestOptions { CustomRouteMappings = new HeadRestRouteMap() .For("^/(?<altRoute>home)/?$").MapTo("/") .For("^/(?<altRoute>page2)/?$").MapTo("/page1") .For("^/(?<altRoute>page2)/?$").MapTo("/page2") , ViewModelMappings = new HeadRestViewModelMap() .For(Home.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "home") .MapTo<HomePageViewModel>() .For(Page2.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "page1") .MapTo<Page2ViewModel>() .For(Page2.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "page2") .MapTo<Page2ViewModel>() .For(Home.ModelTypeAlias).MapTo<HomePageViewModel>() .ForEverythingElse().MapTo<BaseViewModel>() }); }

I want to map www.mywebsite.com/da/home, www.mywebsite.com/da/page1, www.mywebsite.com/da/page2

I am trying but i cannot find any way of mapping the above mentioned urls. Any ideas on how i can acheive that?

tyradeveloper commented 5 years ago

I figured it out and here is the solution. Perhaps it could help others in future.

_headRest.ConfigureEndpoint(new HeadRestOptions { CustomRouteMappings = new HeadRestRouteMap() /* english*/ .For("^/(?<altRoute>home)/?$").MapTo("/") .For("^/(?<altRoute>page2)/?$").MapTo("/page2") /* danish*/ .For("^/(?<altRoute>da/home)/?$").MapTo("/") .For("^/(?<altRoute>da/page2)/?$").MapTo("/page2"), ViewModelMappings = new HeadRestViewModelMap() /* english*/ .For(Home.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "home") .MapTo<HomePageViewModel>() .For(SectionPage.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "page2") .MapTo<SectionPageViewModel>() /*danish*/ .For(Home.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "da/home") .MapTo<HomePageViewModel>() .For(SectionPage.ModelTypeAlias) .If(x => x.Request.HeadRestRouteParam("altRoute") == "da/page2") .MapTo<SectionPageViewModel>() .ForEverythingElse().MapTo<BaseViewModel>() }); }

jessevdp commented 4 years ago

Here's that same code but formatted 😇

_headRest.ConfigureEndpoint(new HeadRestOptions {
    CustomRouteMappings = new HeadRestRouteMap()
        /* english*/
        .For("^/(?<altRoute>home)/?$").MapTo("/").For("^/(?<altRoute>page2)/?$").MapTo("/page2")
        /* danish*/
        .For("^/(?<altRoute>da/home)/?$").MapTo("/").For("^/(?<altRoute>da/page2)/?$").MapTo("/page2"),
    ViewModelMappings = new HeadRestViewModelMap()
        /* english*/
        .For(Home.ModelTypeAlias).If(x = >x.Request.HeadRestRouteParam("altRoute") == "home").MapTo < HomePageViewModel > ()
            .For(SectionPage.ModelTypeAlias).If(x = >x.Request.HeadRestRouteParam("altRoute") == "page2").MapTo < SectionPageViewModel > ()
        /*danish*/
        .For(Home.ModelTypeAlias).If(x = >x.Request.HeadRestRouteParam("altRoute") == "da/home").MapTo < HomePageViewModel > ()
            .For(SectionPage.ModelTypeAlias).If(x = >x.Request.HeadRestRouteParam("altRoute") == "da/page2").MapTo < SectionPageViewModel > ()
            .ForEverythingElse().MapTo < BaseViewModel > ()
});

I don't entirely see how this relates to multilingual content though? Aside from mapping routes prepended by /da (language prefix) to where the route where the content item should exist (in a very verbose way), this code doesn't show anything related to multilingual content.

One missing piece at least is how you resolve the correct language variant for an item. (Based on the route mapping.)