languageSwithcher component added

This commit is contained in:
Al Murad Uzzaman
2024-05-16 12:09:13 +06:00
parent b92a4a4c6a
commit 97d6f22dcb
3 changed files with 49 additions and 50 deletions
+31
View File
@@ -0,0 +1,31 @@
import React from 'react';
const LanguageSwitcher = ({ lang, languages, pathname }: { lang: string; languages: string[]; pathname: string }) => {
return (
<div className="mr-5">
<select
className="border border-dark py-1 rounded-sm"
onChange={(e) => {
const selectedLang = e.target.value;
const newPath = selectedLang === "en"
? pathname.replace(`/${lang}`, "")
: `/${selectedLang}${pathname.replace(`/${lang}`, "")}`;
window.location.href = newPath;
}}
value={languages.includes(lang) ? lang : 'en'}
>
{languages.map((child: string) => (
<option
key={child}
value={child}
>
{child}
</option>
))}
</select>
</div>
)
}
export default LanguageSwitcher
+14 -47
View File
@@ -2,7 +2,12 @@
import Logo from "@/components/Logo.astro";
import ThemeSwitcher from "@/components/ThemeSwitcher.astro";
import config from "@/config/config.json";
import { getLangFromUrl, languages, useTranslations } from "@/lib/utils/i18nUtils";
import LanguageSwitcher from "@/helpers/LanguageSwitcher";
import {
getLangFromUrl,
languages,
useTranslations,
} from "@/lib/utils/i18nUtils";
import { loadMenu } from "@/lib/utils/loadMenu";
import { getRelativeLocaleUrl } from "astro:i18n";
import { IoSearch } from "react-icons/io5";
@@ -108,7 +113,7 @@ const t = useTranslations(lang as any);
class="btn btn-outline-primary btn-sm"
href={navigation_button.link}
>
{t('get_started')}
{t("get_started")}
</a>
</li>
)
@@ -116,50 +121,6 @@ const t = useTranslations(lang as any);
</ul>
<div class="order-1 ml-auto flex items-center md:order-2 lg:ml-0">
<!-- Language dropdown -->
<div class="nav-item nav-dropdown group relative border mr-5">
<span
class={`nav-link inline-flex items-center ${
languages
.map((child) => getRelativeLocaleUrl(child, "/"))
.includes(pathname)
? "active"
: ""
}`}
>
{lang}
<svg class="h-4 w-4 fill-current" viewBox="0 0 20 20">
<path
d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
></path>
</svg>
</span>
<ul
class="nav-dropdown-list hidden group-hover:block lg:invisible lg:absolute lg:block lg:opacity-0 lg:group-hover:visible lg:group-hover:opacity-100"
>
{
languages.map((child: string) => (
<li class="nav-dropdown-item">
<a
href={
child === "en"
? pathname.replace(`/${lang}`, "")
: `/${child}${pathname.replace(`/${lang}`, "")}`
}
aria-label={child}
class={`nav-dropdown-link block ${
pathname.startsWith(`/${child}`) && "active"
}`}
>
{child}
</a>
</li>
))
}
</ul>
</div>
<!-- End Language dropdown -->
{
settings.search && (
<button
@@ -172,13 +133,19 @@ const t = useTranslations(lang as any);
)
}
<ThemeSwitcher className="mr-5" />
<LanguageSwitcher
client:load
lang={lang}
languages={languages}
pathname={pathname}
/>
{
navigation_button.enable && (
<a
class="btn btn-outline-primary btn-sm hidden lg:inline-block"
href={navigation_button.link}
>
{t('get_started')}
{t("get_started")}
</a>
)
}
+4 -3
View File
@@ -19,9 +19,10 @@ export function getStaticPaths() {
}
const { lang } = Astro.params;
const BLOG_FOLDER = "blog";
// const postIndex = await getListPage(BLOG_FOLDER, lang as keyof ContentEntryMap);
const postIndex: any
= await getListPage(BLOG_FOLDER, lang as keyof ContentEntryMap);
const postIndex: any = await getListPage(
BLOG_FOLDER,
lang as keyof ContentEntryMap
);
const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap;
const posts = await getSinglePage(BLOG_FOLDER, lang as keyof ContentEntryMap);