واجهات TypeScript
واجهات TypeScript الأساسية التي تصدّرها مكوّنات Babelize Elements: الخصائص، وأنواع اللغات المحلية، وخيارات المنسّق، لكتابة مكوّنات i18n الخاصة بك في React.
نظرة عامة
صُنعت Babelize Elements بدعم كامل لـ TypeScript. يُصدّر كل مكوّن خصائصه وواجهات بياناته الخاصة، فتحصل على أمان أنواع كامل وIntelliSense.
ثبّت المكتبة أو مكوّنًا، ثم استورد الأنواع التي تحتاجها:
من حزمة npm:
import {
LanguageSwitcher,
PhoneInput,
NavBar,
type Locale,
type Country,
type NavLink,
} from "@babelize/elements";أو، إذا نسخت المكوّنات باستخدام CLI:
import { LanguageSwitcher } from "@/components/ui/language-switcher";
import { PhoneInput, type Country } from "@/components/ui/phone-input";
import { NavBar, type NavLink } from "@/components/ui/navbar";
import type { Locale } from "@/components/ui/types";مبدّل اللغة
اللغة المحلية
export interface Locale {
/** ISO 639-1 / BCP 47 code (e.g. "en", "fr-FR", "ar-SA") — required */
code: string;
/** Override display name (auto-resolved from built-in mapping if omitted) */
label?: string;
/** Override flag emoji (auto-detected from code if omitted) */
flag?: string;
/** Override RTL (auto-detected from code if omitted) */
rtl?: boolean;
}LanguageSwitcherProps
export interface LanguageSwitcherProps {
/** Array of available locales — only `code` is required */
locales: Locale[];
/** Selected locale code. Provide this to control the component. */
value?: string;
/** Initial selected locale code when uncontrolled (default: first locale's code) */
defaultValue?: string;
/** Callback when locale changes */
onValueChange?: (code: string) => void;
/** Show flag emojis next to locale names (default: false) */
showFlags?: boolean;
/** Display labels in native language or English (default: "english") */
label?: "native" | "english";
/** Override the built-in English UI text — any key you omit keeps its default */
labels?: Partial<LanguageSwitcherLabels>;
/** Additional CSS classes */
className?: string;
}LanguageSwitcherLabels
export interface LanguageSwitcherLabels {
/** Placeholder for the search field (default: "Search languages...") */
searchPlaceholder: string;
/** Accessible label for the search field (default: "Search languages") */
searchLabel: string;
/** Accessible label for the button that clears the search (default: "Clear search") */
clearSearch: string;
/** Content shown when the search matches nothing (default: "No languages found.") */
emptyMessage: React.ReactNode;
/** Accessible label for the option list (default: "Select language") */
listbox: string;
/** Accessible label for the trigger (default: "Current language: {name}") */
current: (name: string) => string;
}مرّر أي مجموعة جزئية كـ labels، فالمفاتيح التي تحذفها تحتفظ بقيمها الإنجليزية الافتراضية. وينطبق الأمر نفسه على كل واجهة *Labels أدناه.
حقل إدخال الهاتف
الدولة
export interface Country {
/** ISO 3166-1 alpha-2 code (e.g. "US", "GB", "IN") */
code: string;
/** Dial code (e.g. "+1", "+44", "+91") */
dialCode: string;
/** Country name */
name: string;
/** Flag emoji */
flag: string;
}PhoneInputProps
export interface PhoneInputProps {
/** Phone number. Provide this to control the component. */
value?: string;
/** Initial phone number when uncontrolled */
defaultValue?: string;
/** Callback when the number or the selected country changes */
onValueChange?: (phone: string, country: Country) => void;
/** Default country code (default: "US") */
defaultCountry?: string;
/** Placeholder text */
placeholder?: string;
/** Disable the input */
disabled?: boolean;
/** Additional CSS classes */
className?: string;
/** Show country flag next to dial code (default: true) */
showFlags?: boolean;
/** Accessible label for the input */
label?: string;
/** Override the built-in English UI text — any key you omit keeps its default */
labels?: Partial<PhoneInputLabels>;
/** BCP 47 locale used to display country names, e.g. "fr" (default: English names) */
displayLocale?: string;
}PhoneInputLabels
export interface PhoneInputLabels {
/** Accessible label for the country trigger (default: "Select country") */
trigger: string;
/** Placeholder for the country search field (default: "Search countries...") */
searchPlaceholder: string;
/** Accessible label for the country search field (default: "Search countries") */
searchLabel: string;
/** Accessible label for the button that clears the search (default: "Clear search") */
clearSearch: string;
/** Content shown when the search matches nothing (default: "No countries found.") */
emptyMessage: React.ReactNode;
/** Accessible label for the country list (default: "Select country") */
listbox: string;
}NavBar
NavLink
export interface NavLink {
/** Link text — required */
label: string;
/** Link URL — required */
href: string;
}يستخدم
NavBarواجهةLocaleالمشتركة نفسها التي يستخدمهاLanguageSwitcher. الاسم القديم تم حذف الاسم المستعارNavBarLocaleفي1.1.0، فاستوردLocaleبدلًا منه.الاسمان
currentLocale/onLocaleChangeالخاصان بـNavBar، وonChangeالخاص بـPhoneInput/ أُزيلت أسماءshowFlagفي1.1.3. استخدمvalue/onValueChange/showFlags.
NavBarProps
export interface NavBarProps {
/** Logo element (renders before nav links) */
logo?: React.ReactNode;
/** Navigation links */
links?: NavLink[];
/** Available locales for language selector */
locales?: Locale[];
/** Selected locale code. Provide this to control the component. */
value?: string;
/** Initial locale code when uncontrolled (default: first locale's code) */
defaultValue?: string;
/** Callback when locale changes */
onValueChange?: (code: string) => void;
/** Component used to render internal links (default: "a") */
linkComponent?: React.ElementType;
/** CTA button */
cta?: { label: string; href: string };
/** Show GitHub icon button. Needs `githubUrl`; without one, nothing renders */
showGitHub?: boolean;
/** Full URL, or "owner" / "owner/repo" shorthand for github.com */
githubUrl?: string;
/** Make navbar sticky at top (default: true) */
sticky?: boolean;
/** Show flags in language selector (default: true) */
showFlags?: boolean;
/** Override the built-in English UI text — any key you omit keeps its default */
labels?: Partial<NavBarLabels>;
/** Additional CSS classes */
className?: string;
}NavBarLabels
export interface NavBarLabels {
/** Accessible label for the language list (default: "Select language") */
listbox: string;
/** Accessible label for the language menu triggers (default: "Current language: {name}") */
current: (name: string) => string;
/** Accessible label for the mobile menu button (default: "Toggle menu") */
toggleMenu: string;
/** Accessible label for the GitHub icon link (default: "GitHub") */
github: string;
}أداة الترجمة
TranslationWidgetLabels
export interface TranslationWidgetLabels {
/** Accessible label for the swap button (default: "Swap languages") */
swap: string;
/** Accessible label for the copy button (default: "Copy translation") */
copy: string;
/** Accessible label for the copy button right after a copy (default: "Copied") */
copied: string;
/** Screen reader announcement after a copy (default: "Translation copied to clipboard") */
copiedStatus: string;
/** Accessible label for the clear button (default: "Clear source text") */
clearSource: string;
/**
* Screen reader word read after the character count (default: "characters").
* Receives the count, so it can return the right plural form.
*/
characters: (count: number) => string;
/** UI text for both language pickers, passed through as their `labels` */
languageSwitcher?: Partial<LanguageSwitcherLabels>;
}منتقي الإعدادات المحلية
نص واجهة منتقي الإعدادات المحلية.
LocalePickerLabels
export interface LocalePickerLabels {
/** Placeholder for the search field (default: "Search regions...") */
searchPlaceholder: string;
/** Accessible label for the search field (default: "Search regions") */
searchLabel: string;
/** Accessible label for the button that clears the search (default: "Clear search") */
clearSearch: string;
/** Content shown when the search matches nothing (default: "No results") */
emptyMessage: React.ReactNode;
/** Accessible label for the option list (default: "Select region") */
listbox: string;
/** Accessible label for the trigger (default: "Current region: {name}") */
current: (name: string) => string;
/** Summary row label for the currency preview (default: "Currency") */
currency: string;
/** Summary row label for the date preview (default: "Date format") */
dateFormat: string;
/** Summary row label for the number preview (default: "Number format") */
numberFormat: string;
/** Summary row label for the first day of week (default: "Week starts on") */
weekStart: string;
}توسيع الواجهات
لأن مكوّنات Babelize Elements تصبح ملكًا لك بالكامل بعد التثبيت، يمكنك توسيع هذه الواجهات مباشرةً في ملفات المكوّنات. مثلًا، لإضافة حقل مخصص meta إلى كل لغة محلية، أضفه إلى واجهة Locale في نسختك المحلية:
export interface Locale {
code: string;
label?: string;
flag?: string;
rtl?: boolean;
/** Your custom field */
meta?: Record<string, string>;
}