diff --git a/components/LanguageSelector.tsx b/components/LanguageSelector.tsx index e271fa84..f6586204 100644 --- a/components/LanguageSelector.tsx +++ b/components/LanguageSelector.tsx @@ -2,14 +2,24 @@ import React from 'react'; import i18n, { SUPPORTED_LANGUAGES } from '../i18n'; import { View } from 'react-native'; import { Picker } from './ui/Picker'; +import AsyncStorage from '@react-native-async-storage/async-storage'; export const LanguageSelector: React.FC = (props) => { + const langauges = Object.entries(SUPPORTED_LANGUAGES).map( + ([value, label]) => ({ label, value }) + ); + + const changeLanguage = async (value: string) => { + await i18n.changeLanguage(value); + await AsyncStorage.setItem('language', i18n.language); + }; + return ( i18n.changeLanguage(value)} + onValueChange={changeLanguage} triggerComponent={props.triggerComponent} /> diff --git a/i18n.ts b/i18n.ts index f78e20f3..345a2c7b 100644 --- a/i18n.ts +++ b/i18n.ts @@ -4,20 +4,34 @@ import { initReactI18next } from 'react-i18next'; import en from './locales/en.json'; import fil from './locales/fil.json'; +import AsyncStorage from '@react-native-async-storage/async-storage'; const resources = { en, fil }; -export const SUPPORTED_LANGUAGES = [ - { label: 'English', value: 'en' }, - { label: 'Filipino', value: 'fil' }, -]; +export const SUPPORTED_LANGUAGES = { + en: 'English', + fil: 'Filipino', +}; -i18next.use(initReactI18next).init({ - compatibilityJSON: 'v3', - resources, - lng: locale, - fallbackLng: 'en', - supportedLngs: SUPPORTED_LANGUAGES.map(({ value }) => value), -}); +i18next + .use(initReactI18next) + .init({ + compatibilityJSON: 'v3', + resources, + lng: getLanguageCode(locale), + fallbackLng: getLanguageCode, + supportedLngs: Object.keys(SUPPORTED_LANGUAGES), + }) + .then(async () => { + const language = await AsyncStorage.getItem('language'); + if (language !== i18next.language) { + i18next.changeLanguage(language); + } + }); export default i18next; + +function getLanguageCode(code: string) { + const [language] = code.split('-'); + return language; +} diff --git a/machines/app.ts b/machines/app.ts index db9de0a5..15817d27 100644 --- a/machines/app.ts +++ b/machines/app.ts @@ -5,7 +5,7 @@ import { getDeviceName, getDeviceNameSync, } from 'react-native-device-info'; -import { EventFrom, spawn, StateFrom, send } from 'xstate'; +import { EventFrom, spawn, StateFrom, send, assign } from 'xstate'; import { createModel } from 'xstate/lib/model'; import { authMachine, createAuthMachine } from './auth'; import { createSettingsMachine, settingsMachine } from './settings'; @@ -24,14 +24,13 @@ const model = createModel( }, { events: { - 'xstate.init': () => ({}), - 'ACTIVE': () => ({}), - 'INACTIVE': () => ({}), - 'OFFLINE': () => ({}), - 'ONLINE': (networkType: NetInfoStateType) => ({ networkType }), - 'REQUEST_DEVICE_INFO': () => ({}), - 'READY': (data?: unknown) => ({ data }), - 'APP_INFO_RECEIVED': (info: AppInfo) => ({ info }), + ACTIVE: () => ({}), + INACTIVE: () => ({}), + OFFLINE: () => ({}), + ONLINE: (networkType: NetInfoStateType) => ({ networkType }), + REQUEST_DEVICE_INFO: () => ({}), + READY: (data?: unknown) => ({ data }), + APP_INFO_RECEIVED: (info: AppInfo) => ({ info }), }, } ); @@ -141,7 +140,7 @@ export const appMachine = model.createMachine( }, })), - spawnStoreActor: model.assign({ + spawnStoreActor: assign({ serviceRefs: (context) => ({ ...context.serviceRefs, store: spawn(storeMachine, storeMachine.id), diff --git a/machines/settings.ts b/machines/settings.ts index 90f5c675..71ac7314 100644 --- a/machines/settings.ts +++ b/machines/settings.ts @@ -21,6 +21,7 @@ const model = createModel( UPDATE_VC_LABEL: (label: string) => ({ label }), TOGGLE_BIOMETRIC_UNLOCK: (enable: boolean) => ({ enable }), STORE_RESPONSE: (response: unknown) => ({ response }), + CHANGE_LANGUAGE: (language: string) => ({ language }), }, } ); @@ -101,8 +102,8 @@ export const settingsMachine = model.createMachine( }), toggleBiometricUnlock: model.assign({ - isBiometricUnlockEnabled: (_, event) => event.enable - }) + isBiometricUnlockEnabled: (_, event) => event.enable, + }), }, services: {}, diff --git a/screens/BiometricScreen.tsx b/screens/BiometricScreen.tsx index bea7817d..93fe5a6d 100644 --- a/screens/BiometricScreen.tsx +++ b/screens/BiometricScreen.tsx @@ -20,7 +20,7 @@ export const BiometricScreen: React.FC = (props) => {