Overview
React UI Kit provides multi-language localization to adapt the UI elements based on the user’s preferred language settings. The CometChatLocalize class allows developers to:- Automatically detect and apply a language based on browser/device settings.
- Manually change the UI language.
- Format date and time based on localization settings.
The localization system now includes language JSON files, which store translations, and an improved CometChatLocalize class, which handles language detection and formatting.
Supported Languages
React UI Kit currently supports 19 languages:Language | Code |
---|---|
English (United States) | en-US |
English (United Kingdom) | en-GB |
Dutch | nl |
French | fr |
German | de |
Hindi | hi |
Italian | it |
Japanese | ja |
Korean | ko |
Portuguese | pt |
Russian | ru |
Spanish | es |
Turkish | tr |
Chinese | zh |
Chinese (Traditional) | zh-TW |
Malay | ms |
Swedish | sv |
Lithuanian | lt |
Hungarian | hu |
➡ Language JSON Files
CometChatLocalize
TheCometChatLocalize
class provides methods for managing localization in the UI Kit.
🔗 View full class file in the GitHub repository:➡ CometChatLocalize
LocalizationSettings
TheLocalizationSettings
interface defines various localization settings for an application or component. It allows developers to configure the language, translations, time zone, and calendar formatting while providing options for automatic detection and missing key handling.
Property | Type | Description |
---|---|---|
language | string | The language code (e.g., "en" , "fr" ) for the current localization. |
translationsForLanguage | { [key: string]: any } | An object containing key-value pairs for translations in the current language. |
disableAutoDetection | boolean | Disables automatic language detection based on the browser or device settings. |
fallbackLanguage | string | The fallback language code to use if the primary language is not available. |
disableDateTimeLocalization | boolean | Disables localization for date and time values, forcing the default format. |
timezone | string | The timezone used for date and time formatting (e.g., "America/New_York" , "Europe/London" ). |
calendarObject | CalendarObject | A custom calendar format using CalendarObject to define localized date and time formatting. |
missingKeyHandler | (key: string) => string | A function that handles missing translation keys, allowing custom error handling or fallbacks. |
Example
CalendarObject
TheCalendarObject
class defines customizable formatting for date and time representation. It allows you to format dates based on whether they are today, yesterday, last week, or other days. It also supports relative time formatting for minutes and hours.
NoticeChanging this format will globally update the date and time representation wherever it is used in the component.
However, if a component-specific
However, if a component-specific
CalendarObject
is provided, it will take higher precedence over the global settings.Property | Type | Description |
---|---|---|
today | string | Custom formatting for dates that fall on the same day. Example: "Today at hh:mm A" |
yesterday | string | Custom formatting for dates that fall on the previous day. Example: "Yesterday at hh:mm A" |
lastWeek | string | Custom formatting for dates within the last 7 days. Example: "Last week on dddd" |
otherDays | string | Custom formatting for dates that do not fit other categories. Example: "DD MMM YYYY, hh:mm A" |
relativeTime | object | Custom formatting for relative time expressions (e.g., “2 hours ago”). |
relativeTime.minute | string | Formatting for a single minute. Example: "%d minute ago" |
relativeTime.minutes | string | Formatting for multiple minutes. Example: "%d minutes ago" |
relativeTime.hour | string | Formatting for a single hour. Example: "%d hour ago" |
relativeTime.hours | string | Formatting for multiple hours. Example: "%d hours ago" |
Example
Methods
Initialize CometChatLocalize
This method initializes the localization system with default values and optional configurations. Usage- Set the default language, timezone, and fallback settings.
- Define a custom calendar format if required.
- Customize how missing keys are handled.
Get Browser Language
This method detects the language set in the user’s browser or device settings. Usage- Automatically set the app’s language based on the user’s browser settings.
- Helps in making the UI multilingual without requiring user input.
Get Localized String
This method fetches localized text based on the current language. Usage- Retrieve translations dynamically without hardcoding values in multiple languages.
- Useful for UI elements, buttons, alerts, and system messages.
Get Current Language
This method returns the currently set language for the UI Kit. Usage- Useful to debug and display the currently active language.
- Helps when dynamically switching between languages.
Get Default Language
This method returns the system-preferred language. Usage- If disableAutoDetection is enabled, the method returns the fallback language.
- If auto-detection is enabled, it returns the browser’s preferred language.
Set Current Language
This method updates the language at runtime without reloading the application. Usage- Allow users to change the language via a settings menu.
- Ensure that UI elements are updated instantly after changing the language.
Add Custom Translations
This method allows you to add custom translations to the existing ones dynamically. It ensures that new translations are merged into the existing localization data. Usage- You can define custom translation keys and override the existing translations.
- You can add new languages to the existing translations.
- Useful when you want to support additional words or phrases not present in the default language files.
Customisation
CometChat UI Kit provides flexible customization options for date/time format. Users can configure global settings usingCometChatLocalize.init()
or pass a CalendarObject
directly to individual components for component-specific customization.
- Global Configuration: When settings are provided in
CometChatLocalize.init()
, all UI components will automatically use the configured date/time formats. - Component-Specific Configuration: If a
CalendarObject
is passed to a component, it overrides the global settings and applies only to that specific instance.