Get resolved "url" in alternate_languages

Hello,

I currently have a blog setup with this custom route: path:

"/:lang/magazin/:category/:uid"
(a fetch with 2 levels deep)

and so I am currently fetching all my documents with get Static Paths in such a format: [category]/[uid] that corresponds to what my Router above is resolving.

I am working with multiple languages, and so for each document (:uid) I also need the associated translated category (:category), for the language switcher, unfortunately the only answer I get from the alternate_languages array is something like this

{
    "id": "YOMt4hIAACUAVGuD",
    "uid": "5-questions-to-ask-yourself-before-starting-a-newsletter",
    "type": "magazin_single_post",
    "lang": "de-de"
}

Any tips or ideas?

Best regards

Hi David,

I'll be happy to help you with this.

If you look at the language switcher for our Next.js multi lang example, you simply pass the alternate languages through the link resolver which will fall back on the route resolver. This will treat the alternate language doc as normal document and get the resolved URL as normal.

Let me know if this make sense.

Thanks.

1 Like

Hi,

Thanks for your answer but I think I wasn't clear enough in my initial post.

So imagine the following:

I have a custom Type called "blog post"
This blog post type has a Document Link (Content Relationship) to a secondary custom type called "blog category" that is going to be used for nesting my blog posts in categories in my urls (so category-uid/blog-post-uid)

At the moment, It seems that there is no clear way to retrieve this relationship for alternative languages, let's say we take the example from above we suppose we fetch the "blog post" custom type document, we will get its UID, but also it's category in the french language so we will get something like this

(i've stripped all unecessary things to make the example clearer)

{
  "doc": {
    "id": "YN8NohIAACIAQjsp",
    "uid": "my-custom-blog-post",
    "url": "/blog/category-of-the-blog/my-custom-blog-post",
    "type": "blog_post",

    "lang": "fr-fr",
    "alternate_languages": [
      {
        "id": "YOXJERIAACQAX9Tv",
        "uid": "the-post-in-de-ch",
        "type": "blog_post",
        "lang": "de-ch"
      }
    ],
    "data": {
        "category": {
            "uid": "category-of-the-blog",
            "type": "blog_category"

        }
    }
  }
}

So in this example we have a blog post with the uid "my-custom-blog-post" that has the Content Relationship "category" to the custom type -> "blog_category"

And so through the help of the Route Resolver (from slicemachine I may have forgotten to mention that) I'm creating a nested route of a form of :lang/blog/:category/:uid which then creates the "url" response in the example above to make routing faster.

But I don't see (an easy) way to do the same thing for alternate languages, as the uid of the translated category will be different (translated) and so it's not possible without refetching each alternative languages document and get its Content Relationship -> Category -> UID

So for the alternate languages document I am expecting something like: /de-ch/blog/category-of-the-blog-in-de-ch/the-post-in-de-ch

Ideally it would be nice the route resolver from slicemachine -> adds the resolved translated URL to the alternate_languages array, unless there is another way to do so.

Let me know if this explanation was clearer :laughing:

OK, then it's as I understood and the solution rests the same, the link resolver will fall back on the route resolver if you haven't included this type in the link resolver. It's then smart enough to find that link already built for you.

All you have to do is pass the alternate_languages array to the component I showed you above, then loop through each object and pass the alt lang object...

{
  "id": "YOXJERIAACQAX9Tv",
  "uid": "the-post-in-de-ch",
  "type": "blog_post",
  "lang": "de-ch"
}

... in to the Link Resolver function href={hrefResolver(altLang)} and like I said it will add the correct route with all the correct categories by language etc.

1 Like

Works great, thanks !

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.