diff --git a/src/components/withLocalize.js b/src/components/withLocalize.js index 0a6797ff027c..99c67e934a04 100755 --- a/src/components/withLocalize.js +++ b/src/components/withLocalize.js @@ -29,6 +29,9 @@ const withLocalizePropTypes = { /** Returns an internationally converted phone number with the country code */ fromLocalPhone: PropTypes.func.isRequired, + + /** The user's preferred locale e.g. 'en', 'es-ES' */ + preferredLocale: PropTypes.string.isRequired, }; const localeProviderPropTypes = { diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 73a390a7967d..13cfc7544614 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -60,9 +60,8 @@ import SCREENS from '../../../SCREENS'; import Timers from '../../Timers'; import LogInWithShortLivedTokenPage from '../../../pages/LogInWithShortLivedTokenPage'; import defaultScreenOptions from './defaultScreenOptions'; -import * as API from '../../API'; -import {setLocale} from '../../actions/App'; import {cleanupSession} from '../../actions/Session'; +import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize'; Onyx.connect({ key: ONYXKEYS.MY_PERSONAL_DETAILS, @@ -83,12 +82,6 @@ Onyx.connect({ }, }); -let currentPreferredLocale; -Onyx.connect({ - key: ONYXKEYS.NVP_PREFERRED_LOCALE, - callback: val => currentPreferredLocale = val || CONST.DEFAULT_LOCALE, -}); - const RootStack = createCustomModalStackNavigator(); // We want to delay the re-rendering for components(e.g. ReportActionCompose) @@ -111,6 +104,7 @@ const propTypes = { isOffline: PropTypes.bool, }), + ...withLocalizePropTypes, ...windowDimensionsPropTypes, }; @@ -141,22 +135,9 @@ class AuthScreens extends React.Component { // Fetch some data we need on initialization NameValuePair.get(CONST.NVP.PRIORITY_MODE, ONYXKEYS.NVP_PRIORITY_MODE, 'default'); NameValuePair.get(CONST.NVP.IS_FIRST_TIME_NEW_EXPENSIFY_USER, ONYXKEYS.NVP_IS_FIRST_TIME_NEW_EXPENSIFY_USER, true); - - API.Get({ - returnValueList: 'nameValuePairs', - nvpNames: ONYXKEYS.NVP_PREFERRED_LOCALE, - }).then((response) => { - const preferredLocale = lodashGet(response, ['nameValuePairs', 'preferredLocale'], CONST.DEFAULT_LOCALE); - if ((currentPreferredLocale !== CONST.DEFAULT_LOCALE) && (preferredLocale !== currentPreferredLocale)) { - setLocale(currentPreferredLocale); - } else { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); - } - }); - PersonalDetails.fetchPersonalDetails(); User.getUserDetails(); - User.getBetas(); + User.fetchBetasAndSetPreferredLocale(this.props.preferredLocale); User.getDomainInfo(); PersonalDetails.fetchLocalCurrency(); fetchAllReports(true, true); @@ -382,6 +363,7 @@ AuthScreens.propTypes = propTypes; AuthScreens.defaultProps = defaultProps; export default compose( withWindowDimensions, + withLocalize, withOnyx({ network: { key: ONYXKEYS.NETWORK, diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 95e8b862d8bc..a05faa8bdc8d 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -1,6 +1,7 @@ import Onyx from 'react-native-onyx'; import SignoutManager from '../SignoutManager'; import ONYXKEYS from '../../ONYXKEYS'; +import Permissions from '../Permissions'; let currentActiveClients; Onyx.connect({ @@ -26,7 +27,8 @@ function clearStorageAndRedirect(errorMessage) { // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear() .then(() => { - if (preferredLocale) { + // Betas are only present when user is Logined + if (Permissions.canUseInternationalization([]) && preferredLocale) { Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); } if (activeClients && activeClients.length > 0) { diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index 3021af64d784..c9714f93baee 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -14,6 +14,9 @@ import Log from '../Log'; import NetworkConnection from '../NetworkConnection'; import NameValuePair from './NameValuePair'; import getSkinToneEmojiFromIndex from '../../pages/home/report/EmojiPickerMenu/getSkinToneEmojiFromIndex'; +import Permissions from '../Permissions'; +import {setLocale} from './App'; + let sessionAuthToken = ''; let sessionEmail = ''; @@ -58,9 +61,35 @@ function changePassword(oldPassword, password) { } function getBetas() { - API.User_GetBetas().then((response) => { + return API.User_GetBetas().then((response) => { if (response.jsonCode === 200) { Onyx.set(ONYXKEYS.BETAS, response.betas); + return response.betas; + } + return []; + }); +} + +/** + * Fetches betas and User's preferred Locale, then set the app locale. + * @param {String} currentPreferredLocale + */ +function fetchBetasAndSetPreferredLocale(currentPreferredLocale) { + Promise.all([ + getBetas(), + API.Get({ + returnValueList: 'nameValuePairs', + nvpNames: ONYXKEYS.NVP_PREFERRED_LOCALE, + }), + ]).then((betas, response) => { + const preferredLocale = lodashGet(response, ['nameValuePairs', 'preferredLocale'], CONST.DEFAULT_LOCALE); + if (currentPreferredLocale !== CONST.DEFAULT_LOCALE + && preferredLocale !== currentPreferredLocale + && Permissions.canUseInternationalization(betas) + ) { + setLocale(currentPreferredLocale); + } else { + Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); } }); } @@ -312,4 +341,5 @@ export { setPreferredSkinTone, setShouldUseSecureStaging, clearUserErrorMessage, + fetchBetasAndSetPreferredLocale, };