Skip to content

NextDrupalBase.addLocalePrefix() fails when path starts with same letters as the locale #854

Open
@wktohesi

Description

@wktohesi

Package containing the bug

next-drupal (NPM package)

Describe the bug

In next-drupal version 2.0.0, the NextDrupalBase.addLocalePrefix() fails to add a proper locale prefix if the given path starts with the same letters as the locale. This affects the results from NextDrupalPages.translatePathFromContext() among others.

Expected behavior and steps to reproduce:

Expected value for test would be /en/enable/testing, but /enable/testing is returned instead:

import { NextDrupal } from "next-drupal"

const drupal = new NextDrupal("https://example.com")
const test = drupal.addLocalePrefix('/enable/testing/', {
  locale: 'en',
  default_locale: 'fi'
})

Additional context

I believe the issue comes from this condition:

if (locale && !path.startsWith(`/${locale}`) && locale !== defaultLocale) {
  localePrefix = `/${locale}`
}

Maybe replacing startsWith(`/${locale}`) with startsWith(`/${locale}/`) would do the trick?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageA new issue that needs triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions