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

Add Flag Icons and Improve Locale Switcher #3

Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"dotenv": "^16.3.1",
"drizzle-orm": "^0.28.5",
"embla-carousel-react": "8.0.0-rc12",
"flag-icons": "^6.11.0",
"lucide-react": "^0.269.0",
"mysql2": "^3.6.0",
"next": "13.4.19",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/data/i18n/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ import { createI18nClient } from "next-international/client";

import { locales } from "~/data/i18n";

export const { useI18n, useScopedI18n, I18nProviderClient } =
createI18nClient(locales);
export const {
useI18n,
useScopedI18n,
I18nProviderClient,
useChangeLocale,
useCurrentLocale
} = createI18nClient(locales);
7 changes: 7 additions & 0 deletions src/data/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ type LocalesKeys = keyof typeof locales;

export const localeList = Object.keys(locales) as LocalesKeys[];

const allLocales = {
en: "en",
uk: "uk"
} as const;

export const LOCALES: typeof allLocales = allLocales;

export const defaultLocale = "en" as const satisfies keyof typeof locales;
37 changes: 17 additions & 20 deletions src/islands/switchers/locale-switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use client";

import { Moon, Sun } from "lucide-react";
import { useTheme } from "next-themes";
import { useChangeLocale, useCurrentLocale } from "~/data/i18n/client";

import "/node_modules/flag-icons/css/flag-icons.min.css";

import { LOCALES } from "~/data/i18n";
import { useIsClient } from "~/hooks/use-is-client";
import { Button, type ButtonProps } from "~/islands/primitives/button";
import {
Expand All @@ -18,12 +20,13 @@ export type LocaleSwitcherProps = ButtonProps & {
};

export function LocaleSwitcher({
iconClassName = "mr-2 h-4 w-4",
iconClassName = "mr-2",
className,
...props
}: LocaleSwitcherProps) {
const { setTheme } = useTheme();
const client = useIsClient();
const changeLocale = useChangeLocale();
const currentLocale = useCurrentLocale();

if (!client)
return (
Expand All @@ -35,12 +38,9 @@ export function LocaleSwitcher({
size="icon"
{...props}
>
<span className="h-4 w-4 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0">
🇬🇧
</span>
<span className="absolute h-4 w-4 rotate-90 scale-0 transition-transform dark:rotate-0 dark:scale-100">
🇺🇦
</span>
<span
className={`fi fi-${currentLocale === LOCALES.en ? "gb" : "ua"}`}
></span>
</Button>
);

Expand All @@ -54,27 +54,24 @@ export function LocaleSwitcher({
size="icon"
{...props}
>
<span className="h-4 w-4 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0">
🇬🇧
</span>
<span className="absolute h-4 w-4 rotate-90 scale-0 transition-transform dark:rotate-0 dark:scale-100">
🇺🇦
</span>
<span
className={`fi fi-${currentLocale === LOCALES.en ? "gb" : "ua"}`}
></span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem className="font-heading">Language</DropdownMenuItem>
<DropdownMenuSeparator />
{/* TODO: Use icons instead. Flags emoji are doen't play nice with Windows. */}
<DropdownMenuItem onClick={() => setTheme("light")}>
<DropdownMenuItem onClick={() => changeLocale(LOCALES.en)}>
<span aria-hidden="true" className={iconClassName}>
🇬🇧
<span className="fi fi-gb"></span>
</span>
<span>English</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("dark")}>
<DropdownMenuItem onClick={() => changeLocale(LOCALES.uk)}>
<span aria-hidden="true" className={iconClassName}>
🇺🇦
<span className="fi fi-ua"></span>
</span>
<span>Ukrainian</span>
</DropdownMenuItem>
Expand Down