Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect current language and use JS to present dynamic content in Docusaurus pages

I am using Docusaurus to build a site with two language - en and zh.

What I want is a dynamic if-else in JS:

if current_language is en:
    title = "en title"
else:
    title = "zh title"

How can I achieve this in Docusaurus pages? The default i18n is not handy to translate long HTML...

like image 763
Yang_____ Avatar asked Sep 16 '25 11:09

Yang_____


1 Answers

You can use useDocusaurusContext to get the current locale:

import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

const { siteConfig, i18n } = useDocusaurusContext();
console.log(i18n.currentLocale); // e.g. 'en'
like image 107
Dominik Peters Avatar answered Sep 19 '25 02:09

Dominik Peters