Skip to content

Commit f32db5b

Browse files
committed
feat(@clayui/language-picker): LPD-45714 Add translations
1 parent cc668ff commit f32db5b

File tree

4 files changed

+138
-27
lines changed

4 files changed

+138
-27
lines changed

packages/clay-language-picker/src/__tests__/__snapshots__/index.tsx.snap

+28
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,31 @@ exports[`ClayLanguagePicker renders with custom labels 1`] = `
140140
</button>
141141
</div>
142142
`;
143+
144+
exports[`ClayLanguagePicker renders with translations 1`] = `
145+
<div>
146+
<button
147+
aria-activedescendant=""
148+
aria-expanded="false"
149+
aria-haspopup="listbox"
150+
aria-label="Select a language, current language: en-US."
151+
class="form-control-select btn btn-secondary"
152+
role="combobox"
153+
tabindex="0"
154+
type="button"
155+
>
156+
<span
157+
class="inline-item-before"
158+
>
159+
<svg
160+
class="lexicon-icon lexicon-icon-en-us"
161+
role="presentation"
162+
>
163+
<use
164+
href="/path/to/svg#en-us"
165+
/>
166+
</svg>
167+
</span>
168+
</button>
169+
</div>
170+
`;

packages/clay-language-picker/src/__tests__/index.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ describe('ClayLanguagePicker', () => {
119119
expect(container).toMatchSnapshot();
120120
});
121121

122+
it('renders with translations', () => {
123+
const {container} = render(
124+
<ClayLanguagePicker
125+
locales={locales}
126+
onSelectedLocaleChange={onSelectedLocaleChange}
127+
selectedLocale={locales[0]!}
128+
spritemap="/path/to/svg"
129+
translations={{
130+
'en-US': 'Apple',
131+
'es-ES': 'Manzana',
132+
}}
133+
/>
134+
);
135+
136+
expect(container).toMatchSnapshot();
137+
});
138+
122139
it('calls onSelectedLocaleChange when a language is selected', () => {
123140
render(
124141
<ClayLanguagePicker

packages/clay-language-picker/src/index.tsx

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ClayButton from '@clayui/button';
77
import {Option, Picker} from '@clayui/core';
88
import ClayIcon from '@clayui/icon';
9+
import ClayLabel from '@clayui/label';
910
import ClayLayout from '@clayui/layout';
1011
import {InternalDispatch, sub} from '@clayui/shared';
1112
import classNames from 'classnames';
@@ -18,6 +19,10 @@ type Item = {
1819
symbol: string;
1920
};
2021

22+
type Translations = {
23+
[key: string]: string;
24+
};
25+
2126
type Props = {
2227
/**
2328
* Flag to indicate if the Picker is active or not (controlled).
@@ -75,6 +80,11 @@ type Props = {
7580
* Path to the location of the spritemap resource.
7681
*/
7782
spritemap?: string;
83+
84+
/**
85+
* Translations provided to the component to be used and modified by it
86+
*/
87+
translations?: Translations;
7888
};
7989

8090
const Trigger = React.forwardRef<HTMLButtonElement>(
@@ -127,7 +137,11 @@ const ClayLanguagePicker = ({
127137
selectedLocale,
128138
small,
129139
spritemap,
140+
translations = {},
130141
}: Props) => {
142+
const defaultLanguage = locales[0];
143+
const hasTranslations = Object.keys(translations).length;
144+
131145
return (
132146
<Picker
133147
active={active}
@@ -163,6 +177,29 @@ const ClayLanguagePicker = ({
163177
{locale.label}
164178
</ClayLayout.ContentSection>
165179
</ClayLayout.ContentCol>
180+
{hasTranslations ? (
181+
<ClayLayout.ContentCol containerElement="span">
182+
<ClayLayout.ContentSection>
183+
<ClayLabel
184+
displayType={
185+
locale.label ===
186+
defaultLanguage?.label
187+
? 'info'
188+
: translations[locale.label]
189+
? 'success'
190+
: 'warning'
191+
}
192+
>
193+
{locale.label ===
194+
defaultLanguage?.label
195+
? labels.default
196+
: translations[locale.label]
197+
? labels.translated
198+
: labels.untranslated}
199+
</ClayLabel>
200+
</ClayLayout.ContentSection>
201+
</ClayLayout.ContentCol>
202+
) : null}
166203
</ClayLayout.ContentRow>
167204
</Option>
168205
);

packages/clay-language-picker/stories/LanguagePicker.stories.tsx

+56-27
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ export default {
1212
title: 'Design System/Components/LanguagePicker',
1313
};
1414

15-
export const Default = (args: any) => {
16-
const locales = [
17-
{
18-
displayName: 'English (United States)',
19-
id: 'en_US',
20-
label: 'en-US',
21-
symbol: 'en-us',
22-
},
23-
{
24-
displayName: 'Arabic (Saudi Arabia)',
25-
id: 'ar_SA',
26-
label: 'ar-SA',
27-
symbol: 'ar-sa',
28-
},
29-
{
30-
displayName: 'Catalan (Spain)',
31-
id: 'ca_ES',
32-
label: 'ca-ES',
33-
symbol: 'ca-es',
34-
},
35-
{
36-
displayName: 'Dutch (Netherlands)',
37-
id: 'nl_NL',
38-
label: 'nl-NL',
39-
symbol: 'nl-nl',
40-
},
41-
];
15+
const locales = [
16+
{
17+
displayName: 'English (United States)',
18+
id: 'en_US',
19+
label: 'en-US',
20+
symbol: 'en-us',
21+
},
22+
{
23+
displayName: 'Arabic (Saudi Arabia)',
24+
id: 'ar_SA',
25+
label: 'ar-SA',
26+
symbol: 'ar-sa',
27+
},
28+
{
29+
displayName: 'Catalan (Spain)',
30+
id: 'ca_ES',
31+
label: 'ca-ES',
32+
symbol: 'ca-es',
33+
},
34+
{
35+
displayName: 'Dutch (Netherlands)',
36+
id: 'nl_NL',
37+
label: 'nl-NL',
38+
symbol: 'nl-nl',
39+
},
40+
];
4241

42+
export const Default = (args: any) => {
4343
const [selectedLocale, setSelectedLocale] = useState<any>(locales[0]);
4444

4545
return (
@@ -62,3 +62,32 @@ export const Default = (args: any) => {
6262
Default.args = {
6363
small: false,
6464
};
65+
66+
export const LanguagePickerWithTranslations = (args: any) => {
67+
const [selectedLocale, setSelectedLocale] = useState<any>(locales[0]);
68+
const translations = {
69+
'en-US': 'Apple',
70+
'es-ES': 'Manzana',
71+
};
72+
73+
return (
74+
<ClayLanguagePicker
75+
id="languagePicker"
76+
locales={locales}
77+
onSelectedLocaleChange={(id) => {
78+
const locale = locales.find((locale) => locale.id === id);
79+
80+
if (locale) {
81+
setSelectedLocale(locale);
82+
}
83+
}}
84+
selectedLocale={selectedLocale}
85+
small={args.small}
86+
translations={translations}
87+
/>
88+
);
89+
};
90+
91+
LanguagePickerWithTranslations.args = {
92+
small: false,
93+
};

0 commit comments

Comments
 (0)