diff --git a/translate.html b/translate.html
index 717ec6e..1e818c8 100644
--- a/translate.html
+++ b/translate.html
@@ -225,7 +225,7 @@
Please note that this app uses your default microphone as the audio input source. You sometimes can change the default audio source via the browser's setting, but you can also change it at your system level by changing the default recording device. You can also change audio sources by using a Virtual Audio Cable, such as this one. Using it, it becomes possible to select other sources, including microphones, speakers, and other applications.
- The translation component is based on code from Mozilla https://github.com/mozilla/translate . If your language is missing though, or you want better results, I have a version powered by Google Cloud Translation here . Check out more options and details on GitHub .
+ The translation component is based on code from Mozilla https://github.com/mozilla/translate . If your language is missing though, or you want better results, I have a version powered by Google Cloud Translation here . Check out more options and details on GitHub .
From
@@ -556,17 +556,25 @@ langFrom.addEventListener("change", e => {
const setToCode = (currentTo !== langFrom.value)
? currentTo
: findFirstSupportedTo();
- myLangCode = langFrom.value.split("-")[0];
- if (myLangCode in supportedFromCodes) {
- console.log("guessing input language is", myLangCode);
- setFromCode = myLangCode;
- }
- recognition.stop();
+ myLangCode = langFrom.value.split("-")[0];
+ if (myLangCode in supportedFromCodes) {
+ console.log("setting input language to", myLangCode);
+ myLang = myLangCode;
+ // Update URL parameter
+ updateURL("lang=" + myLangCode, true);
+ // Update recognition language
+ if (recognition) {
+ recognition.lang = myLangCode;
+ }
+ }
+ recognition.stop();
setLangs(langTo, supportedToCodes, setToCode, langFrom.value);
loadModel();
});
langTo.addEventListener("change", e => {
currentTo = langTo.value;
+ // Update URL parameter
+ updateURL("translate=" + currentTo, true);
loadModel();
});
const setLangs = (selector, langsToSet, value, exlcude) => {
@@ -623,7 +631,6 @@ function updateURL(param, force=false) {
} else {
href.searchParams.set(para[0].toLowerCase(), para[1]);
}
- log(href.toString());
window.history.pushState({path:href.toString()},'',href.toString());
}
}
@@ -925,19 +932,19 @@ var sanitize = function(string) {
.trim();
};
function setup(){
- if ('webkitSpeechRecognition' in window){
- recognition = new webkitSpeechRecognition();
- if (myLang && myLangCode){
- if (myLang.split("-")[0] == myLangCode){
- recognition.lang = myLang;
- } else {
- recognition.lang = myLangCode;
- }
- } else if (myLangCode){
- recognition.lang = myLangCode;
- } else if (myLang){
- recognition.lang = myLang;
- }
+ if ('webkitSpeechRecognition' in window){
+ recognition = new webkitSpeechRecognition();
+ if (myLang && myLangCode){
+ if (myLang.split("-")[0] == myLangCode){
+ recognition.lang = myLang;
+ } else {
+ recognition.lang = myLangCode;
+ }
+ } else if (myLangCode){
+ recognition.lang = myLangCode;
+ } else if (myLang){
+ recognition.lang = myLang;
+ }
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function(){
diff --git a/translate_premium.html b/translate_premium.html
index 20aa723..a653b91 100644
--- a/translate_premium.html
+++ b/translate_premium.html
@@ -795,21 +795,46 @@ function updateTrans(translatedText, isIncremental) {
}
langFrom.addEventListener("change", e => {
myLangCode = e.target.value;
- console.log(myLangCode)
- if (myLang.split("-")[0].toLowerCase() !== myLangCode){
- setStorage("myLang", myLangCode, 999999);
- updateURL("lang="+myLangCode, true);
+ console.log("Source language changed to:", myLangCode);
+
+ // Always update storage and URL, regardless of previous language
+ setStorage("myLang", myLangCode, 999999);
+ updateURL("lang="+myLangCode, true);
+
+ // Always restart recognition with new language
+ if (recognition) {
recognition.onend = null;
recognition.stop();
recognition = null;
- setup();
}
+ myLang = myLangCode; // Update the myLang variable directly
+ setup();
});
langTo.addEventListener("change", e => {
targetCode = e.target.value;
+ console.log("Target language changed to:", targetCode);
updateURL("translate="+targetCode, true);
- console.log(targetCode);
setStorage("targetCode", targetCode, 999999);
+
+ // If there's text in the input field, translate it immediately with the new target language
+ const currentText = document.getElementById("input").value.trim();
+ if (currentText && document.getElementById("enabledTranscription").checked) {
+ if (document.getElementById("fullContext").checked) {
+ let fullText = [];
+ if (second_transcription?.trim()) {
+ fullText.push(second_transcription.trim().replace(/\.?$/, '.'));
+ }
+ if (last_transcription?.trim()) {
+ fullText.push(last_transcription.trim().replace(/\.?$/, '.'));
+ }
+ if (currentText?.trim()) {
+ fullText.push(currentText.trim().replace(/\.?$/, '.'));
+ }
+ translateIncremental(fullText.join(' '));
+ } else {
+ translateIncremental(currentText);
+ }
+ }
});
function updateURL(param, force=false) {
var para = param.split('=');
@@ -937,17 +962,11 @@ function setup(){
if ('webkitSpeechRecognition' in window){
console.log("Setting up webkitSpeechRecognition");
recognition = new webkitSpeechRecognition();
- if (myLang && myLangCode){
- if (myLang.split("-")[0] == myLangCode){
- recognition.lang = myLang;
- } else {
- recognition.lang = myLangCode;
- }
- } else if (myLangCode){
- recognition.lang = myLangCode;
- } else if (myLang){
- recognition.lang = myLang;
- }
+
+ // Always use the current myLangCode
+ recognition.lang = myLangCode || myLang || navigator.language || "en-US";
+ console.log("Recognition language set to:", recognition.lang);
+
recognition.continuous = true;
recognition.interimResults = document.getElementById("incrementalUpdates").checked;
recognition.onstart = function(){