diff --git a/middleware.ts b/middleware.ts index 71e9fbd..9df9a2d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -10,8 +10,8 @@ export async function middleware(req: NextRequest) { ) { return } - - if (req.nextUrl.locale === 'default') { + + if (req.nextUrl.pathname == "/") { const locale = req.cookies.get('NEXT_LOCALE') || 'en-us' return NextResponse.redirect( diff --git a/next.config.js b/next.config.js index df45b7e..69e191b 100644 --- a/next.config.js +++ b/next.config.js @@ -2,26 +2,11 @@ const withPWA = require('next-pwa')({ dest: 'public' }) -const { tx } = require('@transifex/native'); - -tx.init({ - token: process.env.TX_NATIVE_PUBLIC_TOKEN, -}); - module.exports = async () => withPWA({ reactStrictMode: true, images: { unoptimized: true, }, - i18n: { - locales: ['default', ...( - await tx.getLanguages()) - .filter((lang) => lang.code !== 'en') - .map((lang) => lang.code.replace('_', '-').toLocaleLowerCase()) - ], - defaultLocale: 'default', - localeDetection: true, - }, publicRuntimeConfig: { TxNativePublicToken: process.env.TX_NATIVE_PUBLIC_TOKEN, } diff --git a/pages/index.tsx b/pages/[locale]/index.tsx similarity index 72% rename from pages/index.tsx rename to pages/[locale]/index.tsx index 76654ce..f63c62b 100644 --- a/pages/index.tsx +++ b/pages/[locale]/index.tsx @@ -62,15 +62,45 @@ export default observer((props) => { ); }); -export async function getServerSideProps(context) { - const data = await getServerSideTranslations(context) +// get the list of paths based on locales from the API +const getPathSlugs = async () => { const languages = await tx.getLanguages(); + // We fetched locales from our API once at build time + return languages + .filter((language) => language.code !== 'en') + .map((language) => ({ + params: { + locale: language.code.replace('_', '-').toLocaleLowerCase(), + }, + })); +} + +export async function getStaticPaths(_) { + const pathsWithLocale = await getPathSlugs(); + return { + paths: pathsWithLocale, + fallback: false, + }; +} + +// get the locales information from the API +export async function getStaticProps(context) { + const languages = (await tx.getLanguages()) + .filter((language) => language.code !== 'en'); + if (!context.locale) { + context.locale = context.params.locale; + } + if (!context.locales) { + context.locales = languages.map((language) => language.code); + } + + const data = await getServerSideTranslations(context) + return { props: { ...data, languages: languages - .filter((language) => language.code !== 'en') .map((language) => ({ ...language, code: language.code.replace('_', '-').toLocaleLowerCase(), @@ -78,4 +108,4 @@ export async function getServerSideProps(context) { locale: context.locale } } -} +} \ No newline at end of file diff --git a/src/app/layout/sim/Sim.tsx b/src/app/layout/sim/Sim.tsx index 7f531ec..87b96cd 100644 --- a/src/app/layout/sim/Sim.tsx +++ b/src/app/layout/sim/Sim.tsx @@ -51,7 +51,6 @@ export default observer(() => { code = Sim.getCode().split("\n") }) => { sim.step++; - console.log(code, editor) if (sim.step > lastStep) { sim.step = 1; sim.codeLine++; diff --git a/src/app/modals/settings.tsx b/src/app/modals/settings.tsx index 3402381..2a29431 100644 --- a/src/app/modals/settings.tsx +++ b/src/app/modals/settings.tsx @@ -51,9 +51,8 @@ export default observer((props:IProps) => { if (!option.value) return; // Update the URL with the new locale while keeping the current path - const { pathname, asPath, query } = router; setCookie('NEXT_LOCALE', option.value, { path: '/' }); - router.push({ pathname, query }, asPath, { locale: option.value }); + router.push("/" + option.value); }; const themeLabelId = useId('label');