Skip to content

Commit

Permalink
Merge pull request #26 from c2r0b/main
Browse files Browse the repository at this point in the history
Fix i18n with tauri
  • Loading branch information
c2r0b authored Apr 3, 2023
2 parents b195b22 + fab3293 commit 1876f64
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 0 additions & 15 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
38 changes: 34 additions & 4 deletions pages/index.tsx → pages/[locale]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,50 @@ 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(),
})),
locale: context.locale
}
}
}
}
1 change: 0 additions & 1 deletion src/app/layout/sim/Sim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
3 changes: 1 addition & 2 deletions src/app/modals/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

1 comment on commit 1876f64

@vercel
Copy link

@vercel vercel bot commented on 1876f64 Apr 3, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.