Đặt câu hỏi và nhận tóm tắt tài liệu bằng cách tham chiếu trang này và nhà cung cấp AI bạn chọn
Bằng cách tích hợp Intlayer MCP Server vào trợ lý AI ưa thích của bạn, bạn có thể truy xuất toàn bộ tài liệu trực tiếp từ ChatGPT, DeepSeek, Cursor, VSCode, v.v.
Xem tài liệu MCP ServerLịch sử phiên bản
- Add locale mapper documentationv5.7.227/7/2025
Nội dung của trang này đã được dịch bằng AI.
Xem phiên bản mới nhất của nội dung gốc bằng tiếng AnhNếu bạn có ý tưởng để cải thiện tài liệu này, vui lòng đóng góp bằng cách gửi pull request trên GitHub.
Liên kết GitHub tới tài liệuSao chép Markdown của tài liệu vào bộ nhớ tạm
Locale Mapper
The Locale Mapper is a powerful utility that helps you work with internationalization data in your Intlayer application. It provides three main functions to transform and organize locale-specific data: localeMap, localeFlatMap, and localeRecord.
How Locale Mapper Works
The Locale Mapper operates on a LocaleData object that contains all the necessary information about a locale:
type LocaleData = { locale: LocalesValues; // Current locale code (e.g., 'en', 'fr') defaultLocale: LocalesValues; // Default locale code isDefault: boolean; // Whether this is the default locale locales: LocalesValues[]; // Array of all available locales urlPrefix: string; // URL prefix for this locale (e.g., '/fr' or '')};The mapper functions automatically generate this data for each locale in your configuration, taking into account:
- Your configured locales list
- The default locale setting
- Whether the default locale should be prefixed in URLs
Core Functions
localeMap
Transforms each locale into a single object using a mapper function.
localeMap<T>( mapper: (locale: LocaleData) => T, locales?: LocalesValues[], defaultLocale?: LocalesValues, prefixDefault?: boolean): T[]Example: Creating route objects
import { localeMap } from "intlayer";const routes = localeMap((localizedData) => ({ path: localizedData.urlPrefix, name: localizedData.locale, isDefault: localizedData.isDefault, locales: localizedData.locales, defaultLocale: localizedData.defaultLocale,}));// Result:// [// { path: '/', name: 'en', isDefault: true, locales: ['en', 'fr', 'es'], defaultLocale: 'en' },// { path: '/fr', name: 'fr', isDefault: false, locales: ['en', 'fr', 'es'], defaultLocale: 'en' },// { path: '/es', name: 'es', isDefault: false, locales: ['en', 'fr', 'es'], defaultLocale: 'en' }// ]localeFlatMap
Similar to localeMap, but the mapper function returns an array of objects that gets flattened into a single array.
localeFlatMap<T>( mapper: (locale: LocaleData) => T[], locales?: LocalesValues[], defaultLocale?: LocalesValues, prefixDefault?: boolean): T[]Example: Creating multiple routes per locale
import { localeFlatMap } from "intlayer";const routes = localeFlatMap((localizedData) => [ { path: localizedData.urlPrefix, name: localizedData.locale, isDefault: localizedData.isDefault, }, { path: `${localizedData.urlPrefix}/about`, name: `${localizedData.locale}-about`, isDefault: localizedData.isDefault, },]);// Result:// [// { path: '/', name: 'en', isDefault: true },// { path: '/about', name: 'en-about', isDefault: true },// { path: '/fr', name: 'fr', isDefault: false },// { path: '/fr/about', name: 'fr-about', isDefault: false },// { path: '/es', name: 'es', isDefault: false },// { path: '/es/about', name: 'es-about', isDefault: false }// ]localeRecord
Creates a record object where each locale is a key mapping to a value transformed by the mapper function.
localeRecord<T>( mapper: (locale: LocaleData) => T, locales?: Locales[], defaultLocale?: Locales, prefixDefault?: boolean): Record<Locales, T>Example: Loading translation files
import { localeRecord } from "intlayer";const translations = localeRecord(({ locale }) => require(`./translations/${locale}.json`));// Result:// {// en: { welcome: 'Welcome', hello: 'Hello' },// fr: { welcome: 'Bienvenue', hello: 'Bonjour' },// es: { welcome: 'Bienvenido', hello: 'Hola' }// }Setting Up Locale Mapper
The Locale Mapper automatically uses your Intlayer configuration, but you can override the defaults by passing parameters:
Using Default Configuration
import { localeMap } from "intlayer";// Uses configuration from intlayer.config.tsconst routes = localeMap((data) => ({ path: data.urlPrefix, locale: data.locale,}));Overriding Configuration
import { localeMap } from "intlayer";// Override locales and default localeconst customRoutes = localeMap( (data) => ({ path: data.urlPrefix, locale: data.locale }), ["en", "fr", "de"], // Custom locales "en", // Custom default locale true // Prefix default locale in URLs);Advanced Usage Examples
Creating Navigation Menus
import { localeMap } from "intlayer";const navigationItems = localeMap((data) => ({ label: data.locale.toUpperCase(), href: data.urlPrefix || "/", isActive: data.isDefault, flag: `/flags/${data.locale}.svg`,}));Generating Sitemap Data
import { localeFlatMap } from "intlayer";const sitemapUrls = localeFlatMap((data) => [ { url: `${data.urlPrefix}/`, lastmod: new Date().toISOString(), changefreq: "daily", priority: data.isDefault ? 1.0 : 0.8, }, { url: `${data.urlPrefix}/about`, lastmod: new Date().toISOString(), changefreq: "monthly", priority: data.isDefault ? 0.8 : 0.6, },]);Dynamic Translation Loading
import { localeRecord } from "intlayer";const translationModules = localeRecord(({ locale }) => ({ messages: import(`./locales/${locale}/messages.json`), validation: import(`./locales/${locale}/validation.json`), metadata: { locale, direction: ["ar", "he", "fa"].includes(locale) ? "rtl" : "ltr", },}));Configuration Integration
The Locale Mapper seamlessly integrates with your Intlayer configuration:
- Locales: Automatically uses configuration.internationalization.locales
- Default Locale: Uses configuration.internationalization.defaultLocale
- URL Prefixing: Respects configuration.middleware.prefixDefault
This ensures consistency across your application and reduces configuration duplication.