question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support internationalized routes in API pages/find/?html_path view

See original GitHub issue

Is your proposal related to a problem?

It doesn’t appear to work to find a page using the ?html_path query param if it has a locale prefix. I found that the find_object method in the pages api view set doesn’t seem to consider the locale prefix so that’s probably the reason. We’re using Wagtail as a headless CMS and the ability to query page data using the meta.html_url we get from the /pages/-endpoint would be very useful.

Describe the solution you’d like

We’d like to be able to simply query the pages using an html path that includes the locale prefix. Eg. /pages/find/?html_path=/sv/my-page/.

I subclassed the Pages view set to implement this feature, but I think it belongs in wagtail core.

Describe alternatives you’ve considered

I also considered respecting the locale query parameter to switch root page in the find_object method. However, I think it is neater to use the locale prefix in the path, since that is what we get back from the API in meta.html_url on the Page object.

Additional context

I can make a PR with the changes if you’d like to merge this change.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mixxorzcommented, Mar 25, 2021

Gave this a go and here’s what I’ve come up with:

class I18nPagesAPIViewSet(PagesAPIViewSet):
    def find_object(self, queryset, request):
        site = Site.find_for_request(request)
        if "html_path" in request.GET and site is not None:
            path = request.GET["html_path"]
            language_code, *path_components = [
                component for component in path.split("/") if component
            ]

            if language_code not in dict(settings.LANGUAGES).keys():
                return

            try:
                page, _, _ = site.root_page.specific.route(request, path_components)
            except Http404:
                return

            if queryset.filter(id=page.id).exists():
                with translation.override(language_code):
                    return page.localized

        return super().find_object(queryset, request)

0reactions
zerolabcommented, Oct 11, 2022

Here’s an updated viewset that handles search for simple_translation/wagtail-localize which create separate trees, thus require searching in root page translation children.

class I18nPagesAPIViewSet(PagesAPIViewSet):
    def find_object(self, queryset, request):
        site = Site.find_for_request(request)
        if "html_path" in request.GET and site is not None:
            path = request.GET["html_path"]
            language_code, *path_components = [
                component for component in path.split("/") if component
            ]

            if language_code not in dict(getattr(settings, "LANGUAGES", [])).keys():
                return super().find_object(queryset, request)

            try:
                locale = Locale.objects.get(language_code=language_code)
            except Locale.DoesNotExist:
                return

            try:
                page, _, _ = site.root_page.get_translation(locale).specific.route(request, path_components)
            except Http404:
                return

            if queryset.filter(id=page.id).exists():
                with translation.override(language_code):
                    return page.localized

        return super().find_object(queryset, request)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Internationalized Routing - Next.js
Next.js has built-in support for internationalized (i18n) routing since v10.0.0 . You can provide a list of locales, the default locale, and domain-specific ......
Read more >
Rails Routing from the Outside In - Ruby on Rails Guides
How to automatically create paths and URLs using route helpers. ... To find the route helper names for your routes, see Listing existing...
Read more >
Directions Service | Maps JavaScript API - Google Developers
On this page ... Also see the Maps JavaScript API Reference: Directions ... Service which receives direction requests and returns an efficient path....
Read more >
Introduction to Next.js 10's Internationalized (i18n) Routing
Next.js 10 came out with Internationalized Routes... we cover the basics of it in this video, how to load translated remote data, and...
Read more >
How To Handle Routing in React Apps with React Router
In this step, you'll use URL queries and parameters to create dynamic routes. You'll learn how to pull information from search parameters with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found