Raypx

@raypx/i18n

Internationalization with use-intl and locale routing.

@raypx/i18n provides internationalization support using use-intl with URL-based locale routing. It handles locale detection, URL rewriting, message loading, and server-side request configuration.

Supported Locales

LocaleLabelNative Label
en-USEnglishEnglish
zh-CNChinese (Simplified)Simplified Chinese

The default locale is en-US.

Core Exports

Routing

ExportDescription
routingRouting configuration object (locales, prefix, cookie, ignored paths)
SUPPORTED_LOCALESReadonly array of supported locale codes
DEFAULT_LOCALEThe default locale ("en-US")
LOCALE_COOKIE_NAMECookie name ("raypx-locale")
LOCALE_METAMetadata map with labels and native labels for each locale
isValidLocale(locale)Type guard to check if a string is a valid locale
shouldIgnorePath(pathname)Check if a path should skip locale processing
extractLocaleFromPath(pathname)Extract locale and stripped path from a URL path
stripLocalePrefix(pathname)Remove locale prefix from a path
localizePath(pathname, locale)Add locale prefix to a path
createCookieHeader(locale)Create a Set-Cookie header string

Server

ExportDescription
getRequestConfig(input)Create the full i18n request config (locale, messages, timeZone)
resolveRequestLocale(request)Detect locale from a Request object
I18nRequestConfigType for the request config object
ExportDescription
getCurrentLocale()Get the current locale from the URL or cookie
deLocalizeUrl(url)Strip the locale prefix from a URL
localizeUrl(url, locale?)Ensure a URL has a locale prefix
switchLocalePath(pathname, locale)Get the path for switching to another locale

Messages

ExportDescription
getMessages(locale)Load translation messages for a locale
AppMessagesType representing the full message structure

Routing Configuration

The routing object defines the core behavior:

export const routing = {
  locales: ["en-US", "zh-CN"] as const,
  defaultLocale: "en-US" as SupportedLocale,
  localeCookie: "raypx-locale",
  localePrefix: "always" as const,
  ignoredPathPrefixes: ["/api", "/__", "/llms"],
}
  • localePrefix: "always" -- every page URL includes the locale (e.g., /en-US/dashboard, /zh-CN/dashboard).
  • Ignored paths -- /api, /__ (internal), and /llms (LLM middleware) paths skip locale processing entirely.

Message Files

Translation messages are stored as TypeScript objects in packages/i18n/src/messages/:

The AppMessages type is inferred from the en-US messages file, ensuring all locales have the same shape.

Usage in Components

import { useTranslations } from "use-intl"

function WelcomePage() {
  const t = useTranslations("WelcomePage")
  return <h1>{t("title")}</h1>
}

Server-Side Request Config

For server components and server functions, use getRequestConfig:

import { getRequestConfig } from "@raypx/i18n"

export async function loader({ request }: { request: Request }) {
  const config = getRequestConfig(request)
  // { locale: "en-US", messages: {...}, timeZone: "America/New_York" }
}

Each locale maps to a default time zone:

LocaleTime Zone
en-USAmerica/New_York
zh-CNAsia/Shanghai

On this page