Skip to content

Commit cb08593

Browse files
committed
refactor page translated logic to its own function
1 parent bc53e35 commit cb08593

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/lib/i18n/pageTranslation.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getPrimaryNamespaceForPath } from "../utils/translations"
2+
3+
import { areNamespacesTranslated } from "./translationStatus"
4+
5+
/**
6+
* Determine if a page should be considered translated for a given locale.
7+
*
8+
* This checks only the primary namespace inferred from the provided path. When
9+
* no primary namespace exists for the path, the page is assumed translated
10+
* because it depends solely on globally available shared namespaces (like
11+
* "common") rather than page-specific strings.
12+
*
13+
* @param locale - Locale code, e.g., "en", "es"
14+
* @param slug - Page path or slug, e.g., "/wallets/"
15+
* @returns Promise resolving to whether the page is translated
16+
* @example
17+
* await isPageTranslated("es", "/wallets/") // => true | false
18+
*/
19+
export async function isPageTranslated(
20+
locale: string,
21+
slug: string
22+
): Promise<boolean> {
23+
const primaryNamespace = getPrimaryNamespaceForPath(slug)
24+
25+
if (!primaryNamespace) {
26+
return true
27+
}
28+
29+
return areNamespacesTranslated(locale, [primaryNamespace])
30+
}

src/lib/utils/metadata.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import { getTranslations } from "next-intl/server"
33

44
import { DEFAULT_OG_IMAGE, SITE_URL } from "@/lib/constants"
55

6-
import { areNamespacesTranslated } from "../i18n/translationStatus"
6+
import { isPageTranslated } from "../i18n/pageTranslation"
77

8-
import {
9-
getPrimaryNamespaceForPath,
10-
isLocaleValidISO639_1,
11-
} from "./translations"
8+
import { isLocaleValidISO639_1 } from "./translations"
129
import { getFullUrl } from "./url"
1310

1411
import { routing } from "@/i18n/routing"
@@ -122,11 +119,7 @@ export const getMetadata = async ({
122119
return { ...base, robots: { index: false } }
123120
}
124121

125-
// Check if the page is translated by checking only the primary namespace
126-
const primaryNamespace = getPrimaryNamespaceForPath(slugString)
127-
const isTranslated = await areNamespacesTranslated(locale, [
128-
primaryNamespace || "",
129-
])
122+
const isTranslated = await isPageTranslated(locale, slugString)
130123

131124
// If the page is not translated, do not index the page
132125
return isTranslated ? base : { ...base, robots: { index: false } }

0 commit comments

Comments
 (0)