Skip to content

Commit

Permalink
Merge pull request #28377 from dukenv0307/fix/27759-catch-translation…
Browse files Browse the repository at this point in the history
…-key
  • Loading branch information
mountiny authored Oct 9, 2023
2 parents 619aee6 + 8c7f8a2 commit a545eaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ const CONST = {
RECEIPTS: '[email protected]',
STUDENT_AMBASSADOR: '[email protected]',
SVFG: '[email protected]',
EXPENSIFY_EMAIL_DOMAIN: '@expensify.com',
},

ACCOUNT_ID: {
Expand Down Expand Up @@ -2725,6 +2726,8 @@ const CONST = {
SPEND: 'spend',
WORKSPACES: 'workspaces',
},

MISSING_TRANSLATION: 'MISSING TRANSLATION',
} as const;

export default CONST;
22 changes: 20 additions & 2 deletions src/libs/Localize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ import _ from 'underscore';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import * as RNLocalize from 'react-native-localize';
import Onyx from 'react-native-onyx';
import Log from '../Log';
import Config from '../../CONFIG';
import translations from '../../languages/translations';
import CONST from '../../CONST';
import LocaleListener from './LocaleListener';
import BaseLocaleListener from './LocaleListener/BaseLocaleListener';
import ONYXKEYS from '../../ONYXKEYS';

// Current user mail is needed for handling missing translations
let userEmail = '';
Onyx.connect({
key: ONYXKEYS.SESSION,
waitForCollectionCallback: true,
callback: (val) => {
if (!val) {
return;
}
userEmail = val.email;
},
});

// Listener when an update in Onyx happens so we use the updated locale when translating/localizing items.
LocaleListener.connect();
Expand Down Expand Up @@ -70,11 +85,14 @@ function translate(desiredLanguage = CONST.LOCALES.DEFAULT, phraseKey, phrasePar
return Str.result(translatedPhrase, phraseParameters);
}

// Phrase is not found in default language, on production log an alert to server
// Phrase is not found in default language, on production and staging log an alert to server
// on development throw an error
if (Config.IS_IN_PRODUCTION) {
if (Config.IS_IN_PRODUCTION || Config.IS_IN_STAGING) {
const phraseString = _.isArray(phraseKey) ? phraseKey.join('.') : phraseKey;
Log.alert(`${phraseString} was not found in the en locale`);
if (userEmail.includes(CONST.EMAIL.EXPENSIFY_EMAIL_DOMAIN)) {
return CONST.MISSING_TRANSLATION;
}
return phraseString;
}
throw new Error(`${phraseKey} was not found in the default language`);
Expand Down

0 comments on commit a545eaa

Please sign in to comment.