Bridge Training
i18n

Validation

Language parsing, validation, and selector option utilities.

Import from @workspace/i18n/validation.

isValidLanguage

Type guard that checks if a string is a valid LanguageCode from ALL_LANGUAGE_CODES.

import { isValidLanguage } from '@workspace/i18n/validation';

isValidLanguage("fr"); // true
isValidLanguage("xx"); // false

if (isValidLanguage(input)) {
  // input is narrowed to LanguageCode
}

parseLanguage

Parses a language string and returns a validated language code. Generic over the allowed codes, so it works with app-specific subsets.

import { parseLanguage } from '@workspace/i18n/validation';
import { ALL_LANGUAGE_CODES } from '@workspace/i18n/constants';

// With a subset (apps/app — only fr, en)
const APP_LANGUAGES = ['fr', 'en'] as const;
parseLanguage("en", "fr", APP_LANGUAGES);  // "en"
parseLanguage("de", "fr", APP_LANGUAGES);  // "fr" (not in subset → fallback)
parseLanguage(null, "fr", APP_LANGUAGES);  // "fr"

// With all languages (apps/web)
parseLanguage("de", "fr", ALL_LANGUAGE_CODES); // "de"
ParameterTypeDescription
langstring | undefined | nullThe language string to parse
fallbackTFallback language if input is invalid
allowedCodesreadonly T[]Allowed language codes for this app

getLanguageSelectorOptions

Generates an array of { value, label } options for language dropdowns, using native language names from LANGUAGE_NAMES.

import { getLanguageSelectorOptions } from '@workspace/i18n/validation';

const options = getLanguageSelectorOptions(['fr', 'en'] as const);
// [
//   { value: 'fr', label: 'Français' },
//   { value: 'en', label: 'English' },
// ]

ALL_LANGUAGE_SELECTOR_OPTIONS

Pre-computed selector options for all 10 supported languages. Use this when your app supports all languages to avoid repeated computation.

import { ALL_LANGUAGE_SELECTOR_OPTIONS } from '@workspace/i18n/validation';

// Ready to pass directly to <LanguageSelect />
<LanguageSelect languages={ALL_LANGUAGE_SELECTOR_OPTIONS} ... />

replaceLocaleInPathname

Replaces the locale segment in a URL pathname. Assumes the pattern /{locale}/rest/of/path.

import { replaceLocaleInPathname } from '@workspace/i18n/validation';

replaceLocaleInPathname("/fr/about", "en");       // "/en/about"
replaceLocaleInPathname("/de/docs/intro", "fr");   // "/fr/docs/intro"

Throws an Error if the pathname doesn't start with / or has no locale segment.

On this page