diff --git a/src/app/(routes)/blog/categories/[categorySlug]/page.tsx b/src/app/(routes)/blog/categories/[categorySlug]/page.tsx index c8e63fd..8d2c851 100644 --- a/src/app/(routes)/blog/categories/[categorySlug]/page.tsx +++ b/src/app/(routes)/blog/categories/[categorySlug]/page.tsx @@ -6,14 +6,18 @@ import { redirect } from "next/navigation"; import NoDataFounded from "../../components/no-data-founded/noDataFounded"; import Pagination from "../../components/pagination/pagination"; import { getDictionary } from "@/app/lib/functions/dictionaries"; -import { getDefaultLocale } from "@/app/lib/functions/utils"; +import { + getDefaultLocale, + slugForLocale, +} from "@/app/lib/functions/utils"; async function fetchCategoryData( categorySlug: string, searchParams: URLSearchParams, + locale: string, ): Promise { const res = await fetch( - `${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories/${categorySlug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}`, + `${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories/${categorySlug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}&lang=${locale}`, { cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", }, @@ -26,11 +30,29 @@ async function fetchCategoryData( return res.json(); } +/** + * Blog categories are language twins sharing the `-fa` slug suffix. A Persian + * visitor landing on the English URL (a shared link, for example) is sent to + * the mirrored Persian category so both versions always show the right language. + */ +async function resolveCategorySlug(raw: string): Promise { + const locale = await getDefaultLocale(); + const localized = slugForLocale(raw, locale); + + if (localized !== raw) { + redirect(`/blog/categories/${localized}`); + } + + return locale; +} + export async function generateMetadata(props: categorySlugPropsType): Promise { const params = await props.params; + const locale = await resolveCategorySlug(params.categorySlug); const categoryData: CategoryDataType = await fetchCategoryData( params.categorySlug, new URLSearchParams(), + locale, ); return { @@ -47,11 +69,13 @@ export async function generateMetadata(props: categorySlugPropsType): Promise), + locale, ); return ( diff --git a/src/app/(routes)/blog/categories/page.tsx b/src/app/(routes)/blog/categories/page.tsx index 663a025..8413d89 100644 --- a/src/app/(routes)/blog/categories/page.tsx +++ b/src/app/(routes)/blog/categories/page.tsx @@ -26,8 +26,9 @@ export const metadata: Metadata = { export default async function Categories(props: categoryPropsType) { const searchParams = await props.searchParams; + const locale = await getDefaultLocale(); const CategoriesData: CategoriesListType = await fetch( - `${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories?per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_CATEGORIES_PER_CATEGORY_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}`, + `${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories?per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_CATEGORIES_PER_CATEGORY_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}&lang=${locale}`, { cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", }, diff --git a/src/app/(routes)/blog/components/home/filtered-posts/filteredPosts.tsx b/src/app/(routes)/blog/components/home/filtered-posts/filteredPosts.tsx index ffc1471..a574578 100644 --- a/src/app/(routes)/blog/components/home/filtered-posts/filteredPosts.tsx +++ b/src/app/(routes)/blog/components/home/filtered-posts/filteredPosts.tsx @@ -73,7 +73,7 @@ export default async function FilteredPosts({ data }: FilteredPostsProps) { {dict.blog.homePage.in}

{data[0].category.name} diff --git a/src/app/(routes)/blog/components/home/new/new.tsx b/src/app/(routes)/blog/components/home/new/new.tsx index b296a66..accc0a2 100644 --- a/src/app/(routes)/blog/components/home/new/new.tsx +++ b/src/app/(routes)/blog/components/home/new/new.tsx @@ -39,7 +39,7 @@ export default async function New({ data }: NewProps) { {dict.blog.homePage.in}

{data[0].category.name} diff --git a/src/app/(routes)/blog/components/home/random/random.tsx b/src/app/(routes)/blog/components/home/random/random.tsx index c41708c..83bbf6d 100644 --- a/src/app/(routes)/blog/components/home/random/random.tsx +++ b/src/app/(routes)/blog/components/home/random/random.tsx @@ -47,7 +47,7 @@ export default async function Random({ data }: RandomProps) { {dict.blog.homePage.in}

{randomPost.category.name} diff --git a/src/app/(routes)/blog/filtered-posts/[filter]/page.tsx b/src/app/(routes)/blog/filtered-posts/[filter]/page.tsx index e3f8499..3c31245 100644 --- a/src/app/(routes)/blog/filtered-posts/[filter]/page.tsx +++ b/src/app/(routes)/blog/filtered-posts/[filter]/page.tsx @@ -83,12 +83,13 @@ export default async function FilteredPosts(props: FilteredPostsPagepropsType) { const filteredPostApiUrl = filterKey ? FILTERS[filterKey].apiUrl : ""; const dict = await getDictionary(await getDefaultLocale()); + const locale = await getDefaultLocale(); let FilteredPostsData: FilteredPostsDataType; if (filteredPostApiUrl !== "") { FilteredPostsData = await fetch( - `${process.env.NEXT_PUBLIC_BLOG_API_URL}${filteredPostApiUrl}&per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_POSTS_PER_FILTERED_POSTS_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}`, + `${process.env.NEXT_PUBLIC_BLOG_API_URL}${filteredPostApiUrl}&per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_POSTS_PER_FILTERED_POSTS_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}&lang=${locale}`, { cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", }, diff --git a/src/app/(routes)/blog/page.tsx b/src/app/(routes)/blog/page.tsx index 6db9195..d9e320a 100644 --- a/src/app/(routes)/blog/page.tsx +++ b/src/app/(routes)/blog/page.tsx @@ -2,16 +2,21 @@ import FilteredPosts from "./components/home/filtered-posts/filteredPosts"; import New from "./components/home/new/new"; import Random from "./components/home/random/random"; import Categories from "./components/home/Categories/Categories"; +import { getDefaultLocale } from "@/app/lib/functions/utils"; export default async function Blog() { + const locale = await getDefaultLocale(); const homePageData: { latest_posts: []; latest_categories: []; most_viewed_posts: []; random_posts: []; - } = await fetch(`${process.env.NEXT_PUBLIC_BLOG_API_URL}/homepage`, { - cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", - }).then((res) => res.json()); + } = await fetch( + `${process.env.NEXT_PUBLIC_BLOG_API_URL}/homepage?lang=${locale}`, + { + cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", + }, + ).then((res) => res.json()); return (
diff --git a/src/app/(routes)/blog/posts/[postSlug]/page.tsx b/src/app/(routes)/blog/posts/[postSlug]/page.tsx index fecc73f..7955c4a 100644 --- a/src/app/(routes)/blog/posts/[postSlug]/page.tsx +++ b/src/app/(routes)/blog/posts/[postSlug]/page.tsx @@ -1,5 +1,8 @@ import { getDictionary } from "@/app/lib/functions/dictionaries"; -import { getDefaultLocale } from "@/app/lib/functions/utils"; +import { + getDefaultLocale, + slugForLocale, +} from "@/app/lib/functions/utils"; import { PostSlugPagePostType, PostSlugPagepropsType, @@ -14,9 +17,12 @@ import { getCoverImageSrc } from "./coverImage"; import { AUTHOR_PROFILE_URL } from "@/app/lib/social-links"; import { SITE_NAME, SITE_NAME_EN, SITE_URL } from "@/app/lib/site-config"; -async function fetchPostData(postSlug: string): Promise { +async function fetchPostData( + postSlug: string, + locale: string, +): Promise { const res = await fetch( - `${process.env.NEXT_PUBLIC_BLOG_API_URL}/posts/${postSlug}`, + `${process.env.NEXT_PUBLIC_BLOG_API_URL}/posts/${postSlug}?lang=${locale}`, { cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", } @@ -29,6 +35,23 @@ async function fetchPostData(postSlug: string): Promise { return res.json(); } +/** + * Every post is a strict English/Persian mirror sharing the `-fa` slug suffix. + * A visitor whose locale does not match the requested URL is sent to the twin, + * so the shared English link opens the Persian article when the site is in + * Persian and vice versa. + */ +async function resolvePostSlug(raw: string): Promise { + const locale = await getDefaultLocale(); + const localized = slugForLocale(raw, locale); + + if (localized !== raw) { + redirect(`/blog/posts/${localized}`); + } + + return locale; +} + function formatPostDate(value: unknown): string { if (typeof value !== "string" || value.trim() === "") { return ""; @@ -47,7 +70,8 @@ function formatPostDate(value: unknown): string { export async function generateMetadata(props: PostSlugPagepropsType): Promise { const params = await props.params; - const post: PostSlugPagePostType = await fetchPostData(params.postSlug); + const locale = await resolvePostSlug(params.postSlug); + const post: PostSlugPagePostType = await fetchPostData(params.postSlug, locale); const coverImage = getCoverImageSrc(post.post.featured_image); const pageUrl = `${SITE_URL}/blog/posts/${params.postSlug}`; @@ -93,8 +117,12 @@ export async function generateMetadata(props: PostSlugPagepropsType): Promise { const res = await fetch( - `${process.env.NEXT_PUBLIC_BLOG_API_URL}/tags/by-slug/${slug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}`, + `${process.env.NEXT_PUBLIC_BLOG_API_URL}/tags/by-slug/${slug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}&lang=${locale}`, { cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store", }, @@ -29,11 +33,29 @@ async function fetchTagData( return res.json(); } +/** + * Blog tags are language twins sharing the `-fa` slug suffix. A Persian visitor + * landing on the English URL is sent to the mirrored Persian tag so both + * versions always show the right language. + */ +async function resolveTagSlug(raw: string): Promise { + const locale = await getDefaultLocale(); + const localized = slugForLocale(raw, locale); + + if (localized !== raw) { + redirect(`/blog/tags/${localized}`); + } + + return locale; +} + export async function generateMetadata(props: tagSlugPropsType): Promise { const params = await props.params; + const locale = await resolveTagSlug(params.slug); const tagData: TagDataType = await fetchTagData( params.slug, new URLSearchParams(), + locale, ); return { @@ -45,11 +67,13 @@ export async function generateMetadata(props: tagSlugPropsType): Promise), + locale, ); const tagName = tagData.tag.name || params.slug.replaceAll(/%20|-|_/g, " "); diff --git a/src/app/lib/functions/utils.ts b/src/app/lib/functions/utils.ts index 9d1879f..2c2c8de 100644 --- a/src/app/lib/functions/utils.ts +++ b/src/app/lib/functions/utils.ts @@ -8,3 +8,20 @@ export const getDefaultLocale = async () => { : "en"; return defaultLocale; }; + +/** + * Blog content is stored as a strict English/Persian mirror, with Persian rows + * using the `-fa` slug suffix. These helpers locate a row's twin in the other + * language so the language switcher can jump between the two versions. + */ +export const isFaSlug = (slug: string): boolean => slug.endsWith("-fa"); + +export const slugForLocale = (slug: string, locale: string): string => { + const isFa = isFaSlug(slug); + + if (locale === "fa") { + return isFa ? slug : `${slug}-fa`; + } + + return isFa ? slug.slice(0, -3) : slug; +};