Skip to content

Commit

Permalink
locale-aware document url
Browse files Browse the repository at this point in the history
resolved #4
  • Loading branch information
Fallen-Breath committed Nov 18, 2024
1 parent c26049f commit c0b9989
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/app/[locale]/homepage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Link } from "@/common/navigation";
import { McdrLogo } from "@/components/icons";
import { siteConfig } from "@/site/config";
import { Button, Text, ThemeIcon, Title } from '@mantine/core';
import { Icon, IconBook2, IconDevicesCheck, IconExternalLink, IconPackage, IconPackages, IconPlant2 } from "@tabler/icons-react";
import { clsx } from "clsx";
Expand Down Expand Up @@ -53,6 +52,7 @@ async function ClassicLongLogo({className}: {className?: string}) {

async function Hero() {
const t = await getTranslations('page.home')
const tUrls = await getTranslations('urls')

const buttonWidth = 180
const intro = (
Expand Down Expand Up @@ -86,7 +86,7 @@ async function Hero() {
variant="default"
component={Link}
target="_blank"
href={siteConfig.links.docs}
href={tUrls('document')}
>
{t('docs')}
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default async function RootLayout({
<div className={clsx("relative flex flex-col min-h-screen", styles.mainContainer)}>
<RouterTransition/>

<NextIntlClientProvider locale={locale} messages={pick(messages, 'layout.nav_bar')}>
<NextIntlClientProvider locale={locale} messages={pick(messages, ['layout.nav_bar', 'urls'])}>
<Navbar/>
</NextIntlClientProvider>

Expand Down
22 changes: 13 additions & 9 deletions src/components/layout/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import Link from "next/link";
import React from "react";
import styles from './navbar.module.css';

interface UrlProvider {
(key: string): string
}

interface NavItem {
icon: (props: TablerIconsProps) => React.ReactNode,
key: string
href: string
href: (urls?: UrlProvider) => string
isExternal: boolean
checkActive: (pathname: string) => boolean
}
Expand All @@ -27,21 +31,21 @@ const navItems: NavItem[] = [
{
icon: IconHome,
key: 'home',
href: '/',
href: (urls) => '/',
isExternal: false,
checkActive: (pathname: string) => pathname === '/',
},
{
icon: IconPackages,
key: 'plugins',
href: '/plugins',
href: (urls) => '/plugins',
isExternal: false,
checkActive: (pathname: string) => pathname === routes.catalogue() || pathname.startsWith(routes.pluginBase() + '/'),
},
{
icon: IconBook2,
key: 'docs',
href: siteConfig.links.docs,
href: (urls) => urls ? urls('document') : siteConfig.links.docs,
isExternal: true,
checkActive: (pathname: string) => false,
},
Expand All @@ -55,13 +59,14 @@ interface NavBarLinkProps {
}

function NavbarLink({className, showIcon, item, ...props}: NavBarLinkProps) {
const t = useTranslations('layout.nav_bar.navigation');
const t = useTranslations('layout.nav_bar.navigation')
const tUrls = useTranslations('urls')
const pathname = usePathname()
const Icon = item.icon
return (
<NaLink
className={clsx(styles.link, className, "hover:bg-mantine-light-gray-background")}
href={item.href}
href={item.href(tUrls)}
data-active={item.checkActive(pathname) || undefined}
{...props}
>
Expand Down Expand Up @@ -104,7 +109,7 @@ function DesktopNavBar({className, navOpened, navToggle}: { className?: string,
</div>

<div className="hidden sm:flex gap-3 justify-start ml-2 grow">
{navItems.map((item) => <NavbarLink key={item.href} item={item} showIcon={false}/>)}
{navItems.map((item) => <NavbarLink key={item.key} item={item} showIcon={false}/>)}
</div>

<div className="justify-end flex gap-2 items-center">
Expand All @@ -130,7 +135,7 @@ function MobileNavMenu({navOpened, setClosed}: { navOpened: boolean, setClosed:
<div className="flex flex-col gap-2 max-w-screen-xl mx-auto">
{navItems.map((item) => (
<NavbarLink
key={item.href} item={item}
key={item.key} item={item}
className="h-[3rem] w-full"
showIcon={true}
onClick={setClosed}
Expand Down Expand Up @@ -158,7 +163,6 @@ export function Navbar() {
<MobileNavMenu navOpened={navOpened} setClosed={navOpener.close}/>
</div>
</header>

</>
);
}
3 changes: 3 additions & 0 deletions src/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,8 @@
"meta_source_branch": "Git branch {branch}"
}
}
},
"urls": {
"document": "https://docs.mcdreforged.com/en"
}
}
3 changes: 3 additions & 0 deletions src/messages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,8 @@
"meta_source_branch": "Git 的 {branch} 分支"
}
}
},
"urls": {
"document": "https://docs.mcdreforged.com/zh-cn"
}
}

0 comments on commit c0b9989

Please sign in to comment.