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

feat: enable i18n support for AsyncAPI website #2184

Merged
merged 43 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
22da891
add custom i18n link
anshgoyalevil Sep 8, 2023
01972c9
Merge branch 'asyncapi:master' into i18n-link
anshgoyalevil Sep 9, 2023
b86965a
fix link.js
anshgoyalevil Sep 9, 2023
0062e7d
Merge branch 'master' into i18n-link
anshgoyalevil Sep 15, 2023
952c1ef
Merge branch 'master' into i18n-link
anshgoyalevil Sep 16, 2023
8a32016
Merge branch 'master' into i18n-link
anshgoyalevil Sep 18, 2023
0e95bd8
Merge branch 'master' into i18n-link
anshgoyalevil Sep 22, 2023
0b63e20
Merge branch 'master' into i18n-link
anshgoyalevil Sep 26, 2023
f8a981b
Merge branch 'master' into i18n-link
anshgoyalevil Sep 28, 2023
4bb2f3b
Enable i18n
anshgoyalevil Sep 28, 2023
13a375e
Fix type error
anshgoyalevil Sep 28, 2023
32af36f
Merge branch 'master' into enable-i18n
anshgoyalevil Oct 1, 2023
9e49072
Merge branch 'master' into enable-i18n
anshgoyalevil Oct 7, 2023
9d62657
Merge branch 'master' into enable-i18n
anshgoyalevil Oct 9, 2023
ba9e97a
change select value
anshgoyalevil Oct 9, 2023
6af5eeb
Merge branch 'master' into enable-i18n
anshgoyalevil Oct 11, 2023
40b2dd4
Merge branch 'master' into enable-i18n
anshgoyalevil Oct 15, 2023
99858db
Merge branch 'master' into enable-i18n
akshatnema Oct 21, 2023
863cdff
Merge branch 'master' into enable-i18n
anshgoyalevil Nov 3, 2023
fbf1226
Merge branch 'master' into enable-i18n
anshgoyalevil Nov 21, 2023
883ac0b
Merge branch 'master' into enable-i18n
anshgoyalevil Nov 29, 2023
e671b8b
Merge branch 'master' into enable-i18n
anshgoyalevil Dec 7, 2023
597cdb5
Merge branch 'master' into enable-i18n
anshgoyalevil Dec 16, 2023
a7c1f90
Merge branch 'master' into enable-i18n
anshgoyalevil Dec 27, 2023
c6d53c8
make changes to LanguageSelect
anshgoyalevil Dec 27, 2023
3444dad
Merge branch 'enable-i18n' of https://github.com/anshgoyalevil/websit…
anshgoyalevil Dec 27, 2023
563a817
Merge branch 'master' into enable-i18n
anshgoyalevil Jan 18, 2024
db8d0f1
Merge branch 'master' into enable-i18n
anshgoyalevil Feb 2, 2024
c308f23
add changes
anshgoyalevil Feb 2, 2024
4f85edf
Merge branch 'master' into enable-i18n
anshgoyalevil Feb 4, 2024
fd28f05
add comments and fix router
anshgoyalevil Feb 4, 2024
64accd0
remove unused import
anshgoyalevil Feb 4, 2024
c3d72f0
updated js doc comments
Feb 4, 2024
c64e922
update jsdoc comments
anshgoyalevil Feb 4, 2024
3a6c90c
Merge branch 'enable-i18n' of https://github.com/anshgoyalevil/websit…
anshgoyalevil Feb 4, 2024
867b943
Merge branch 'master' into enable-i18n
akshatnema Feb 6, 2024
4642b9a
add navbar and centered logo
anshgoyalevil Feb 6, 2024
2f07331
add infinite loop fix
anshgoyalevil Feb 6, 2024
30aded4
Merge branch 'master' into enable-i18n
anshgoyalevil Feb 6, 2024
ca1e82b
update UI of LanguageSelect
akshatnema Feb 7, 2024
a0c81e4
Merge branch 'master' into enable-i18n
akshatnema Feb 7, 2024
66a5186
Merge branch 'master' into enable-i18n
anshgoyalevil Feb 9, 2024
e6c95e0
Merge branch 'master' into enable-i18n
akshatnema Feb 11, 2024
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
2 changes: 1 addition & 1 deletion components/languageSelector/LanguageSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function LanguageSelect({
value={selected}
>
{options.map((option, index) => (
<option key={index} selected={option.value === selected} value={option.value} data-testid="Option-form">
<option key={index} value={option.value} data-testid="Option-form">
{option.text}
</option>
))}
Expand Down
28 changes: 27 additions & 1 deletion components/link.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
import Link from "next/link";
import { useRouter } from "next/router";
import { defaultLanguage, languages } from "../lib/i18n";
import i18nPaths from "../lib/i18nPaths";

const LinkComponent = ({ children, locale, ...props }) => {
const router = useRouter();

// If there is no router available (e.g., during server-side rendering & cypress tests), render a standard Link
if (!router) {
return (
<Link href={props.href} passHref>
{children}
</Link>
);
}
Comment on lines +10 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment for the usecase handled with this condtion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


const { pathname, query, asPath } = router;

// Detect current language
// Detect current language based on the path or query parameter
const slug = asPath.split("/")[1];
const langSlug = languages.includes(slug) && slug;
const language = query.lang || langSlug || defaultLanguage;

let href = props.href || pathname;

/*
If explicit href is provided, and the language-specific paths for the current language do not include the href, or if the href starts with "http", render a standard Link
*/
if ((props.href && i18nPaths[language] && !i18nPaths[language].includes(href)) || href.includes("http", 0)) {
return (
<Link href={href} passHref>
{children}
</Link>
);
}
Comment on lines +30 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment for the usecase handled with this condtion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// If a locale is provided, update the href with the locale
if (locale) {
if (props.href) {
href = `/${locale}${href}`;
} else {
// If the current path starts with "/404", update href to be the root path with the locale
// Otherwise, replace "[lang]" placeholder with the locale
if (pathname.startsWith("/404")) {
href = `/${locale}`;
} else {
href = pathname.replace("[lang]", locale);
}
}
} else {
// If no locale is provided, update the href with the current language or keep it as is
if (language) {
href = `/${language}${href}`;
} else {
Expand Down
6 changes: 3 additions & 3 deletions components/navigation/MenuBlocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import LinkComponent from '../link';
import Paragraph from '../typography/Paragraph';
import Label from './Label'
import Link from 'next/link'
import { useRouter } from 'next/router';


Expand All @@ -14,7 +14,7 @@ export default function MenuBlocks ({
items.map((item, index) => {
const isExternalHref = item.href && item.href.startsWith('http');
return (
<Link href={item.comingSoon ? '' : item.href} key={index}>
<LinkComponent href={item.comingSoon ? '' : item.href} key={index}>
<a data-testid="MenuBlocks-Link"
className={`flex items-start p-3 -m-3 space-x-4 transition duration-150 ease-in-out rounded-lg ${router.asPath === item.href ? 'bg-secondary-100 shadow-sm': 'hover:bg-gray-50'}`}
target={isExternalHref ? "_blank" : undefined}
Expand All @@ -32,7 +32,7 @@ export default function MenuBlocks ({
</Paragraph>
</div>
</a>
</Link>
</LinkComponent>
)
})
}
Expand Down
42 changes: 30 additions & 12 deletions components/navigation/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ import {
useTranslation,
} from "../../lib/i18n";
import browserLanguageDetector from "../../lib/browserLanguageDetector";
import i18nPaths from "../../lib/i18nPaths";

const isMobile = isMobileDevice();
const uniqueLangs = [...new Set(["EN", "DE"])].map((repo) => ({
key: repo,
text: repo,
}));

export default function NavBar({
className = '',
Expand All @@ -37,10 +34,30 @@ export default function NavBar({
const [mobileMenuOpen, setMobileMenuOpen] = useState();
const { i18n } = useTranslation();

const getUniqueLangs = () => {
let pathnameWithoutLocale = pathname;

// Check if the pathname includes "/[lang]", if so, replace it with an empty string
if (pathname && pathname.includes("/[lang]")) {
pathnameWithoutLocale = pathname.replace("/[lang]", "");
}

// Filter unique languages based on i18nPaths that include the modified pathnameWithoutLocale
let uniqueLangs = Object.keys(i18nPaths).filter(lang => i18nPaths[lang].includes(pathnameWithoutLocale)).map(lang => lang.toUpperCase());

// If no unique languages are found, default to ["EN"]
return uniqueLangs.length === 0 ? ["EN"] : uniqueLangs;
}
const uniqueLangs = getUniqueLangs().map((lang) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const uniqueLangs = getUniqueLangs().map((lang) => ({
const uniqueLangs = getUniqueLangs().map((lang) => ({

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

key: lang,
text: lang,
value: lang
}));

const changeLanguage = async (locale, langPicker) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add jsdocs to specify the functionality and params of this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// Verifies if the language change is from langPicker or the browser-api
if(langPicker){
if (langPicker) {
localStorage.setItem('i18nLang', locale);
}

Expand Down Expand Up @@ -71,10 +88,11 @@ export default function NavBar({
router.push(href);
};

// To be enabled on the last PR
// useEffect(() => {
// changeLanguage(browserLanguageDetector(), false);
// }, []);
// First param: Passes the language based on the browser's defalt language
// Second param: Prevents the language change from being saved in the local storage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add the function description here. Instead specify, what is the main functionality done by useEffect at initial render.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

useEffect(() => {
changeLanguage(browserLanguageDetector(), false);
}, []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What function does this useEffect do on initial render? specify this in a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


function outsideClick(menu) {
if (open !== menu) return;
Expand Down Expand Up @@ -179,14 +197,14 @@ export default function NavBar({
</SearchButton>

{/* // Language Picker Component */}
{/* <LanguageSelect
<LanguageSelect
options={uniqueLangs}
onChange={(value) => {
changeLanguage(value.toLowerCase(), true);
}}
className=""
selected={i18n.language.toLocaleUpperCase()}
/> */}
selected={i18n.language ? i18n.language.toUpperCase() : "EN"}
/>

<GithubButton text="Star on GitHub" href="https://github.com/asyncapi/spec" className="py-2 ml-2" inNav="true" />
</div>
Expand Down
12 changes: 12 additions & 0 deletions lib/i18nPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const i18nPaths = {
en: [
"", //Homepage Route
"/tools/cli"
],
de: [
"", //Homepage Route
"/tools/cli"
]
};

export default i18nPaths;
Loading
Loading