Next.js reference
init(options)
Import from @18ways/next/server.
import { init } from '@18ways/next/server';Common options:
| Option | Type | Notes |
|---|---|---|
apiKey | string | Required. |
baseLocale | string | Source locale for the app. |
locale | string | Optional fixed locale override. |
acceptedLocales | string[] | Optional explicit locale list. |
persistLocaleCookie | boolean | ((request) => boolean) | Default locale-cookie policy for middleware and client locale changes. |
cacheTtl | number | Cache TTL for adapter requests. |
fetcher | typeof fetch | Custom fetch implementation. |
pathRouting | WaysPathRoutingConfig | Include or exclude locale-prefixed routes. |
WaysRoot
Use WaysRoot in your App Router layout so the adapter can seed runtime state for the current
request.
htmlAttrs()
htmlAttrs() resolves the current lang and dir values for the document element.
generateWaysMetadata()
Use generateWaysMetadata() inside generateMetadata() when you want translated titles,
descriptions, and locale-aware canonical or alternate URLs.
waysMiddleware()
waysMiddleware() is the request-time middleware API returned from init(). It resolves locale
state, applies locale cookies, and returns the final NextResponse. Use
transformRequestHeaders or transformResponse when you need custom behavior.
init() return value
init() returns these fields:
| Field | What it does |
|---|---|
WaysRoot | Root wrapper for App Router layouts. |
htmlAttrs() | Resolves lang and dir for the current request. |
generateWaysMetadata() | Builds locale-aware metadata and can translate metadata strings. |
waysMiddleware() | Resolves locale-aware middleware behavior and returns a response. |
Client helpers
Import from @18ways/next/client.
useLocale()
Read and change the current locale. This is the hook you pair with LanguageSwitcher or your own
custom selector UI.
useUnlocalizedPathname()
Read the current pathname without the locale prefix.
useLocalizedHref()
Convert an internal href into the current locale-aware href.
localizePathname()
Pure helper for building a localized pathname outside React.
stripLocalePrefix()
Pure helper for removing a locale prefix from a pathname.
Path routing config
WaysPathRoutingConfig accepts:
{
include?: Array<string | RegExp>;
exclude?: Array<string | RegExp>;
}Use it when public URLs should carry locale prefixes. Leave it out when you want cookie-based or header-based locale selection without path rewriting.
Lower-level exports
There are also RSC and middleware exports in @18ways/next/rsc and @18ways/next/server, but
for most apps the init() surface above is the right abstraction.