@@ -8,7 +8,16 @@ Basically a drop-down to change the language (i18n localization)
8
8
*/
9
9
10
10
import { DownOutlined } from "@ant-design/icons" ;
11
- import { Button , Dropdown , MenuProps , Modal , Space , Tooltip } from "antd" ;
11
+ import {
12
+ Button ,
13
+ Dropdown ,
14
+ MenuProps ,
15
+ Modal ,
16
+ Select ,
17
+ SelectProps ,
18
+ Space ,
19
+ Tooltip ,
20
+ } from "antd" ;
12
21
import { SizeType } from "antd/es/config-provider/SizeContext" ;
13
22
import { useState } from "react" ;
14
23
import { defineMessage , useIntl } from "react-intl" ;
@@ -54,6 +63,50 @@ Thank you for your patience and understanding as we work to make our application
54
63
description : "Content of translation information modal" ,
55
64
} ) ;
56
65
66
+ interface LanguageSelectorProps
67
+ extends Omit < SelectProps , "options" | "onChange" > {
68
+ value ?: string ;
69
+ onChange ?: ( language : Locale ) => void ;
70
+ }
71
+
72
+ /**
73
+ * A reusable language selector component for translation purposes.
74
+ */
75
+ export function LanguageSelector ( {
76
+ value,
77
+ onChange,
78
+ ...props
79
+ } : LanguageSelectorProps ) {
80
+ const intl = useIntl ( ) ;
81
+
82
+ let availableLocales = Object . keys ( LOCALIZATIONS ) as Locale [ ] ;
83
+
84
+ const options = availableLocales . map ( ( locale ) => {
85
+ const localization = LOCALIZATIONS [ locale ] ;
86
+ const other =
87
+ locale === value
88
+ ? localization . name
89
+ : intl . formatMessage ( localization . trans ) ;
90
+ return {
91
+ value : locale ,
92
+ label : `${ localization . flag } ${ localization . native } (${ other } )` ,
93
+ } ;
94
+ } ) ;
95
+
96
+ return (
97
+ < Select
98
+ value = { value }
99
+ onChange = { onChange }
100
+ options = { options }
101
+ placeholder = "Select a language..."
102
+ showSearch
103
+ optionFilterProp = "label"
104
+ popupMatchSelectWidth = { false }
105
+ { ...props }
106
+ />
107
+ ) ;
108
+ }
109
+
57
110
export function I18NSelector ( props : Readonly < Props > ) {
58
111
const { isWide = true , size, confirm = false } = props ;
59
112
0 commit comments