-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Changes from all commits
22da891
01972c9
b86965a
0062e7d
952c1ef
8a32016
0e95bd8
0b63e20
f8a981b
4bb2f3b
13a375e
32af36f
9e49072
9d62657
ba9e97a
6af5eeb
40b2dd4
99858db
863cdff
fbf1226
883ac0b
e671b8b
597cdb5
a7c1f90
c6d53c8
3444dad
563a817
db8d0f1
c308f23
4f85edf
fd28f05
64accd0
c3d72f0
c64e922
3a6c90c
867b943
4642b9a
2f07331
30aded4
ca1e82b
a0c81e4
66a5186
e6c95e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
); | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment for the usecase handled with this condtion. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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 = '', | ||||||||
|
@@ -37,10 +34,46 @@ export default function NavBar({ | |||||||
const [mobileMenuOpen, setMobileMenuOpen] = useState(); | ||||||||
const { i18n } = useTranslation(); | ||||||||
|
||||||||
/** | ||||||||
* Retrieves unique language options based on the current path and i18nPaths configuration. | ||||||||
* | ||||||||
* @returns {string[]} - An array of unique language options in uppercase. | ||||||||
*/ | ||||||||
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) => ({ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||
key: lang, | ||||||||
text: lang, | ||||||||
value: lang | ||||||||
})); | ||||||||
|
||||||||
/** | ||||||||
* Changes the language and updates the URL accordingly. | ||||||||
* | ||||||||
* @async | ||||||||
* @param {string} locale - The new locale/language to set. | ||||||||
* @param {boolean} langPicker - Indicates whether the change is from the language picker. | ||||||||
* If true, stores the language in local storage. | ||||||||
* @returns {Promise<void>} - A promise representing the completion of the language change. | ||||||||
* @throws {Error} - If an error occurs during the language change process. | ||||||||
*/ | ||||||||
const changeLanguage = async (locale, langPicker) => { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add jsdocs to specify the functionality and params of this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||
} | ||||||||
|
||||||||
|
@@ -71,11 +104,6 @@ export default function NavBar({ | |||||||
router.push(href); | ||||||||
}; | ||||||||
|
||||||||
// To be enabled on the last PR | ||||||||
// useEffect(() => { | ||||||||
// changeLanguage(browserLanguageDetector(), false); | ||||||||
// }, []); | ||||||||
|
||||||||
function outsideClick(menu) { | ||||||||
if (open !== menu) return; | ||||||||
setOpen(null); | ||||||||
|
@@ -179,14 +207,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> | ||||||||
|
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done