Skip to content

Commit 7100f36

Browse files
Integrate i18n for error messages in Bedrock service and ChatPage
- Replaced hardcoded error messages with i18n translations in BedrockService and ChatPage. - Added a new function to retrieve translated content filtered messages. - Updated i18n configuration to include 'errors' namespace and added corresponding translation files for English, Spanish, and French. - Enhanced error handling in ChatPage to utilize i18n for user-friendly feedback.
1 parent 50646ef commit 7100f36

File tree

9 files changed

+44
-22
lines changed

9 files changed

+44
-22
lines changed

frontend/src/common/services/ai/bedrock.service.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { BedrockRuntimeClient, InvokeModelCommand } from '@aws-sdk/client-bedrock-runtime';
22
import { fetchAuthSession } from '@aws-amplify/auth';
33
import { REGION } from '../../config/aws-config';
4+
import i18n from '../../utils/i18n';
45

5-
export const CONTENT_FILTERED_MESSAGE = "I couldn't find an answer. Please try rephrasing your question or consult your healthcare provider.";
6+
// This function gets the translated message from i18n
7+
const getContentFilteredMessage = (): string => {
8+
return i18n.t('ai.content_filtered', { ns: 'errors' });
9+
};
610

711
export interface ChatMessage {
812
role: 'user' | 'assistant' | 'system';
@@ -82,7 +86,7 @@ class BedrockService {
8286
}
8387
}
8488

85-
private handleBedrockResponse(parsedResponse: BedrockResponse, userPrompt?: string): string {
89+
private handleBedrockResponse(parsedResponse: BedrockResponse): string {
8690
// Check if we have results
8791
if (!parsedResponse.results || !parsedResponse.results.length) {
8892
throw new Error('Invalid response structure: missing results');
@@ -95,20 +99,10 @@ class BedrockService {
9599
// Increment counter for analytics
96100
this.contentFilteredCount++;
97101

98-
// Log for debugging if we have the original prompt
99-
if (userPrompt) {
100-
console.warn(`Content filtered response #${this.contentFilteredCount} for prompt: "${userPrompt.substring(0, 100)}..."`);
101-
} else {
102-
console.warn(`Content filtered response #${this.contentFilteredCount}`);
103-
}
104-
105-
return CONTENT_FILTERED_MESSAGE;
106-
}
107-
108-
// Check for other potential failure reasons
109-
if (result.completionReason && result.completionReason !== "COMPLETE") {
110-
console.warn(`Incomplete response with reason: ${result.completionReason}`);
102+
// Return the translated message
103+
return getContentFilteredMessage();
111104
}
105+
112106

113107
return result.outputText;
114108
}

frontend/src/common/utils/i18n/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ i18n
1919
// languages, namespaces, and resources
2020
supportedLngs: ['en', 'es', 'fr'],
2121
fallbackLng: 'en',
22-
ns: ['account', 'auth', 'common', 'home', 'user'],
22+
ns: ['account', 'auth', 'common', 'errors', 'home', 'user'],
2323
defaultNS: 'common',
2424
resources: { en, es, fr },
2525

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"chat": {
3+
"general": "Sorry, something went wrong. Please try again."
4+
},
5+
"ai": {
6+
"content_filtered": "I couldn't find an answer. Please try rephrasing your question or consult your healthcare provider."
7+
}
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import account from './account.json';
22
import auth from './auth.json';
33
import common from './common.json';
4+
import errors from './errors.json';
45
import home from './home.json';
56
import user from './user.json';
67

7-
export default { account, auth, common, home, user };
8+
export default { account, auth, common, errors, home, user };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"chat": {
3+
"general": "Lo siento, algo salió mal. Por favor, inténtalo de nuevo."
4+
},
5+
"ai": {
6+
"content_filtered": "No pude encontrar una respuesta. Intenta reformular tu pregunta o consulta a tu proveedor de salud."
7+
}
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import account from './account.json';
22
import auth from './auth.json';
33
import common from './common.json';
4+
import errors from './errors.json';
45
import home from './home.json';
56
import user from './user.json';
67

7-
export default { account, auth, common, home, user };
8+
export default { account, auth, common, errors, home, user };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"chat": {
3+
"general": "Désolé, une erreur s'est produite. Veuillez réessayer."
4+
},
5+
"ai": {
6+
"content_filtered": "Je n'ai pas pu trouver de réponse. Veuillez reformuler votre question ou consulter votre prestataire de soins de santé."
7+
}
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import account from './account.json';
22
import auth from './auth.json';
33
import common from './common.json';
4+
import errors from './errors.json';
45
import home from './home.json';
56
import user from './user.json';
67

7-
export default { account, auth, common, home, user };
8+
export default { account, auth, common, errors, home, user };

frontend/src/pages/Chat/ChatPage.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { chatService } from '../../common/services/ChatService';
88
import { ChatMessageData } from '../../common/components/Chat/ChatMessage';
99
import aiIcon from '../../assets/img/ai-icon.svg';
1010
import { faRobot } from '@fortawesome/free-solid-svg-icons';
11+
import i18n from '../../common/utils/i18n';
1112
import './ChatPage.scss';
1213

1314
/**
1415
* The `ChatPage` component displays the chat interface.
1516
* @returns JSX
1617
*/
1718
const ChatPage = (): JSX.Element => {
18-
const { t } = useTranslation();
19+
const { t } = useTranslation(['common', 'errors']);
1920
const [messages, setMessages] = useState<ChatMessageData[]>([]);
2021
const location = useLocation();
2122
const prevPathRef = useRef(location.pathname);
@@ -74,8 +75,8 @@ const ChatPage = (): JSX.Element => {
7475
} catch (error) {
7576
console.error('Error getting AI response:', error);
7677

77-
// Create an error message to display to the user
78-
const errorMessage = "Sorry, something went wrong. Please try again.";
78+
// Use i18n directly with the full namespace and key
79+
const errorMessage = i18n.t('chat.general', { ns: 'errors' });
7980

8081
const assistantErrorMessage = chatService.createAssistantMessage(errorMessage);
8182
setMessages(prevMessages => [...prevMessages, assistantErrorMessage]);

0 commit comments

Comments
 (0)