Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __mocks__/react-native-localize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mockRNLocalize = require('react-native-localize/mock');

module.exports = mockRNLocalize;
2 changes: 1 addition & 1 deletion desktop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ let downloadedVersion;
// Note that we have to subscribe to this separately and cannot use Localize.translateLocal,
// because the only way code can be shared between the main and renderer processes at runtime is via the context bridge
// So we track preferredLocale separately via ELECTRON_EVENTS.LOCALE_UPDATED
let preferredLocale = CONST.DEFAULT_LOCALE;
let preferredLocale = CONST.LOCALES.DEFAULT;

const appProtocol = CONST.DEEPLINK_BASE_URL.replace('://', '');

Expand Down
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ PODS:
- RNFBApp
- RNGestureHandler (2.9.0):
- React-Core
- RNLocalize (2.2.6):
- React-Core
- RNPermissions (3.6.1):
- React-Core
- RNReactNativeHapticFeedback (1.14.0):
Expand Down Expand Up @@ -751,6 +753,7 @@ DEPENDENCIES:
- "RNFBCrashlytics (from `../node_modules/@react-native-firebase/crashlytics`)"
- "RNFBPerf (from `../node_modules/@react-native-firebase/perf`)"
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
- RNLocalize (from `../node_modules/react-native-localize`)
- RNPermissions (from `../node_modules/react-native-permissions`)
- RNReactNativeHapticFeedback (from `../node_modules/react-native-haptic-feedback`)
- RNReanimated (from `../node_modules/react-native-reanimated`)
Expand Down Expand Up @@ -935,6 +938,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/@react-native-firebase/perf"
RNGestureHandler:
:path: "../node_modules/react-native-gesture-handler"
RNLocalize:
:path: "../node_modules/react-native-localize"
RNPermissions:
:path: "../node_modules/react-native-permissions"
RNReactNativeHapticFeedback:
Expand Down Expand Up @@ -1050,6 +1055,7 @@ SPEC CHECKSUMS:
RNFBCrashlytics: 2061ca863e8e2fa1aae9b12477d7dfa8e88ca0f9
RNFBPerf: 389914cda4000fe0d996a752532a591132cbf3f9
RNGestureHandler: 071d7a9ad81e8b83fe7663b303d132406a7d8f39
RNLocalize: d4b8af4e442d4bcca54e68fc687a2129b4d71a81
RNPermissions: dcdb7b99796bbeda6975a6e79ad519c41b251b1c
RNReactNativeHapticFeedback: 1e3efeca9628ff9876ee7cdd9edec1b336913f8c
RNReanimated: 3eb05867410b44acaa81dd423945af3093305bd4
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"react-native-image-pan-zoom": "^2.1.12",
"react-native-image-picker": "^5.0.2",
"react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972",
"react-native-localize": "^2.2.6",
"react-native-modal": "^13.0.0",
"react-native-onyx": "1.0.36",
"react-native-pdf": "^6.6.2",
Expand Down
8 changes: 7 additions & 1 deletion src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,13 @@ const CONST = {
DURATION_LONG: 3500,
},

DEFAULT_LOCALE: 'en',
LOCALES: {
EN: 'en',
ES_ES: 'es-ES',
ES: 'es',

DEFAULT: 'en',
},

