-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement language switching #289
Changes from 14 commits
f4d373a
0f22db8
555079c
35c4125
29e5a5e
eeaf01b
9f87c1b
469bc7d
8e31bad
71b2a82
f520c45
f4577f8
f6c9c9c
0c7b2e2
0b58b11
c3f558b
06d46dc
c085659
3b9b3a8
a6c3074
00e42bd
9f82e5c
33fb7d7
49cc05f
318fcb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
import { ElectricityType } from "carbon-footprint"; | ||
import { locale } from "expo-localization"; | ||
|
||
import SupportedLanguages from "../../screens/Languages/SupportedLanguages"; | ||
|
||
const initialState = { | ||
acceptedTermsOfUseVersion: 0, | ||
activatedNotifications: false, | ||
location: ElectricityType.world, | ||
language: Object.keys(SupportedLanguages).includes(locale.substr(0, 2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Same here) see my other comment on
|
||
? locale.substr(0, 2) | ||
: "en", | ||
}; | ||
|
||
const userPreferences = createSlice({ | ||
|
@@ -20,15 +26,24 @@ const userPreferences = createSlice({ | |
updateLocation(state, action: PayloadAction<ElectricityType>) { | ||
state.location = action.payload; | ||
}, | ||
changeLanguage(state, action: PayloadAction<string>) { | ||
state.language = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
const { acceptTermsOfUse, updateLocation, toggleNotifications } = userPreferences.actions; | ||
const { | ||
acceptTermsOfUse, | ||
updateLocation, | ||
toggleNotifications, | ||
changeLanguage, | ||
} = userPreferences.actions; | ||
|
||
export const actions = { | ||
acceptTermsOfUse, | ||
updateLocation, | ||
toggleNotifications, | ||
changeLanguage, | ||
}; | ||
|
||
export const namespace = userPreferences.name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from "react"; | ||
|
||
import { Text } from "components"; | ||
import { t } from "utils"; | ||
import { Colors, ComponentsStyle } from "style"; | ||
|
||
const navigationOptions = () => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once you have merged
|
||
...ComponentsStyle.transitionBetweenScreenPresets, | ||
headerStyle: { | ||
...ComponentsStyle.header, | ||
}, | ||
headerBackTitleVisible: false, | ||
headerTintColor: Colors.grey100, | ||
headerTitle: () => <Text.H1>{t("LANGUAGES_SCREEN_TITLE")}</Text.H1>, | ||
}); | ||
|
||
export default navigationOptions; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { StyleSheet } from "react-native"; | ||
|
||
import { Layout } from "style"; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
...Layout.containerWithPadding, | ||
}, | ||
}); | ||
|
||
export default styles; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { useCallback, useContext } from "react"; | ||
import { View, ScrollView } from "react-native"; | ||
import { useDispatch } from "react-redux"; | ||
|
||
import { SelectableListItem } from "components"; | ||
import { userPreferences } from "ducks"; | ||
import { LocalizationContext } from "utils"; | ||
|
||
import SupportedLanguages from "./SupportedLanguages"; | ||
import navigationOptions from "./LanguagesScreen.navigationOptions"; | ||
import styles from "./LanguagesScreen.styles"; | ||
|
||
const Language: React.FC<{ | ||
selectedLanguage: string; | ||
language: string; | ||
onSelectLanguage: (language: string) => void; | ||
}> = ({ language, onSelectLanguage, selectedLanguage }) => { | ||
const onClickLanguage = useCallback(() => { | ||
onSelectLanguage?.(language); | ||
}, [language, onSelectLanguage]); | ||
|
||
return ( | ||
<SelectableListItem | ||
key={language} | ||
selected={selectedLanguage === language} | ||
title={SupportedLanguages[language]} | ||
onPress={onClickLanguage} | ||
/> | ||
); | ||
}; | ||
|
||
const LanguagesScreen = () => { | ||
const dispatch = useDispatch(); | ||
const { language, setLanguage } = useContext(LocalizationContext); | ||
const onPress = useCallback( | ||
(lang: string) => { | ||
dispatch(userPreferences.actions.changeLanguage(lang)); | ||
setLanguage(lang); | ||
}, | ||
[dispatch, setLanguage] | ||
); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<ScrollView> | ||
{Object.keys(SupportedLanguages).map((lang: string) => ( | ||
<Language | ||
key={lang} | ||
selectedLanguage={language} | ||
language={lang} | ||
onSelectLanguage={onPress} | ||
/> | ||
))} | ||
</ScrollView> | ||
</View> | ||
); | ||
}; | ||
|
||
LanguagesScreen.navigationOptions = navigationOptions; | ||
|
||
export default LanguagesScreen; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const SupportedLanguages = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change to
|
||
// Key-value pairs | ||
// State the name of the language in its own language :) | ||
en: "English", | ||
fr: "Français", | ||
de: "Deutsche", | ||
sv: "Slovenščina", | ||
ru: "Pусский", | ||
pt: "Português", | ||
pl: "Polskie", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are missing 2 languages : Swedish & Danish (and maybe chinese which might come soon as well depending on who merge first). |
||
}; | ||
|
||
export default SupportedLanguages; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from "react"; | ||
import { create } from "react-test-renderer"; | ||
|
||
import LanguagesScreen from "../LanguagesScreen"; | ||
|
||
// jest.unmock("../"); | ||
|
||
it("LanguageScreen renders correctly", () => { | ||
const tree = create(<LanguagesScreen />).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comment on
SupportedLanguages.ts
Then you can do something like :