Skip to content

Commit

Permalink
Implement exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed Feb 22, 2024
1 parent fd0bd3a commit 5d79d6f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("<Link>", () => {
beforeEach(() => {
//known starting state
setLanguageTag(sourceLanguageTag)
process.env.__NEXT_ROUTER_BASE_PATH = ""
})
afterEach(() => cleanup())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
} = prefixStrategy({
availableLanguageTags: ["en", "de", "de-CH"],
sourceLanguageTag: "en",
exclude: [],
exclude: [/^\/api\//],
})

describe("getLocaleFromLocalisedPath", () => {
Expand Down Expand Up @@ -46,6 +46,10 @@ describe("getLocalisedPath", () => {
it("does not add a language prefix if the new locale is the source language tag", () => {
expect(getLocalisedPath("/some/path", "en")).toBe("/some/path")
})

it("does not localise excluded paths", () => {
expect(getLocalisedPath("/api/some/path", "de")).toBe("/api/some/path")
})
})

describe("translatePath", () => {
Expand Down Expand Up @@ -163,3 +167,4 @@ describe("localiseHref", () => {
})
})
})

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import type { LinkProps } from "next/link"
export function prefixStrategy({
availableLanguageTags,
sourceLanguageTag,
exclude,
}: {
availableLanguageTags: readonly string[]
sourceLanguageTag: string
exclude: RegExp[]
}) {
function isExcluded(path: string) {
return exclude.some((pattern) => pattern.test(path))
}

function getLocaleFromLocalisedPath(localisedPath: string): string | undefined {
const maybeLocale = localisedPath.split("/")[1]
if (!availableLanguageTags.includes(maybeLocale)) return undefined
Expand All @@ -29,7 +34,7 @@ export function prefixStrategy({
}

function getLocalisedPath(canonicalPath: string, locale: string): string {
if (locale === sourceLanguageTag) return canonicalPath
if (isExcluded(canonicalPath) || locale === sourceLanguageTag) return canonicalPath
return `/${locale}${canonicalPath}`
}

Expand Down

0 comments on commit 5d79d6f

Please sign in to comment.