-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): language switcher + translated legal links
- Loading branch information
Showing
11 changed files
with
169 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Link from "next/link"; | ||
|
||
import Text from "src/components/text/Text"; | ||
import useLanguage from "src/utils/hooks/useLanguage"; | ||
|
||
import styles from "./languageSwitcher.module.css"; | ||
|
||
export default function LanguageSwitcher() { | ||
const { slugTranslations } = useLanguage(); | ||
return ( | ||
<div className={styles.wrapper}> | ||
{slugTranslations?.map( | ||
(slugTranslation) => | ||
slugTranslation?.language && ( | ||
<Link key={slugTranslation.language.id} href={slugTranslation.slug}> | ||
<Text type={"small"}>{slugTranslation.language.icon}</Text> | ||
</Link> | ||
), | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.wrapper { | ||
display: flex; | ||
gap: 1rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { usePathname } from "next/navigation"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { fetchWithToken } from "studio/lib/fetchWithToken"; | ||
import { SlugTranslations } from "studio/lib/interfaces/slugTranslations"; | ||
import { LanguageObject } from "studio/lib/interfaces/supportedLanguages"; | ||
import { LANGUAGES_QUERY } from "studio/lib/queries/languages"; | ||
import { SLUG_TRANSLATIONS_FROM_LANGUAGE_QUERY } from "studio/lib/queries/slugTranslations"; | ||
|
||
function useSlugTranslations( | ||
currentLanguage: LanguageObject | undefined, | ||
slug: string, | ||
availableLanguages: LanguageObject[] | null, | ||
) { | ||
const [slugTranslationsData, setSlugTranslationsData] = | ||
useState<SlugTranslations | null>(null); | ||
|
||
useEffect(() => { | ||
if (currentLanguage === undefined) { | ||
return; | ||
} | ||
fetchWithToken<SlugTranslations | null>( | ||
SLUG_TRANSLATIONS_FROM_LANGUAGE_QUERY, | ||
{ | ||
slug, | ||
language: currentLanguage?.id, | ||
}, | ||
).then((data) => setSlugTranslationsData(data)); | ||
}, [currentLanguage, slug]); | ||
|
||
return ( | ||
slug === "" | ||
? availableLanguages?.map((lang) => ({ | ||
slug: "", | ||
language: lang.id, | ||
})) | ||
: slugTranslationsData?._translations | ||
) | ||
?.filter( | ||
(translation) => | ||
translation && translation.language !== currentLanguage?.id, | ||
) | ||
?.map( | ||
(translation) => | ||
translation && { | ||
slug: `/${translation.language}/${translation.slug}`, | ||
language: availableLanguages?.find( | ||
(lang) => lang.id === translation.language, | ||
), | ||
}, | ||
); | ||
} | ||
|
||
export default function useLanguage() { | ||
const pathname = usePathname(); | ||
|
||
const [availableLanguages, setAvailableLanguages] = useState< | ||
LanguageObject[] | null | ||
>(null); | ||
|
||
const defaultLanguage = availableLanguages?.find( | ||
(language) => language.default, | ||
); | ||
|
||
const language = availableLanguages?.find( | ||
({ id }) => pathname.startsWith(`/${id}/`) || pathname === `/${id}`, | ||
); | ||
|
||
const currentLanguage = language ?? defaultLanguage; | ||
|
||
const slug = pathname | ||
.split("/") | ||
.slice(language !== undefined ? 2 : 1) | ||
.join("/"); | ||
|
||
const slugTranslations = useSlugTranslations( | ||
currentLanguage, | ||
slug, | ||
availableLanguages, | ||
); | ||
|
||
useEffect(() => { | ||
fetchWithToken<LanguageObject[] | null>(LANGUAGES_QUERY).then((data) => | ||
setAvailableLanguages(data), | ||
); | ||
}, []); | ||
|
||
return { | ||
language, | ||
defaultLanguage, | ||
availableLanguages, | ||
slugTranslations, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export interface SlugTranslation { | ||
language: string; | ||
slug: string; | ||
} | ||
|
||
export interface SlugTranslations { | ||
_translations: ({ | ||
language: string; | ||
slug: string; | ||
} | null)[]; | ||
_translations: (SlugTranslation | null)[]; | ||
} |