Standalone Functions
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
While createI18n() is the recommended setup, all the underlying functions are exported individually. This is useful for custom build scripts, testing, or composing your own integration.
createTranslator(translations, defaultLocale, i18nextConfig?)
Initializes i18next and returns a translation function.
import { createTranslator } from '@otrodigital/astro-i18n-next';
const t = createTranslator( { en: enJson, es: esJson }, 'en');
t('en', 'nav.about'); // "About"t('es', 'nav.about'); // "Acerca de"t('es', 'missing.key'); // Falls back to English valueFeatures:
- Supports dot-notation keys (
'section.key') - Falls back to
defaultLocalefor missing translations - HTML is not escaped (safe for
set:html) - Creates an isolated i18next instance (safe for multiple calls)
createRouteHelpers(defaultLocale, locales, slugMaps)
Returns localePath, switchLocalePath, and getLocaleFromPath. See Route Helpers.
import { createRouteHelpers } from '@otrodigital/astro-i18n-next';
const { localePath, switchLocalePath, getLocaleFromPath } = createRouteHelpers('en', ['en', 'es'], slugMaps);createSlugResolver(slugMaps, defaultLocale)
Returns getLocalizedSlug, getCanonicalSlug, and getAllSlugPairs. See Slug Resolver.
import { createSlugResolver } from '@otrodigital/astro-i18n-next';
const { getLocalizedSlug, getCanonicalSlug } = createSlugResolver(slugMaps, 'en');createContentHelper(defaultLocale)
Returns the localized() function.
import { createContentHelper } from '@otrodigital/astro-i18n-next';
const localized = createContentHelper('en');localized({ en: 'Hello', es: 'Hola' }, 'es'); // "Hola"createI18nIntegration({ config, pages })
Low-level Astro integration that configures i18n routing. Used internally by createI18n.
import { createI18nIntegration } from '@otrodigital/astro-i18n-next';
const integration = createI18nIntegration({ config: { defaultLocale: 'en', locales: ['en', 'es'], ... }, pages: discoveredPages,});What it does:
- Configures Astro’s built-in i18n settings
- Injects routes for every non-default locale using
injectRoute()
loadPageMapSync(pagesDir, locales)
Synchronously scans a pages directory for .astro files and builds the page map.
import { loadPageMapSync } from '@otrodigital/astro-i18n-next';
const { pages, pageSlugMap } = loadPageMapSync('src/pages', ['en', 'es']);pagesDiris a relative path from the project root- Skips dynamic routes (files/directories containing
[) - Extracts
export const slugs = { ... }from page frontmatter
loadSlugMapSync(contentDir)
Synchronously reads markdown files and extracts slug maps from YAML frontmatter.
import { join } from 'node:path';import { loadSlugMapSync } from '@otrodigital/astro-i18n-next';
const slugMap = loadSlugMapSync(join(process.cwd(), 'src/content/saunas'));// { 'model-165': { en: 'model-165', es: 'modelo-165' }, ... }contentDirmust be an absolute path- Reads
.mdfiles only - Extracts the
slugsfield from YAML frontmatter