disabled ln fn added

This commit is contained in:
Al Murad Uzzaman
2024-05-19 14:01:35 +06:00
parent 4df29fbf63
commit e2338efc6b
10 changed files with 127 additions and 85 deletions
+2 -1
View File
@@ -20,7 +20,8 @@
"pagination": 2,
"summary_length": 200,
"blog_folder": "blog",
"default_language": "en"
"default_language": "en",
"disable_languages": []
},
"params": {
+9 -1
View File
@@ -25,7 +25,15 @@ const { theme_switcher }: { theme_switcher: boolean } = config.settings;
const { default_language } = config.settings;
const { trailing_slash } = config.site;
const lang = getLangFromUrl(Astro.url);
let lang = getLangFromUrl(Astro.url);
// Check if the current language is disabled
const disabledLanguages = config.settings.disable_languages as string[];
if (disabledLanguages.includes(lang)) {
// Redirect to the default language or handle as per your application logic
lang = default_language;
}
const url = constructUrl("/", lang, default_language, trailing_slash);
---
+5 -3
View File
@@ -19,8 +19,10 @@ const LanguageSwitcher = ({
return path;
};
// Sort languages by weight
const sortedLanguages = languages.sort((a, b) => a.weight - b.weight);
// Sort languages by weight and filter out disabled languages
const sortedLanguages = languages
.filter(language => !config.settings.disable_languages.includes(language.languageCode))
.sort((a, b) => a.weight - b.weight);
return (
<div className="mr-5">
@@ -53,4 +55,4 @@ const LanguageSwitcher = ({
);
};
export default LanguageSwitcher;
export default LanguageSwitcher;
+8 -5
View File
@@ -3,14 +3,17 @@ import Logo from "@/components/Logo.astro";
import Social from "@/components/Social.astro";
import config from "@/config/config.json";
import social from "@/config/social.json";
import { getLangFromUrl } from "@/lib/utils/i18nUtils";
import { loadMenu } from "@/lib/utils/loadMenu";
import { getLangFromUrl, getTranslations } from "@/lib/utils/i18nUtils";
import { markdownify } from "@/lib/utils/textConverter";
import type { ContentEntryMap } from "astro:content";
import { getRelativeLocaleUrl } from "astro:i18n";
const lang = getLangFromUrl(Astro.url);
const menu = loadMenu(lang);
const { footer }: { footer: { name: string; url: string }[] } = menu;
const menu = await getTranslations(lang as keyof ContentEntryMap);
let footer: any = [];
if (menu) {
footer = menu.footer;
}
---
<footer class="bg-theme-light dark:bg-darkmode-theme-light">
@@ -22,7 +25,7 @@ const { footer }: { footer: { name: string; url: string }[] } = menu;
<div class="mb-8 text-center lg:col-6 lg:mb-0">
<ul>
{
footer.map((menu) => (
footer.map((menu: any) => (
<li class="m-3 inline-block">
<a href={getRelativeLocaleUrl(lang, menu.url)}>{menu.name}</a>
</li>
+19 -10
View File
@@ -9,27 +9,33 @@ import {
getLangFromUrl,
getTranslations,
} from "@/lib/utils/i18nUtils";
import { loadMenu } from "@/lib/utils/loadMenu";
import type { ContentEntryMap } from "astro:content";
import { IoSearch } from "react-icons/io5";
const lang = getLangFromUrl(Astro.url);
const menu = loadMenu(lang);
let lang = getLangFromUrl(Astro.url);
const menu = await getTranslations(lang as keyof ContentEntryMap);
const { navigation_button, settings } = config;
const { default_language } = config.settings;
const { trailing_slash } = config.site;
const { pathname } = Astro.url;
const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
// Check if the current language is disabled
const disabledLanguages = config.settings.disable_languages as string[];
if (disabledLanguages.includes(lang)) {
// Redirect to the default language or handle as per your application logic
lang = default_language;
}
---
<header class={`header z-30 ${settings.sticky_header && "sticky top-0"}`}>
<nav class="navbar container">
<!-- logo -->
{/* logo */}
<div class="order-0">
<Logo />
</div>
<!-- navbar toggler -->
{/* navbar toggler */}
<input id="nav-toggle" type="checkbox" class="hidden" />
<label
for="nav-toggle"
@@ -46,14 +52,14 @@ const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
transform="rotate(45 10 10)"></polygon>
</svg>
</label>
<!-- /navbar toggler -->
{/* /navbar toggler */}
<ul
id="nav-menu"
class="navbar-nav order-3 hidden w-full pb-6 lg:order-1 lg:flex lg:w-auto lg:space-x-2 lg:pb-0 xl:space-x-8"
>
{
menu.main.map((menu: any) => (
menu?.main.map((menu: any) => (
<>
{menu.hasChildren ? (
<li class="nav-item nav-dropdown group relative">
@@ -131,7 +137,7 @@ const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
</>
))
}
{/* Navigation button */}
{
navigation_button.enable && (
<li class="mt-4 inline-block lg:hidden">
@@ -147,6 +153,7 @@ const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
</ul>
<div class="order-1 ml-auto flex items-center md:order-2 lg:ml-0">
{/* Search button */}
{
settings.search && (
<button
@@ -158,13 +165,15 @@ const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
</button>
)
}
{/* Theme switcher */}
<ThemeSwitcher className="mr-5" />
{/* Language switcher */}
{
languages.length > 1 && (
<LanguageSwitcher client:load lang={lang} pathname={pathname} />
)
}
{/* Navigation button */}
{
navigation_button.enable && (
<a
@@ -177,4 +186,4 @@ const { get_started } = await getTranslations(lang as keyof ContentEntryMap);
}
</div>
</nav>
</header>
</header>;
+58 -13
View File
@@ -1,8 +1,6 @@
import config from "@/config/config.json";
import languagesJSON from "@/config/language.json";
import { getRelativeLocaleUrl } from "astro:i18n";
import fs from "fs";
import path from "path";
const { default_language } = config.settings;
const locales: { [key: string]: any } = {};
@@ -31,20 +29,57 @@ export function getLangFromUrl(url: URL): string {
return default_language;
}
// export const getTranslations = async (lang: string) => {
// const menu = await import(
// `../../config/menu.${lang || default_language}.json`
// );
// const dictionary = await import(
// `../../i18n/${lang || default_language}.json`
// );
// return { ...menu, ...dictionary };
// };
// export const supportedLang = [""].concat(
// languagesJSON.map((lang) => lang.languageCode),
// );
// Function to construct the URL based on trailing_slash value
export const getTranslations = async (lang: string) => {
const menu = await import(
`../../config/menu.${lang || default_language}.json`
);
const dictionary = await import(
`../../i18n/${lang || default_language}.json`
);
return { ...menu, ...dictionary };
const {
default_language,
disable_languages,
}: { default_language: string; disable_languages: string[] } =
config.settings;
if (disable_languages.includes(lang)) {
lang = default_language;
}
let language = languagesJSON.find((l) => l.languageCode === lang);
if (!language) {
lang = default_language;
language = languagesJSON.find((l) => l.languageCode === default_language);
}
if (!language) {
throw new Error("Default language not found");
}
const contentDir = language.contentDir;
let menu, dictionary;
try {
menu = await import(`../../config/menu.${lang}.json`);
dictionary = await import(`../../i18n/${lang}.json`);
} catch (error) {
menu = await import(`../../config/menu.${default_language}.json`);
dictionary = await import(`../../i18n/${default_language}.json`);
}
return { ...menu.default, ...dictionary.default, contentDir };
};
export const supportedLang = [""].concat(
languagesJSON.map((lang) => lang.languageCode),
);
// Function to construct the URL based on trailing_slash value
export const constructUrl = (
url: string,
lang: string,
@@ -76,3 +111,13 @@ export const constructUrl = (
return constructedUrl;
};
const supportedLang = ["", ...languagesJSON.map((lang) => lang.languageCode)];
const disabledLanguages = config.settings.disable_languages as string[];
// Filter out disabled languages from supportedLang
const filteredSupportedLang = supportedLang.filter(
(lang) => !disabledLanguages.includes(lang),
);
export { filteredSupportedLang as supportedLang };
-41
View File
@@ -1,41 +0,0 @@
import fs from "fs";
import path from "path";
import languages from "@/config/language.json";
export interface ChildNavigationLink {
name: string;
url: string;
}
export interface NavigationLink {
name: string;
url: string;
hasChildren?: boolean;
children?: ChildNavigationLink[];
}
interface Menu {
main: NavigationLink[];
footer: NavigationLink[];
}
export function loadMenu(lang: string): Menu {
// Find the language object based on the language code
const language = languages.find((l) => l.languageCode === lang);
if (!language) {
throw new Error(
`Language '${lang}' not found in the list of supported languages.`,
);
}
// Load menu data based on the language code
const menuPath: string = path.join(
process.cwd(),
"src",
"config",
`menu.${language.languageCode}.json`,
);
const menuData: string = fs.readFileSync(menuPath, "utf-8");
return JSON.parse(menuData) as Menu;
}
+17 -3
View File
@@ -2,11 +2,13 @@
import config from "@/config/config.json";
import Base from "@/layouts/Base.astro";
import {
constructUrl,
getLangFromUrl,
getTranslations,
supportedLang,
} from "@/lib/utils/i18nUtils";
import type { ContentEntryMap } from "astro:content";
const { trailing_slash } = config.site;
export function getStaticPaths() {
const paths = supportedLang.map((lang) => ({
@@ -15,11 +17,23 @@ export function getStaticPaths() {
return paths;
}
const lang = getLangFromUrl(Astro.url);
let lang = getLangFromUrl(Astro.url);
// Check if the current language is disabled
const disabledLanguages = config.settings.disable_languages as string[];
if (disabledLanguages.includes(lang)) {
// Redirect to the default language or handle as per your application logic
lang = config.settings.default_language;
}
const { page_not_found_content, page_not_found, back_to_home } =
await getTranslations(lang as keyof ContentEntryMap);
const { default_language } = config.settings;
const href = lang && lang !== default_language ? `/${lang}/` : "/";
const href = constructUrl(
"/",
lang,
config.settings.default_language,
trailing_slash
);
---
<Base title="Page Not Found">
+5 -1
View File
@@ -4,6 +4,7 @@ import Base from "@/layouts/Base.astro";
import { getListPage } from "@/lib/contentParser.astro";
import { getTranslations, supportedLang } from "@/lib/utils/i18nUtils";
import PageHeader from "@/partials/PageHeader.astro";
import type { CollectionKey } from "astro:content";
import { type ContentEntryMap } from "astro:content";
export function getStaticPaths() {
@@ -14,7 +15,10 @@ export function getStaticPaths() {
}
const { lang } = Astro.params;
const contact = await getListPage("contact", lang as keyof ContentEntryMap);
const contact: any = await getListPage(
"contact" as CollectionKey,
lang as keyof ContentEntryMap
);
const { contact_form_action }: { contact_form_action: string } = config.params;
const { title, description, meta_title, image } = contact[0].data;
+4 -7
View File
@@ -1,6 +1,5 @@
---
import ImageMod from "@/components/ImageMod.astro";
import config from "@/config/config.json";
import Base from "@/layouts/Base.astro";
import { getListPage, getSinglePage } from "@/lib/contentParser.astro";
import { supportedLang } from "@/lib/utils/i18nUtils";
@@ -10,8 +9,6 @@ import Testimonial from "@/partials/Testimonial.astro";
import type { Button, Feature } from "@/types";
import type { ContentCollectionKey, ContentEntryMap } from "astro:content";
import { FaCheck } from "react-icons/fa";
const { default_language } = config.settings;
interface Homepage {
banner: {
title: string;
@@ -32,18 +29,18 @@ export function getStaticPaths() {
const { lang } = Astro.params;
const homepage: any = await getListPage(
"homepage" as ContentCollectionKey,
lang as keyof ContentEntryMap,
lang as keyof ContentEntryMap
);
const { banner, features } = homepage[0].data;
const { banner, features }: Homepage = homepage[0].data;
const testimonial = await getSinglePage(
"sections/testimonial" as ContentCollectionKey,
lang as keyof ContentEntryMap,
lang as keyof ContentEntryMap
);
const call_to_action = await getSinglePage(
"sections/call-to-action" as ContentCollectionKey,
lang as keyof ContentEntryMap,
lang as keyof ContentEntryMap
);
---