Skip to content
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

Merged
merged 25 commits into from
Aug 7, 2021
Merged

Implement language switching #289

merged 25 commits into from
Aug 7, 2021

Conversation

adrianxx321
Copy link
Contributor

This is referring to the feature as requested in #249, i.e, to have language switching mechanism when the app is running.

First of all, apologize if I have been overdoing it as I introduced a new screen under settings that allows for languages selection. I admit the changes that I've made is quite a lot, but no worries as most of the changes involved are just adding the translation entries for the associated translation files.

Right now, the behaviour of language switching is of only minimal, like what is mentioned in the ticket #249 where I couldn't actually change the language for the entire app, but I believe having able to see the name of the month changed (see screenshot below) should indicate that I've implemented the desired mechanism. By the way, even though I observed the language is currently set once in the App.tsx, I didn't make any change to it. Instead, I just added a new entry under userPreferences store to keep track of the language selected which will be reflected in the new language settings screen, and then from the languages screen when the language is changed, I used the setLanguage property as provided in the LocalizationContext, thereby achieving the desired mechanism.

Below are the screenshot showing the sign of language being changed as well as a demo in GIF to better show that behaviour.

Screenshot 2021-07-29 at 10 35 41 AM
Screen Recording 2021-07-29 at 10 37 21 AM

Last but not least, I am proud to be able to contribute to the localisation feature of this app. I strongly believe that once we figure out the way of making language switching to cover the rest of the UI, at one day, this app will definitely have significantly greater worldwide reachability, to help save our planet!

Thank you.

Copy link
Member

@PierreBresson PierreBresson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there 🙂

@@ -0,0 +1,13 @@
const SupportedLanguages = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change to supportedLanguages instead since it's not a class/component but just a static list. Could you move this file to utils/translations? And change it the following :

import { locale } from "expo-localization";

const supportedLanguages = { ... }

const currentLanguage = Object.keys(supportedLanguages).includes(locale.substr(0, 2))
  ? locale.substr(0, 2)
  : supportedLanguages[0];

export { currentLanguage, supportedLanguages };

@@ -10,6 +12,9 @@ describe("userPreferences reducer should", () => {
acceptedTermsOfUseVersion: 0,
activatedNotifications: false,
location: ElectricityType.world,
language: Object.keys(SupportedLanguages).includes(locale.substr(0, 2))
Copy link
Member

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 :

import { currentLanguage } from "utils";
...
language: currentLanguage


const initialState = {
acceptedTermsOfUseVersion: 0,
activatedNotifications: false,
location: ElectricityType.world,
language: Object.keys(SupportedLanguages).includes(locale.substr(0, 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same here) see my other comment on SupportedLanguages.ts
Then you can do something like :

import { currentLanguage } from "utils";
...
language: currentLanguage

sv: "Slovenščina",
ru: "Pусский",
pt: "Português",
pl: "Polskie",
Copy link
Member

Choose a reason for hiding this comment

The 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).

@@ -32,6 +32,10 @@ const SettingsScreen = (props) => {
title: t("SETTINGS_SCREEN_MY_LOCATION"),
onPress: navigator.openMyLocation,
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these changes from rowItems.
Instead, add the following at the end of rowItems :

  if (__DEV__) {
    rowItems.push({
      title: t("SETTINGS_SCREEN_LANGUAGES"),
      onPress: navigator.openLanguages,
    });
  }

Reason is, this cannot be send to prod as it is, since this is just changing the date language and not the entire app language.

@@ -6,5 +6,6 @@
"SETTINGS_SCREEN_SUPPORT_US": "Stöd oss!",
"SETTINGS_SCREEN_TAB_NAME": "Inställningar",
"SETTINGS_SCREEN_TERMS_OF_USE": "Villkor",
"SETTINGS_SCREEN_TITLE": "Inställningar"
"SETTINGS_SCREEN_TITLE": "Inställningar",
"SETTINGS_SCREEN_LANGUAGES": "Jeziki"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"SETTINGS_SCREEN_LANGUAGES": "Språk"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear, I thought sv meant Slovenian, but it turns out to be Swedish 😅

import { t } from "utils";
import { Colors, ComponentsStyle } from "style";

const navigationOptions = () => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you have merged main into your branch, here is how to fix eslint issues :

import { StackNavigationOptions } from "@react-navigation/stack";
...
const navigationOptions = (): StackNavigationOptions => ({

@PierreBresson PierreBresson merged commit ff5d2ad into NMF-earth:main Aug 7, 2021
@PierreBresson
Copy link
Member

@adrianxx321 thanks for the help 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants