@@ -12,15 +12,27 @@ import {InternalDispatch, sub} from '@clayui/shared';
12
12
import classNames from 'classnames' ;
13
13
import React from 'react' ;
14
14
15
+ type DisplayType = 'info' | 'secondary' | 'success' | 'warning' ;
16
+
15
17
type Item = {
16
18
href ?: string ;
17
19
id : string ;
18
20
label : string ;
19
21
symbol : string ;
20
22
} ;
21
23
24
+ type Labels = {
25
+ default : string ;
26
+ trigger : string ;
27
+ translated : string ;
28
+ translating : string ;
29
+ untranslated : string ;
30
+ } ;
31
+
32
+ type Translation = { total : number ; translated : number } ;
33
+
22
34
type Translations = {
23
- [ key : string ] : string ;
35
+ [ key : string ] : Translation ;
24
36
} ;
25
37
26
38
type Props = {
@@ -49,12 +61,7 @@ type Props = {
49
61
/**
50
62
* Labels for the component
51
63
*/
52
- labels ?: {
53
- default : string ;
54
- trigger : string ;
55
- translated : string ;
56
- untranslated : string ;
57
- } ;
64
+ labels ?: Labels ;
58
65
59
66
/**
60
67
* List of locales to allow localization for
@@ -93,6 +100,40 @@ type Props = {
93
100
translations ?: Translations ;
94
101
} ;
95
102
103
+ const TranslationLabel = ( {
104
+ defaultLanguage,
105
+ labels,
106
+ locale,
107
+ translation,
108
+ } : {
109
+ defaultLanguage : Item ;
110
+ labels : Labels ;
111
+ locale : Item ;
112
+ translation : Translation ;
113
+ } ) => {
114
+ let displayType : DisplayType = 'warning' ;
115
+ let label = labels . untranslated ;
116
+
117
+ if ( locale . label === defaultLanguage ?. label ) {
118
+ displayType = 'info' ;
119
+ label = labels . default ;
120
+ } else if ( translation ) {
121
+ const { total, translated} = translation ;
122
+
123
+ if ( total && total === translated ) {
124
+ displayType = 'success' ;
125
+ label = labels . translated ;
126
+ } else {
127
+ displayType = 'secondary' ;
128
+ label = sub ( labels . translating , [ translated , total ] ) ;
129
+ }
130
+ }
131
+
132
+ return < ClayLabel displayType = { displayType } > { label } </ ClayLabel > ;
133
+ } ;
134
+
135
+ TranslationLabel . displayName = 'Label' ;
136
+
96
137
const Trigger = React . forwardRef < HTMLButtonElement > (
97
138
(
98
139
{
@@ -144,6 +185,7 @@ const ClayLanguagePicker = ({
144
185
labels = {
145
186
default : 'Default' ,
146
187
translated : 'Translated' ,
188
+ translating : 'Translating {0}/{1}' ,
147
189
trigger : 'Select a language, current language: {0}.' ,
148
190
untranslated : 'Untranslated' ,
149
191
} ,
@@ -201,23 +243,14 @@ const ClayLanguagePicker = ({
201
243
{ hasTranslations ? (
202
244
< ClayLayout . ContentCol containerElement = "span" >
203
245
< ClayLayout . ContentSection >
204
- < ClayLabel
205
- displayType = {
206
- locale . label ===
207
- defaultLanguage ?. label
208
- ? 'info'
209
- : translations [ locale . label ]
210
- ? 'success'
211
- : 'warning'
246
+ < TranslationLabel
247
+ defaultLanguage = { defaultLanguage ! }
248
+ labels = { labels }
249
+ locale = { locale }
250
+ translation = {
251
+ translations [ locale . label ] !
212
252
}
213
- >
214
- { locale . label ===
215
- defaultLanguage ?. label
216
- ? labels . default
217
- : translations [ locale . label ]
218
- ? labels . translated
219
- : labels . untranslated }
220
- </ ClayLabel >
253
+ />
221
254
</ ClayLayout . ContentSection >
222
255
</ ClayLayout . ContentCol >
223
256
) : null }
0 commit comments