POLICY: {
TYPE: {
Expand Down
5 changes: 3 additions & 2 deletions src/components/CalendarPicker/generateMonthMatrix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moment from 'moment';
import CONST from '../../CONST';

/**
* Generates a matrix representation of a month's calendar given the year and month.
Expand Down Expand Up @@ -26,7 +27,7 @@ export default function generateMonthMatrix(year, month) {

// Get the number of days in the month and the first day of the month
const daysInMonth = moment([year, month]).daysInMonth();
const firstDay = moment([year, month, 1]).locale('en');
const firstDay = moment([year, month, 1]).locale(CONST.LOCALES.EN);

// Create a matrix to hold the calendar days
const matrix = [];
Expand All @@ -39,7 +40,7 @@ export default function generateMonthMatrix(year, month) {

// Add calendar days to the matrix
for (let i = 1; i <= daysInMonth; i++) {
const day = moment([year, month, i]).locale('en');
const day = moment([year, month, i]).locale(CONST.LOCALES.EN);
currentWeek.push(day.date());

// Start a new row when the current week is full
Expand Down
2 changes: 1 addition & 1 deletion src/components/LocalePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const propTypes = {
};

const defaultProps = {
preferredLocale: CONST.DEFAULT_LOCALE,
preferredLocale: CONST.LOCALES.DEFAULT,
size: 'normal',
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/withLocalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const localeProviderPropTypes = {
};

const localeProviderDefaultProps = {
preferredLocale: CONST.DEFAULT_LOCALE,
preferredLocale: CONST.LOCALES.DEFAULT,
currentUserPersonalDetails: {},
};

Expand Down
2 changes: 1 addition & 1 deletion src/libs/IntlPolyfill/shouldPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CONST from '../../CONST';
*/
function oldCurrencyData() {
return (
new Intl.NumberFormat(CONST.DEFAULT_LOCALE, {
new Intl.NumberFormat(CONST.LOCALES.DEFAULT, {
style: CONST.POLYFILL_TEST.STYLE,
currency: CONST.POLYFILL_TEST.CURRENCY,
currencyDisplay: CONST.POLYFILL_TEST.FORMAT,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Localize/LocaleListener/BaseLocaleListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Onyx from 'react-native-onyx';
import CONST from '../../../CONST';
import ONYXKEYS from '../../../ONYXKEYS';

let preferredLocale = CONST.DEFAULT_LOCALE;
let preferredLocale = CONST.LOCALES.DEFAULT;

/**
* Adds event listener for changes to the locale. Callbacks are executed when the locale changes in Onyx.
Expand Down
19 changes: 16 additions & 3 deletions src/libs/Localize/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import * as RNLocalize from 'react-native-localize';
import Log from '../Log';
import Config from '../../CONFIG';
import translations from '../../languages/translations';
Expand All @@ -19,7 +20,7 @@ LocaleListener.connect();
* @param {Object} [phraseParameters] Parameters to supply if the phrase is a template literal.
* @returns {String}
*/
function translate(desiredLanguage = CONST.DEFAULT_LOCALE, phraseKey, phraseParameters = {}) {
function translate(desiredLanguage = CONST.LOCALES.DEFAULT, phraseKey, phraseParameters = {}) {
const languageAbbreviation = desiredLanguage.substring(0, 2);
let translatedPhrase;

Expand All @@ -36,12 +37,12 @@ function translate(desiredLanguage = CONST.DEFAULT_LOCALE, phraseKey, phrasePara
if (translatedPhrase) {
return Str.result(translatedPhrase, phraseParameters);
}
if (languageAbbreviation !== 'en') {
if (languageAbbreviation !== CONST.LOCALES.DEFAULT) {
Log.alert(`${phraseKey} was not found in the ${languageAbbreviation} locale`);
}

// Phrase is not translated, search it in default language (en)
const defaultLanguageDictionary = lodashGet(translations, 'en', {});
const defaultLanguageDictionary = lodashGet(translations, CONST.LOCALES.DEFAULT, {});
translatedPhrase = lodashGet(defaultLanguageDictionary, phraseKey);
if (translatedPhrase) {
return Str.result(translatedPhrase, phraseParameters);
Expand Down Expand Up @@ -87,8 +88,20 @@ function arrayToString(anArray) {
return aString;
}

/**
* Returns the user device's preferred language.
*
* @return {String}
*/
function getDevicePreferredLocale() {
return lodashGet(
RNLocalize.findBestAvailableLanguage([CONST.LOCALES.EN, CONST.LOCALES.ES]), 'languageTag', CONST.LOCALES.DEFAULT,
);
}

export {
translate,
translateLocal,
arrayToString,
getDevicePreferredLocale,
};
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Onyx.connect({
let preferredLocale;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE,
callback: val => preferredLocale = val || CONST.LOCALES.DEFAULT,
});

const policies = {};
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Onyx.connect({
callback: val => sessionEmail = val ? val.email : null,
});

let preferredLocale = CONST.DEFAULT_LOCALE;
let preferredLocale = CONST.LOCALES.DEFAULT;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (val) => {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Onyx.connect({
let preferredLocale;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE,
callback: val => preferredLocale = val || CONST.LOCALES.DEFAULT,
});

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Onyx.connect({
},
});

let preferredLocale = CONST.DEFAULT_LOCALE;
let preferredLocale = CONST.LOCALES.DEFAULT;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (val) => {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ function signInWithShortLivedAuthToken(email, authToken) {
* @param {String} [twoFactorAuthCode]
* @param {String} [preferredLocale] Indicates which language to use when the user lands in the app
*/
function signIn(password, validateCode, twoFactorAuthCode, preferredLocale = CONST.DEFAULT_LOCALE) {
function signIn(password, validateCode, twoFactorAuthCode, preferredLocale = CONST.LOCALES.DEFAULT) {
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
Expand Down
7 changes: 4 additions & 3 deletions src/pages/ReimbursementAccount/exampleCheckImage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import exampleCheckImageEn from '../../../assets/images/example-check-image-en.png';
import exampleCheckImageEs from '../../../assets/images/example-check-image-es.png';
import CONST from '../../CONST';

const images = {
en: exampleCheckImageEn,
es: exampleCheckImageEs,
[CONST.LOCALES.EN]: exampleCheckImageEn,
[CONST.LOCALES.ES]: exampleCheckImageEs,
};

function exampleCheckImage(languageKey = 'en') {
function exampleCheckImage(languageKey = CONST.LOCALES.EN) {
return images[languageKey];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import withLocalize, {withLocalizePropTypes} from '../../../../components/withLo
import PopoverWithMeasuredContent from '../../../../components/PopoverWithMeasuredContent';
import BaseReportActionContextMenu from './BaseReportActionContextMenu';
import ConfirmModal from '../../../../components/ConfirmModal';
import CONST from '../../../../CONST';

const propTypes = {
...withLocalizePropTypes,
Expand Down Expand Up @@ -69,8 +70,8 @@ class PopoverReportActionContextMenu extends React.Component {
}

shouldComponentUpdate(nextProps, nextState) {
const previousLocale = lodashGet(this.props, 'preferredLocale', 'en');
const nextLocale = lodashGet(nextProps, 'preferredLocale', 'en');
const previousLocale = lodashGet(this.props, 'preferredLocale', CONST.LOCALES.DEFAULT);
const nextLocale = lodashGet(nextProps, 'preferredLocale', CONST.LOCALES.DEFAULT);
return this.state.isPopoverVisible !== nextState.isPopoverVisible
|| this.state.popoverAnchorPosition !== nextState.popoverAnchorPosition
|| this.state.isDeleteCommentConfirmModalVisible !== nextState.isDeleteCommentConfirmModalVisible
Expand Down
2 changes: 1 addition & 1 deletion src/pages/signin/PasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const propTypes = {

const defaultProps = {
account: {},
preferredLocale: CONST.DEFAULT_LOCALE,
preferredLocale: CONST.LOCALES.DEFAULT,
};

class PasswordForm extends React.Component {
Expand Down
4 changes: 4 additions & 0 deletions src/pages/signin/SignInPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import ValidateCodeForm from './ValidateCodeForm';
import ResendValidationForm from './ResendValidationForm';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import Performance from '../../libs/Performance';
import * as App from '../../libs/actions/App';
import Permissions from '../../libs/Permissions';
import * as Localize from '../../libs/Localize';

const propTypes = {
/* Onyx Props */
Expand Down Expand Up @@ -50,6 +52,8 @@ const defaultProps = {
class SignInPage extends Component {
componentDidMount() {
Performance.measureTTI();

App.setLocale(Localize.getDevicePreferredLocale());
Comment on lines +55 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This caused a regression in #29620 It will always reset the language to the device-preferred language even when the user sets a different language. we fix this by adding an extra check to Onyx preferredLocale.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@fedirjh We had discussed about the issue and decided to keep it this way.

@fedirjh fedirjh Oct 27, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sobitneupane Thanks for the feedback, in that case this is not a regression rather than a feature request.

I have updated the BZ checklist.

}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const propTypes = {
const defaultProps = {
account: {},
credentials: {},
preferredLocale: CONST.DEFAULT_LOCALE,
preferredLocale: CONST.LOCALES.DEFAULT,
};

class BaseValidateCodeForm extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/wallet/WalletStatementPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const defaultProps = {
walletStatement: {
isGenerating: false,
},
preferredLocale: CONST.DEFAULT_LOCALE,
preferredLocale: CONST.LOCALES.DEFAULT,
};

class WalletStatementPage extends React.Component {
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/CalendarPickerTest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {render, fireEvent, within} from '@testing-library/react-native';
import moment from 'moment';
import CalendarPicker from '../../src/components/CalendarPicker';
import CONST from '../../src/CONST';

moment.locale('en');
moment.locale(CONST.LOCALES.EN);
const monthNames = moment.localeData().months();

jest.mock('@react-navigation/native', () => ({
Expand All @@ -13,7 +14,7 @@ jest.mock('@react-navigation/native', () => ({
// eslint-disable-next-line arrow-body-style
const MockedCalendarPicker = (props) => {
// eslint-disable-next-line react/jsx-props-no-spreading
return <CalendarPicker {...props} translate={() => ''} preferredLocale="en" />;
return <CalendarPicker {...props} translate={() => ''} preferredLocale={CONST.LOCALES.EN} />;
};

describe('CalendarPicker', () => {
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/CurrencySymbolUtilsTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'underscore';
import * as CurrencySymbolUtils from '../../src/libs/CurrencySymbolUtils';
import CONST from '../../src/CONST';

// This file can get outdated. In that case, you can follow these steps to update it:
// - open your browser console and navigate to the Network tab
Expand All @@ -10,12 +11,12 @@ import * as CurrencySymbolUtils from '../../src/libs/CurrencySymbolUtils';
import currencyList from './currencyList.json';

const currencyCodeList = _.keys(currencyList);
const AVAILABLE_LOCALES = ['en', 'es'];
const AVAILABLE_LOCALES = [CONST.LOCALES.EN, CONST.LOCALES.ES];

// Contains item [isLeft, locale, currencyCode]
const symbolPositions = [
[true, 'en', 'USD'],
[false, 'es', 'USD'],
[true, CONST.LOCALES.EN, 'USD'],
[false, CONST.LOCALES.ES, 'USD'],
];

describe('CurrencySymbolUtils', () => {
Expand Down
Loading