styledsea

Internationalization

Support multiple languages in your documentation

Read the Next.js Docs to learn more about implementing I18n in Next.js.

Introduction

Adalt core provides necessary middleware and options for i18n support.

You can define a config to share between utilities.

lib/i18n.ts
import type { I18nConfig } from 'styledsea-docs-core/i18n';
 
export const i18n: I18nConfig = {
  defaultLanguage: 'en',
  languages: ['en', 'cn'],
};

Source API

Change your current source configurations, loader will now generate multiple page trees for every locale.

lib/source.ts
import { i18n } from '@/lib/i18n';
import { loader } from 'styledsea-docs-core/source';
 
export const source = loader({
  i18n,
  // other options
});

You can access it with:

import { source } from '@/lib/source';
 
// get page tree
source.pageTree[params.lang];
 
// get page
source.getPage(params.slug, params.lang);
 
// get pages
source.getPages(params.lang);

Middleware

Redirects users to appropriate locale.

middleware.ts
import { createI18nMiddleware } from 'styledsea-docs-core/i18n';
import { i18n } from '@/lib/i18n';
 
export default createI18nMiddleware(i18n);
 
export const config = {
  // Matcher ignoring `/_next/` and `/api/`
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};

You can also customise the i18n middleware from i18n.ts.

Hide Locale Prefix

To hide the locale prefix, for example, use / instead of /en, use the hideLocale option.

ModeDescription
alwaysAlways hide the prefix, detect locale from cookies
default-localeOnly hide the default locale
neverNever hide the prefix (default)

It uses NextResponse.rewrite under the hood.

import type { I18nConfig } from 'styledsea-docs-core/i18n';
 
export const i18n: I18nConfig = {
  defaultLanguage: 'en',
  languages: ['en', 'cn'],
  hideLocale: 'default-locale',
};

It's not recommended to use always. On this mode, locale is stored as a cookie, read and set on the middleware.

This may cause undesired cache problems on your hosting platform, and need to pay extra attention on SEO to ensure search engines can index your pages correctly.

Writing Documents

see Page Conventions to learn how to organize your documents.

For Flexsearch, see Setup I18n.

Last updated on

On this page

Edit on GitHub