Skip to content

Commit 8f6e24a

Browse files
committed
language display and selection
1 parent c379db6 commit 8f6e24a

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

client/packages/common/src/intl/locales/en/app.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"searching": "Searching...",
8181
"select-report": "Select Template",
8282
"select-store": "Select store",
83+
"select-language": "Select language",
8384
"stock": "View Stock",
8485
"stocktakes": "Stocktakes",
8586
"store-details": "Store code: {{code}}",

client/packages/common/src/intl/utils/IntlUtils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { LanguageType } from '../../types/schema';
55

66
export { useTranslationNext };
77

8+
const languageOptions = [
9+
{ label: 'عربي', value: 'ar' },
10+
{ label: 'Français', value: 'fr' },
11+
{ label: 'English', value: 'en' },
12+
{ label: 'Española', value: 'es' },
13+
{ label: 'Tetum', value: 'tet' },
14+
];
15+
816
const locales = [
917
'ar' as const,
1018
'en' as const,
@@ -53,6 +61,9 @@ export const IntlUtils = {
5361
}
5462
return 'en';
5563
},
64+
languageOptions,
65+
getLanguageName: (language: string) =>
66+
languageOptions.find(option => option.value === language)?.label,
5667
};
5768

5869
const parseLanguage = (language?: LanguageType) => {

client/packages/host/src/components/Footer/Footer.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import React from 'react';
22
import {
33
Box,
44
HomeIcon,
5+
IntlUtils,
56
styled,
67
Tooltip,
8+
TranslateIcon,
79
Typography,
810
useAuthContext,
911
UserIcon,
1012
useTranslation,
1113
} from '@openmsupply-client/common';
1214
import { StoreSelector } from './StoreSelector';
15+
import { LanguageSelector } from './LanguageSelector';
1316

1417
export const Footer: React.FC = () => {
1518
const { user, store } = useAuthContext();
1619
const t = useTranslation('app');
20+
const i18n = IntlUtils.useI18N();
1721
const PaddedCell = styled(Box)({ display: 'flex' });
1822
const iconStyles = { color: 'gray.main', height: '16px', width: '16px' };
1923
const textStyles = {
@@ -38,6 +42,16 @@ export const Footer: React.FC = () => {
3842
<Typography sx={textStyles}>{user.name}</Typography>
3943
</PaddedCell>
4044
) : null}
45+
<LanguageSelector>
46+
<PaddedCell>
47+
<TranslateIcon sx={iconStyles} />
48+
<Tooltip title={t('select-language', { ...store })}>
49+
<Typography sx={textStyles}>
50+
{IntlUtils.getLanguageName(i18n.language)}
51+
</Typography>
52+
</Tooltip>
53+
</PaddedCell>
54+
</LanguageSelector>
4155
</Box>
4256
);
4357
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, { FC } from 'react';
2+
import {
3+
Box,
4+
FlatButton,
5+
PaperPopoverSection,
6+
usePaperClickPopover,
7+
useTranslation,
8+
IntlUtils,
9+
useNavigate,
10+
} from '@openmsupply-client/common';
11+
12+
import { PropsWithChildrenOnly } from '@common/types';
13+
14+
export const LanguageSelector: FC<PropsWithChildrenOnly> = ({ children }) => {
15+
const navigate = useNavigate();
16+
const { hide, PaperClickPopover } = usePaperClickPopover();
17+
const t = useTranslation('app');
18+
19+
const i18n = IntlUtils.useI18N();
20+
21+
const languageButtons = IntlUtils.languageOptions.map(l => (
22+
<FlatButton
23+
label={l.label}
24+
disabled={l.value === i18n.language}
25+
onClick={() => {
26+
i18n.changeLanguage(l.value);
27+
hide();
28+
navigate(0);
29+
}}
30+
key={l.value}
31+
sx={{
32+
whiteSpace: 'nowrap',
33+
overflowX: 'hidden',
34+
overflowY: 'visible',
35+
textOverflow: 'ellipsis',
36+
display: 'block',
37+
textAlign: 'left',
38+
}}
39+
/>
40+
));
41+
return (
42+
<PaperClickPopover
43+
placement="top"
44+
width={300}
45+
Content={
46+
<PaperPopoverSection label={t('select-language')}>
47+
<Box
48+
style={{
49+
overflowY: 'auto',
50+
maxHeight: 300,
51+
}}
52+
>
53+
{languageButtons}
54+
</Box>
55+
</PaperPopoverSection>
56+
}
57+
>
58+
{children}
59+
</PaperClickPopover>
60+
);
61+
};

client/packages/host/src/components/LanguageMenu.tsx

+1-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ export const LanguageMenu: React.FC = () => {
1616
navigate(0);
1717
};
1818

19-
const options = [
20-
{ label: 'عربي', value: 'ar' },
21-
{ label: 'Français', value: 'fr' },
22-
{ label: 'English', value: 'en' },
23-
{ label: 'Española', value: 'es' },
24-
{ label: 'Tetum', value: 'tet' },
25-
];
26-
2719
const renderOption = (option: Option) => (
2820
<MenuItem
2921
key={option.value}
@@ -37,7 +29,7 @@ export const LanguageMenu: React.FC = () => {
3729
return (
3830
<Select
3931
onChange={handleChange}
40-
options={options}
32+
options={IntlUtils.languageOptions}
4133
value={i18n.language}
4234
renderOption={renderOption}
4335
/>

0 commit comments

Comments
 (0)