From c2523dd294e906b4d884345eedded45b786c063c Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sun, 28 Aug 2022 18:39:16 +0300 Subject: [PATCH] fixed i18n --- astro.config.mjs | 18 +++++++---- .../AlternateLangLink/AlternateLangLink.astro | 30 +++++++++++++++++++ src/components/AlternateLangLink/index.ts | 1 + src/components/ArticleItem/ArticleItem.tsx | 2 +- .../LanguageSelect/LanguageSelect.astro | 14 +++++++-- src/config.ts | 2 ++ src/i18n/languages.ts | 1 + src/i18n/utils.ts | 4 +-- src/pages/[...slug].astro | 9 ++---- src/templates/About.astro | 15 ++++++++-- src/types/main.d.ts | 5 ++-- src/utils/url.ts | 4 +++ 12 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 src/components/AlternateLangLink/AlternateLangLink.astro create mode 100644 src/components/AlternateLangLink/index.ts diff --git a/astro.config.mjs b/astro.config.mjs index 0c6ce25..9991a20 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,8 +1,3 @@ -import { defineConfig } from "astro/config"; -import { h } from "hastscript"; -import { toString } from "hast-util-to-string"; -import { escape } from "html-escaper"; - /** * Integrations */ @@ -24,6 +19,14 @@ import remarkSmartypants from "remark-smartypants"; import rehypeSlug from "rehype-slug"; import rehypeAutolinkHeadings from "rehype-autolink-headings"; +/** + * Other required modules + */ +import { defineConfig } from "astro/config"; +import { h } from "hastscript"; +import { toString } from "hast-util-to-string"; +import { escape } from "html-escaper"; + const createAnchorLabel = (heading) => { const node = h("span.anchor-label", escape(heading)); node.properties["is:raw"] = true; @@ -41,11 +44,14 @@ export default defineConfig({ locales: { ru: "ru", en: "en", + de: "de", }, }, }), image(), - compress(), + compress({ + img: false, + }), mdx({ remarkPlugins: [remarkGfm, remarkSmartypants], rehypePlugins: [ diff --git a/src/components/AlternateLangLink/AlternateLangLink.astro b/src/components/AlternateLangLink/AlternateLangLink.astro new file mode 100644 index 0000000..9481056 --- /dev/null +++ b/src/components/AlternateLangLink/AlternateLangLink.astro @@ -0,0 +1,30 @@ +--- +/** + * This components is used in article layout to display links in alternative lanugages. + */ +import { DEFAULT_LANGUAGE } from "@root/config"; +import { getLanguageFromURL } from "@root/utils"; + +export interface Props extends astroHTML.JSX.AnchorHTMLAttributes { + lang: LanguageKeys; +} + +const { pathname } = Astro.url; +const siteLang = getLanguageFromURL(pathname); +const { lang, ...attrs } = Astro.props as Props; +let href: string; +if (lang === DEFAULT_LANGUAGE) { + href = `/${pathname.split("/").slice(2).join("/")}`; +} else { + href = `/${lang}/`; + if (siteLang !== DEFAULT_LANGUAGE) { + href += `${pathname.split("/").slice(2).join("/")}`; + } else { + href += `${pathname.split("/").slice(1).join("/")}`; + } +} +--- + + + + diff --git a/src/components/AlternateLangLink/index.ts b/src/components/AlternateLangLink/index.ts new file mode 100644 index 0000000..49f0896 --- /dev/null +++ b/src/components/AlternateLangLink/index.ts @@ -0,0 +1 @@ +export { default } from "./AlternateLangLink.astro"; diff --git a/src/components/ArticleItem/ArticleItem.tsx b/src/components/ArticleItem/ArticleItem.tsx index 0c0e074..4051a13 100644 --- a/src/components/ArticleItem/ArticleItem.tsx +++ b/src/components/ArticleItem/ArticleItem.tsx @@ -11,7 +11,7 @@ export type ArticleItemProps = { export const ArticleItem: Component = (props) => { const isNonDefaultLanguage = props.data.lang && props.data.lang !== props.lang; - const hrefLangPrefix = props.data.lang && props.data.lang !== DEFAULT_LANGUAGE ? "/en" : ""; + const hrefLangPrefix = props.data.lang && props.data.lang !== DEFAULT_LANGUAGE ? `/${props.data.lang}` : ""; return (