Guide
Localization
Customize labels and formats for international audiences
Custom Labels
Timepicker-UI allows you to customize all text labels to support any language:
Default Labels
index.tstypescript
1// Default English labels2{3 labels: {4 ok: 'Ok',5 cancel: 'Cancel',6 am: 'AM',7 pm: 'PM'8 }9}
Spanish Localization
index.tstypescript
1const picker = new TimepickerUI(input, {2 labels: {3 ok: 'Aceptar',4 cancel: 'Cancelar',5 am: 'AM',6 pm: 'PM'7 }8});910picker.create();
French Localization
index.tstypescript
1const picker = new TimepickerUI(input, {2 labels: {3 ok: 'Valider',4 cancel: 'Annuler',5 am: 'AM',6 pm: 'PM'7 }8});910picker.create();
German Localization
index.tstypescript
1const picker = new TimepickerUI(input, {2 labels: {3 ok: 'OK',4 cancel: 'Abbrechen',5 am: 'AM',6 pm: 'PM'7 }8});910picker.create();
Language Configuration Object
Create reusable language configuration objects for consistency:
Language Config Files
index.tstypescript
1// locales/es.ts2export const esLocale = {3 labels: {4 ok: 'Aceptar',5 cancel: 'Cancelar',6 am: 'AM',7 pm: 'PM'8 }9};1011// locales/fr.ts12export const frLocale = {13 labels: {14 ok: 'Valider',15 cancel: 'Annuler',16 am: 'AM',17 pm: 'PM'18 }19};2021// locales/de.ts22export const deLocale = {23 labels: {24 ok: 'OK',25 cancel: 'Abbrechen',26 am: 'AM',27 pm: 'PM'28 }29};3031// locales/ja.ts32export const jaLocale = {33 okLabel: '確認',34 cancelLabel: 'キャンセル',35 amLabel: '午前',36 pmLabel: '午後'37};3839// locales/zh.ts40export const zhLocale = {41 okLabel: '确定',42 cancelLabel: '取消',43 amLabel: '上午',44 pmLabel: '下午'45};
Using Language Configs
index.tstypescript
1import TimepickerUI from 'timepicker-ui';2import { esLocale } from './locales/es';34const picker = new TimepickerUI(input, {5 ...esLocale,6 clock: { type: '24h' }7});89picker.create();
Dynamic Language Switching
index.tstypescript
1import { esLocale, frLocale, deLocale } from './locales';23const locales = {4 es: esLocale,5 fr: frLocale,6 de: deLocale7};89function createLocalizedPicker(language: string) {10 const locale = locales[language] || esLocale;1112 const picker = new TimepickerUI(input, {13 ...locale,14 clock: { type: '24h' }15 });1617 return picker;18}1920// Usage21let picker = createLocalizedPicker('es');22picker.create();2324// Switch language25picker.destroy();26picker = createLocalizedPicker('fr');27picker.create();
Time Format Localization
Different regions use different time formats. Configure the clock type based on locale:
12-hour Format (US, UK, Canada)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '12h' },3 labels: {4 ok: 'OK',5 cancel: 'Cancel',6 am: 'AM',7 pm: 'PM'8 }9});
24-hour Format (Europe, Asia, Most of World)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: 'OK',5 cancel: 'Annuler'6 }7 // No am/pm labels needed for 24h format8});
Auto-detect User Locale
index.tstypescript
1function getLocaleTimeFormat() {2 // Get user's locale3 const locale = navigator.language || 'en-US';45 // Test if locale uses 12-hour format6 const testDate = new Date(2000, 0, 1, 13, 0);7 const formatted = testDate.toLocaleTimeString(locale);89 return formatted.includes('PM') || formatted.includes('AM') ? '12h' : '24h';10}1112// Usage13const clockType = getLocaleTimeFormat();14const picker = new TimepickerUI(input, {15 clock: { type: clockType }16});1718picker.create();
Complete Locale Examples
Ready-to-use configurations for common languages:
Japanese (日本語)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: '確認',5 cancel: 'キャンセル'6 }7});
Chinese Simplified (简体中文)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: '确定',5 cancel: '取消'6 }7});
Arabic (العربية)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '12h' },3 labels: {4 ok: 'موافق',5 cancel: 'إلغاء',6 am: 'ص',7 pm: 'م'8 }9});
Russian (Русский)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: 'ОК',5 cancel: 'Отмена'6 }7});
Portuguese (Português)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: 'Confirmar',5 cancel: 'Cancelar'6 }7});
Italian (Italiano)
index.tstypescript
1const picker = new TimepickerUI(input, {2 clock: { type: '24h' },3 labels: {4 ok: 'OK',5 cancel: 'Annulla'6 }7});
React i18n Integration
Integrate with popular i18n libraries:
react-i18next
index.tstypescript
1import { useTranslation } from 'react-i18next';2import { useEffect, useRef } from 'react';3import TimepickerUI from 'timepicker-ui';45function LocalizedTimePicker() {6 const { t, i18n } = useTranslation();7 const inputRef = useRef<HTMLInputElement>(null);8 const pickerRef = useRef<TimepickerUI | null>(null);910 useEffect(() => {11 if (inputRef.current) {12 pickerRef.current = new TimepickerUI(inputRef.current, {13 clock: {14 type: i18n.language.includes('en') ? '12h' : '24h'15 },16 labels: {17 ok: t('timepicker.ok'),18 cancel: t('timepicker.cancel'),19 am: t('timepicker.am'),20 pm: t('timepicker.pm')21 }22 });2324 pickerRef.current.create();25 }2627 return () => {28 pickerRef.current?.destroy();29 };30 }, [t, i18n.language]);3132 return <input ref={inputRef} type="time" />;33}
i18n Translation File
index.jsonjson
1// locales/en/translation.json2{3 "timepicker": {4 "ok": "OK",5 "cancel": "Cancel",6 "am": "AM",7 "pm": "PM"8 }9}1011// locales/es/translation.json12{13 "timepicker": {14 "ok": "Aceptar",15 "cancel": "Cancelar",16 "am": "AM",17 "pm": "PM"18 }19}2021// locales/fr/translation.json22{23 "timepicker": {24 "ok": "Valider",25 "cancel": "Annuler",26 "am": "AM",27 "pm": "PM"28 }29}
Right-to-Left (RTL) Support
Support for RTL languages like Arabic and Hebrew:
index.csscss
1/* Add to your CSS */2[dir="rtl"] .timepicker-ui-wrapper {3 direction: rtl;4}56[dir="rtl"] .timepicker-ui-hour,7[dir="rtl"] .timepicker-ui-minutes {8 text-align: right;9}1011// JavaScript12const picker = new TimepickerUI(input, {13 clock: { type: '12h' },14 labels: {15 ok: 'موافق',16 cancel: 'إلغاء',17 am: 'ص',18 pm: 'م'19 }20});2122picker.create();2324// Set RTL on container25document.querySelector('.timepicker-container').dir = 'rtl';
Contributing Translations
If you'd like to contribute translations for additional languages to the official documentation or create a locale pack, please open a pull request on GitHub. Community translations help make Timepicker-UI accessible to users worldwide.
View on GitHub