Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add internationalization #131

Merged
merged 12 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add localized link component and replace across app
  • Loading branch information
DaniloArantesF committed Feb 14, 2025
commit 4221a1a876d1cb550382dab9ba618c2e67eacf9a
2 changes: 1 addition & 1 deletion src/app/[lang]/about/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BookOpenCheckIcon, StarIcon } from "lucide-react";
import { Metadata } from "next";
import Image from "next/image";
import Link from "next/link";
import Link from "~/components/features/Link";
import { PageTitle } from "~/components/layout/PageTitle";

export const metadata: Metadata = {
Expand Down
19 changes: 19 additions & 0 deletions src/components/features/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client";
import NextLink from "next/link";
import { useMemo } from "react";
import { useLocale } from "~/hooks/use-locale";
import { localizePath } from "~/lib/utils";

type LinkProps = React.ComponentProps<typeof NextLink>;

export default function Link(props: LinkProps) {
const { locale } = useLocale();
const href = useMemo(() => {
if (!(typeof props.href === "string")) {
return props.href.href ?? "/";
}
return props.href;
}, [props]);

return <NextLink {...props} href={localizePath(locale, href)} />;
}
7 changes: 5 additions & 2 deletions src/components/features/committees/CommitteeDirectory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import React from "react";
import { TableCell, TableRow } from "~/components/ui/table";
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar";
import { DirectorySearchParams, TableDirectory } from "~/components/layout/Directory";
import {
DirectorySearchParams,
TableDirectory,
} from "~/components/layout/Directory";
import {
GlobeIcon,
LinkedinIcon,
Expand All @@ -11,7 +14,7 @@ import {
XSquareIcon,
} from "lucide-react";
import { Badge } from "~/components/ui/badge";
import Link from "next/link";
import Link from "~/components/features/Link";
import clsx from "clsx";

export interface CommitteeMember {
Expand Down
8 changes: 3 additions & 5 deletions src/components/features/governance/GovernanceActionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
} from "lucide-react";
import { GovernanceAction } from "~/lib/governance-actions";
import { GovernanceActionMetadata } from "~/lib/metadata";
import Link from "next/link";
import { useLocalizedPath } from "~/hooks/use-localized-path";
import Link from "~/components/features/Link";
import { useTranslation } from "~/hooks/use-translation/use-translation";

const getTypeLabel = (type: GovernanceAction["type"]) => {
Expand All @@ -29,7 +28,7 @@ const getTypeLabel = (type: GovernanceAction["type"]) => {

const getStatusBadge = (
status: "Pending" | "In Progress" | "Completed",
className?: string,
className?: string
) => {
const variants = {
Pending: "bg-yellow-100 text-yellow-800",
Expand Down Expand Up @@ -77,7 +76,6 @@ export const GovernanceActionCard = ({
const standard = "CIP-129"; // TODO
const governanceActionId =
"gov_action1pvv5wmjqhwa4u85vu9f4ydmzu2mgt8n7et967ph2urhx53r70xusqnmm525"; // TODO
const { getLocalizedPath } = useLocalizedPath();
const { dictionary } = useTranslation();

return (
Expand Down Expand Up @@ -140,7 +138,7 @@ export const GovernanceActionCard = ({

<div className="flex justify-center">
<Link
href={getLocalizedPath(`/governance/${action.id}`)}
href={`/governance/${action.id}`}
className={` ${buttonVariants()} `}
>
{dictionary.general["view-details-and-vote"]}
Expand Down
2 changes: 1 addition & 1 deletion src/components/features/profile/ProfileInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
VoteIcon,
} from "lucide-react";
import { Badge } from "~/components/ui/badge";
import Link from "next/link";
import Link from "~/components/features/Link";
import {
Tooltip,
TooltipContent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/features/proposals/ProposalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Eye, MessageSquare, Calendar } from "lucide-react";
import { buttonVariants } from "~/components/ui/button";
import { Proposal } from "~/lib/proposals";
import clsx from "clsx";
import Link from "next/link";
import Link from "~/components/features/Link";

export type ProposalCardProps = {
proposal: Proposal;
Expand Down
2 changes: 1 addition & 1 deletion src/components/features/proposals/ProposalContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link as LinkIcon } from "lucide-react";
import Link from "next/link";
import Link from "~/components/features/Link";
import { Proposal } from "~/lib/proposals";

interface ProposalContentProps {
Expand Down
2 changes: 1 addition & 1 deletion src/components/icons/Logo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Image from "next/image";
import Link from "next/link";
import Link from "~/components/features/Link";

export default function Logo() {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/AppSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SidebarMenuItem,
} from "~/components/ui/sidebar";
import { headerNavLinks } from "./Header";
import Link from "next/link";
import Link from "~/components/features/Link";
import { usePathname } from "next/navigation";
import clsx from "clsx";
import { Input } from "../ui/input";
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import React from "react";
import Link from "next/link";
import Link from "~/components/features/Link";
import { usePathname } from "next/navigation";
import { ChevronRight } from "lucide-react";

Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Image from "next/image";
import Link from "next/link";
import Link from "~/components/features/Link";
import { Twitter } from "lucide-react";

import CardanoLogo from "../../../public/cardano-logo.svg";
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Button, buttonVariants } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar";
import Logo from "../icons/Logo";
import Link from "next/link";
import Link from "~/components/features/Link";
import { SidebarTrigger } from "../ui/sidebar";
import { Separator } from "../ui/separator";
import { usePathname } from "next/navigation";
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react";

import { cn } from "~/lib/utils";
import { ButtonProps, buttonVariants } from "~/components/ui/button";
import Link from "next/link";
import Link from "~/components/features/Link";

const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => (
<nav
Expand Down
9 changes: 9 additions & 0 deletions src/hooks/use-locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client";
import { useParams } from "next/navigation";
import { Locale } from "~/config/i18n";

export function useLocale() {
const params = useParams();
const locale = params?.lang as Locale;
return { locale };
}
15 changes: 0 additions & 15 deletions src/hooks/use-localized-path.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import { i18n, Locale } from "~/config/i18n";

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
Expand All @@ -24,3 +25,22 @@ export const truncateMiddle = (input: string, maxLength: number): string => {
const end = input.slice(-half);
return `${start}...${end}`;
};

export function localizePath(locale: Locale, path: string) {
const urlParts = path.slice(1).split("/");
const locales = i18n.locales.map((l) => l.key);

// Absolute path, don't modify
if (!path.startsWith("/")) {
return path;
}

// Replace locale if present
if (urlParts.length && locales.includes(urlParts[0] as Locale)) {
return `/${locale}/` + urlParts.slice(1).join("/");
}

const normalizedPath = path.startsWith("/") ? path.slice(1) : path;

return `/${locale}/${normalizedPath}`;
}