-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: souyahia-monk <samy.ouyahia@monkvision.ai>
- Loading branch information
1 parent
e9f64e8
commit 8ed5856
Showing
2 changed files
with
53 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,67 @@ | ||
import { useMonitoring } from '@monkvision/corejs'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
import React, { useCallback, useState } from 'react'; | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ActivityIndicator, Button, Menu } from 'react-native-paper'; | ||
import { useMonitoring } from '@monkvision/corejs'; | ||
import { StyleSheet } from 'react-native'; | ||
import { Dropdown } from 'react-native-element-dropdown'; | ||
|
||
export const ASYNC_STORAGE_LANG_KEY = '@lang_Storage'; | ||
|
||
const options = [ | ||
{ label: '🇬🇧 English', value: 'en' }, | ||
{ label: '🇫🇷 French', value: 'fr' }, | ||
]; | ||
|
||
const styles = StyleSheet.create({ | ||
dropdown: { | ||
margin: 16, | ||
height: 50, | ||
borderBottomColor: 'gray', | ||
borderBottomWidth: 0.5, | ||
cursor: 'pointer', | ||
}, | ||
selectedTextStyle: { | ||
fontSize: 14, | ||
color: '#efefef', | ||
}, | ||
iconStyle: { | ||
width: 20, | ||
height: 20, | ||
}, | ||
containerStyle: { | ||
backgroundColor: '#313240', | ||
color: '#efefef', | ||
}, | ||
itemTextStyle: { | ||
color: '#efefef', | ||
}, | ||
}); | ||
|
||
export default function LanguageSwitch() { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [visible, setVisible] = React.useState(false); | ||
const { errorHandler } = useMonitoring(); | ||
const { i18n } = useTranslation(); | ||
|
||
const openMenu = () => setVisible(true); | ||
const closeMenu = () => setVisible(false); | ||
|
||
const setLanguage = (lng) => { | ||
setIsLoading(true); | ||
closeMenu(); | ||
i18n.changeLanguage(lng) | ||
.then(() => AsyncStorage.setItem(ASYNC_STORAGE_LANG_KEY, lng)) | ||
.then(() => setIsLoading(false)) | ||
.catch((err) => { | ||
setIsLoading(false); | ||
errorHandler(err); | ||
}); | ||
.catch((err) => errorHandler(err)); | ||
}; | ||
|
||
const getButtonContent = useCallback(() => { | ||
if (isLoading) { | ||
return <ActivityIndicator animating />; | ||
} | ||
const en = '🇬🇧 ▼'; | ||
const fr = '🇫🇷 ▼'; | ||
if (!i18n.language) { | ||
setLanguage('en'); | ||
return en; | ||
} | ||
return i18n.language.startsWith('fr') ? fr : en; | ||
}, [isLoading, i18n.language]); | ||
|
||
return ( | ||
<Menu | ||
visible={visible} | ||
onDismiss={closeMenu} | ||
anchor={<Button color="white" onPress={openMenu} disabled={isLoading}>{getButtonContent()}</Button>} | ||
> | ||
<Menu.Item onPress={() => setLanguage('en')} title="🇬🇧 English" /> | ||
<Menu.Item onPress={() => setLanguage('fr')} title="🇫🇷 Français" /> | ||
</Menu> | ||
<Dropdown | ||
style={styles.dropdown} | ||
selectedTextStyle={styles.selectedTextStyle} | ||
containerStyle={styles.containerStyle} | ||
itemTextStyle={styles.itemTextStyle} | ||
iconStyle={styles.iconStyle} | ||
activeColor="#424456" | ||
data={options} | ||
labelField="label" | ||
valueField="value" | ||
placeholder="Change language" | ||
value={i18n.language} | ||
onChange={(item) => setLanguage(item.value)} | ||
backgroundColor="#00000080" | ||
/> | ||
); | ||
} |