Skip to content
Open
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
18 changes: 18 additions & 0 deletions AI-chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ def health_check():
def handle_chatbot_request(data):
data = data or {}
user_message = str(data.get("message", "")).strip()
lang = data.get("lang", "en")

try:
if lang and lang != "en":
from deep_translator import GoogleTranslator
user_message = GoogleTranslator(source=lang, target='en').translate(user_message)
except Exception as e:
print("Chatbot translation error (input):", e)
pass

context = data.get('context') # will be None if no analysis has been run yet

context_summary = ""
Expand Down Expand Up @@ -540,6 +550,14 @@ def level(val):
if response is None:
response = generate_response(user_message, context_summary)

try:
if lang and lang != "en":
from deep_translator import GoogleTranslator
response = GoogleTranslator(source='en', target=lang).translate(response)
except Exception as e:
print("Chatbot translation error (response):", e)
pass

return {
"success": True,
"response": response
Expand Down
192 changes: 106 additions & 86 deletions Frontend/Analysis/analysis.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Frontend/Analysis/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ document.addEventListener("DOMContentLoaded", () => {

window.useCurrentLocation = async function () {
if (!navigator.geolocation) {
alert("Geolocation is not supported by your browser.");
showToast("Geolocation is not supported by your browser.", "error");
return;
}

Expand Down Expand Up @@ -837,7 +837,7 @@ window.useCurrentLocation = async function () {
const data = await response.json();

if (!data.success) {
alert(data.message || "Unable to detect location.");
showToast(data.message || "Unable to detect location.", "error");
return;
}

Expand All @@ -848,11 +848,11 @@ window.useCurrentLocation = async function () {
getWeatherData();
} catch (error) {
console.error(error);
alert("Unable to detect location.");
showToast("Unable to detect location.", "error");
}
},
function () {
alert("Location permission denied.");
showToast("Location permission denied.", "warning");
}
);
};
Expand Down
65 changes: 65 additions & 0 deletions Frontend/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
i18next
.use(i18nextHttpBackend)
.use(i18nextBrowserLanguageDetector)
.init({
fallbackLng: 'en',
backend: {
loadPath: '/locales/{{lng}}/translation.json',
}
}).then(() => {
const initLangSync = () => {
updateContent();
const langSelect = document.getElementById('langSelect');
if (langSelect && i18next.language) {
const baseLang = i18next.language.split('-')[0];
langSelect.value = baseLang;
}
};

if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initLangSync);
} else {
initLangSync();
}
});

function updateContent() {
document.querySelectorAll('[data-i18n]').forEach(el => {
const key = el.getAttribute('data-i18n');
if (el.tagName === 'INPUT' && el.placeholder) {
el.placeholder = i18next.t(key);
} else {
el.innerHTML = i18next.t(key);
}
});
}

window.changeLanguage = function(lng) {
i18next.changeLanguage(lng).then(() => {
updateContent();
const langSelect = document.getElementById('langSelect');
if (langSelect && i18next.language) {
const baseLang = i18next.language.split('-')[0];
langSelect.value = baseLang;
}
});
}

// Intercept chatbot submissions to pass current language
document.addEventListener('DOMContentLoaded', () => {
// Override the native fetch to append language if hitting our endpoints
const originalFetch = window.fetch;
window.fetch = function() {
let [resource, config] = arguments;
if (typeof resource === 'string' && (resource.includes('/chatbot') || resource.includes('/weather'))) {
if (config && config.body && typeof config.body === 'string') {
try {
let bodyObj = JSON.parse(config.body);
bodyObj.lang = i18next.language || 'en';
config.body = JSON.stringify(bodyObj);
} catch(e) {}
}
}
return originalFetch.apply(this, arguments);
};
});
Loading