You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Progressive Web App (PWA) optimized with offline capability.
TypeScript
Implementing a lightweight Internationalization (i18n) solution wtih Next.js 13 App Router and SSG support
Design Goals
Support static site generation (SSG), automatically generated as static HTML + JSON (uses getStaticProps).
Auto-detect the user's locale by default.
Allow users to select their preferred locale and remember their preference.
Display the default locale without a URL prefix.
Allow users to manually change the locale in the URL and override the detected and saved locales.
Locale Priority
Locale Priority
Description
URL locale (/locale)
The locale specified in the URL, e.g. /de or /en/about.
Preferred locale
The user's preferred locale, saved in a cookie.
Detected locale
The user's locale, detected using the accept-language header.
Default locale
The website's default locale.
Routing Behavior
Scenario
Routing Behavior
User visits the website for the first time and their locale is de.
The user is redirected to /de.
User visits the website and their preferred locale is fr.
The user is redirected to /fr, even if their detected locale is de.
User visits /en and their preferred locale is fr.
The user's preferred locale is changed to en, and they are redirected to /.
User visits /de and their preferred locale is fr.
The user's preferred locale is ignored, and /de is displayed.
Example
Suppose a website has the following configuration:
Default locale: en
Supported locales: en, de, fr
A user visits the website for the first time and their locale is de. The user is redirected to /de because their detected locale is de and there is a supported locale for that language.
The user then selects fr as their preferred locale. The next time the user visits the website, they are redirected to /fr because their preferred locale is fr.
The user then visits /en. Their preferred locale is changed to en and they are redirected to /. This is because the user's preferred locale has precedence over the detected locale.
Implementation
Install dependencies
npm i @formatjs/intl-localematcher negotiator
npm i -D @types/negotiator
i18n.ts
Configure your default locale (defaultLocale) and supported locales (locales)
Server Components: Use getDictionary to get the translation dictionary t for the current locale
Move your layout.tsx and page.tsx files inside app/ into app/[lang], icons (favicon.ico, icon.png, apple-icon.png) and manifest.ts should stay in app/.
In the root layout, use generateStaticParams to statically generate all locale routes at build time (SSG) instead of on-demand at request time (SSR).
Implementing a three state Dark Mode Toggle wtih React and TailwindCSS support
Design Goals
Does not flash the wrong mode on page load. The website should immediately render in the light or dark mode that matches the user's system settings.
Adds or removes the dark class for Tailwind CSS and also sets the CSS color-scheme property for scrollbars on the <html> element.
The toggle button allows the user to override the system settings. The toggle button has three states: Light, System, and Dark, with System as the default.
If the toggle is set to System, the page should live update if the system settings are changed.
Theme selection is persisted through page changes and separate sessions using local storage.
Does not block rendering of the UI until the React app is hydrated and renders. Only a few lines of JavaScript that set the light/dark mode attribute on the <html> element should block rendering.
Does not cause any terminal or browser errors. This includes errors during the server component render and the rendering of any of the React components in the browser.
The first time the light/dark mode toggle is rendered on the client, it should already be in the correct state. Since this cannot be determined on the server, the toggle component may flash briefly on the client.
Implementation
Set up tailwind.config.ts to toggle dark mode manually
Add suppressHydrationWarning to the <html> element
This property only applies one level deep to just the <html> element, so it won't block hydration warnings on other elements.
The HydrationWarning is caused by the few lines of JavaScript that add the dark class to the <html> element according to client settings before first rendering. This is used to prevent the wrong theme from flashing on page load.