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: add i18nPaths for selective i18n routing #2131

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 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
24283b2
Merge branch 'master' into i18n-link
anshgoyalevil Oct 1, 2023
705c4cf
Merge branch 'master' into i18n-link
anshgoyalevil Oct 7, 2023
519bdeb
Update components/link.js
anshgoyalevil Oct 9, 2023
7161832
add props spreader
anshgoyalevil Oct 9, 2023
f763069
Merge branch 'master' into i18n-link
anshgoyalevil Oct 9, 2023
2d5a874
Merge branch 'master' into i18n-link
anshgoyalevil Oct 11, 2023
fd6f146
Merge branch 'master' into i18n-link
anshgoyalevil Oct 15, 2023
8634178
Merge branch 'master' into i18n-link
anshgoyalevil Oct 21, 2023
9801eb5
Merge branch 'master' into i18n-link
anshgoyalevil Nov 3, 2023
a1efd99
Merge branch 'master' into i18n-link
anshgoyalevil Nov 21, 2023
5c4aea5
Merge branch 'master' into i18n-link
anshgoyalevil Nov 29, 2023
bf2a2d7
Merge branch 'master' into i18n-link
anshgoyalevil Dec 7, 2023
3920471
Merge branch 'master' into i18n-link
anshgoyalevil Dec 16, 2023
8603593
Merge branch 'master' into i18n-link
anshgoyalevil Dec 27, 2023
233b1db
Merge branch 'master' into i18n-link
anshgoyalevil Jan 18, 2024
a8368e9
Merge branch 'master' into i18n-link
akshatnema Feb 2, 2024
6a25760
remove Link import from MenuBlocks
anshgoyalevil Feb 2, 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
22 changes: 20 additions & 2 deletions components/link.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
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 (!router) {
return (
<Link href={props.href} passHref>
{children}
</Link>
);
}

const { pathname, query, asPath } = router;

// Detect current language
Expand All @@ -13,6 +23,14 @@ const LinkComponent = ({ children, locale, ...props }) => {

let href = props.href || pathname;

if ((props.href && i18nPaths[language] && !i18nPaths[language].includes(href)) || href.startsWith("http")) {
return (
<Link {...props} href={href} passHref>
{children}
</Link>
);
}
Comment on lines +26 to +32
Copy link
Member

Choose a reason for hiding this comment

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

Which usecase is handled in this if condition? Kindly specify that in comment.

Copy link
Member Author

@anshgoyalevil anshgoyalevil Feb 2, 2024

Choose a reason for hiding this comment

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

It would handle the cases where a webpage is not yet translated or there is an external website's href


if (locale) {
if (props.href) {
href = `/${locale}${href}`;
Expand All @@ -35,14 +53,14 @@ const LinkComponent = ({ children, locale, ...props }) => {
href = href.replace(/([^:]\/)\/+/g, "$1").replace("//", "/");

return (
<Link href={href} passHref>
<Link {...props} href={href} passHref>
{children}
</Link>
);
};

export const LinkText = ({ href, children, ...props }) => {
return <Link href={href || ""}>{children}</Link>;
return <Link {...props} href={href || ""}>{children}</Link>;
};

export default LinkComponent;
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
10 changes: 10 additions & 0 deletions lib/i18nPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const i18nPaths = {
en: [
"/tools/cli"
],
de: [
"/tools/cli"
]
};
Comment on lines +1 to +8
Copy link
Member

Choose a reason for hiding this comment

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

What is the use of these i18nPaths?

Copy link
Member Author

Choose a reason for hiding this comment

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

It would work as a record for the translated links. Since we are using static site generation, we needed a mechanism to predefine the translated webpages which could be used by the i18n LinkComponent


export default i18nPaths;
Loading