From d7966ec272c9c07880f4234a82347bec0a0dac77 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 28 Jun 2023 16:44:40 +0700 Subject: [PATCH 1/2] fix: translation for system message --- src/CONST.js | 4 ++++ src/libs/OptionsListUtils.js | 4 ++-- src/libs/ReportActionsUtils.js | 18 ++++++++++++------ src/libs/ReportUtils.js | 2 ++ src/libs/actions/Report.js | 9 +++++++-- src/libs/isReportMessageAttachment.js | 8 ++++++-- tests/actions/ReportTest.js | 2 +- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/CONST.js b/src/CONST.js index f8ac136e720a..1612b2ad8826 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -2513,6 +2513,10 @@ const CONST = { TEXT: 'text', RADIO: 'radio', }, + + TRANSLATION_KEYS: { + ATTACHMENT: 'common.attachment' + }, }; export default CONST; diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index a8edd8636202..a30863e02460 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -378,8 +378,8 @@ function getAllReportErrors(report, reportActions) { */ function getLastMessageTextForReport(report) { let lastMessageTextFromReport = ''; - if (ReportUtils.isReportMessageAttachment({text: report.lastMessageText, html: report.lastMessageHtml})) { - lastMessageTextFromReport = `[${Localize.translateLocal('common.attachment')}]`; + if (ReportUtils.isReportMessageAttachment({text: report.lastMessageText, html: report.lastMessageHtml, translationKey: report.lastMessageTranslationKey})) { + lastMessageTextFromReport = `[${Localize.translateLocal(report.lastMessageTranslationKey || 'common.attachment')}]`; } else { lastMessageTextFromReport = report ? report.lastMessageText || '' : ''; diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index f57084c9f0e0..e951cca1d911 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -254,22 +254,28 @@ function getLastVisibleAction(reportID, actionsToMerge = {}) { /** * @param {String} reportID * @param {Object} [actionsToMerge] - * @return {String} + * @return {Object} */ -function getLastVisibleMessageText(reportID, actionsToMerge = {}) { +function getLastVisibleMessage(reportID, actionsToMerge = {}) { const lastVisibleAction = getLastVisibleAction(reportID, actionsToMerge); const message = lodashGet(lastVisibleAction, ['message', 0], {}); if (isReportMessageAttachment(message)) { - return CONST.ATTACHMENT_MESSAGE_TEXT; + return { + lastMessageTranslationKey: CONST.TRANSLATION_KEYS.ATTACHMENT, + }; } if (isCreatedAction(lastVisibleAction)) { - return ''; + return { + lastMessageText: '', + }; } const messageText = lodashGet(message, 'text', ''); - return String(messageText).replace(CONST.REGEX.AFTER_FIRST_LINE_BREAK, '').substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH).trim(); + return { + lastMessageText: String(messageText).replace(CONST.REGEX.AFTER_FIRST_LINE_BREAK, '').substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH).trim(), + }; } /** @@ -442,7 +448,7 @@ function isWhisperAction(action) { export { getSortedReportActions, getLastVisibleAction, - getLastVisibleMessageText, + getLastVisibleMessage, getMostRecentIOURequestActionID, extractLinksFromMessageHtml, isDeletedAction, diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 64a07113cb51..546b609c3ec4 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1187,6 +1187,7 @@ function buildOptimisticAddCommentReportAction(text, file) { created: DateUtils.getDBTime(), message: [ { + translationKey: isAttachment ? CONST.TRANSLATION_KEYS.ATTACHMENT : '', type: CONST.REPORT.MESSAGE.TYPE.COMMENT, html: htmlForNewComment, text: textForNewComment, @@ -1527,6 +1528,7 @@ function buildOptimisticChatReport( isPinned: reportName === CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS, lastActorEmail: '', lastActorAccountID: 0, + lastMessageTranslationKey: '', lastMessageHtml: '', lastMessageText: null, lastReadTime: currentTime, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c5c5aa2250c3..45a0630c7b65 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -234,6 +234,7 @@ function addActions(reportID, text = '', file) { const optimisticReport = { lastVisibleActionCreated: currentTime, + lastMessageTranslationKey: lodashGet(lastAction, 'message[0].translationKey', ''), lastMessageText: lastCommentText, lastMessageHtml: lastCommentText, lastActorEmail: currentUserEmail, @@ -824,6 +825,7 @@ function deleteReportComment(reportID, reportAction) { const reportActionID = reportAction.reportActionID; const deletedMessage = [ { + translationKey: '', type: 'COMMENT', html: '', text: '', @@ -843,13 +845,15 @@ function deleteReportComment(reportID, reportAction) { // If we are deleting the last visible message, let's find the previous visible one (or set an empty one if there are none) and update the lastMessageText in the LHN. // Similarly, if we are deleting the last read comment we will want to update the lastVisibleActionCreated to use the previous visible message. let optimisticReport = { + lastMessageTranslationKey: '', lastMessageText: '', lastVisibleActionCreated: '', }; - const lastMessageText = ReportActionsUtils.getLastVisibleMessageText(reportID, optimisticReportActions); - if (lastMessageText.length > 0) { + const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportActionsUtils.getLastVisibleMessage(reportID, optimisticReportActions); + if (lastMessageText || lastMessageTranslationKey) { const lastVisibleActionCreated = ReportActionsUtils.getLastVisibleAction(reportID, optimisticReportActions).created; optimisticReport = { + lastMessageTranslationKey, lastMessageText, lastVisibleActionCreated, }; @@ -1008,6 +1012,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) { if (reportActionID === lastVisibleAction.reportActionID) { const lastMessageText = ReportUtils.formatReportLastMessageText(reportComment); const optimisticReport = { + lastMessageTranslationKey: '', lastMessageText, }; optimisticData.push({ diff --git a/src/libs/isReportMessageAttachment.js b/src/libs/isReportMessageAttachment.js index b449c72eaa0d..e107df8ddfaa 100644 --- a/src/libs/isReportMessageAttachment.js +++ b/src/libs/isReportMessageAttachment.js @@ -4,10 +4,14 @@ import CONST from '../CONST'; * Check whether a report action is Attachment or not. * Ignore messages containing [Attachment] as the main content. Attachments are actions with only text as [Attachment]. * - * @param {Object} reportActionMessage report action's message as text and html + * @param {Object} reportActionMessage report action's message as text, html and translationKey * @returns {Boolean} */ -export default function isReportMessageAttachment({text, html}) { +export default function isReportMessageAttachment({text, html, translationKey}) { + if (translationKey) { + return translationKey === CONST.TRANSLATION_KEYS.ATTACHMENT; + } + if (!text || !html) { return false; } diff --git a/tests/actions/ReportTest.js b/tests/actions/ReportTest.js index e98a5249187f..b8b45026e9aa 100644 --- a/tests/actions/ReportTest.js +++ b/tests/actions/ReportTest.js @@ -50,7 +50,7 @@ describe('actions/Report', () => { actorEmail: TEST_USER_LOGIN, automatic: false, avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/avatar_3.png', - message: [{type: 'COMMENT', html: 'Testing a comment', text: 'Testing a comment'}], + message: [{type: 'COMMENT', html: 'Testing a comment', text: 'Testing a comment', translationKey: ''}], person: [{type: 'TEXT', style: 'strong', text: 'Test User'}], shouldShow: true, }; From a5ace867270bb5f0ae34635adb4faa7ef8ea173e Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 28 Jun 2023 16:56:59 +0700 Subject: [PATCH 2/2] fix lint --- src/CONST.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CONST.js b/src/CONST.js index f45452e0a75f..6f0814b1444b 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -2518,7 +2518,7 @@ const CONST = { HEADER_IMAGE_ASPECT_RATIO: 0.64, }, TRANSLATION_KEYS: { - ATTACHMENT: 'common.attachment' + ATTACHMENT: 'common.attachment', }, };