Thanks a lot for sharing this work around! It’s awesome. I didn’t even know you could add something on the navbar without editing the template html on the server.
Did you come up with this yourself by manually inspecting and trial or is there documentation to do such things?
I love the /all-products web page but I don’t think I can provide any translations there as I can’t even find it’s edit page on the desk app. Perhaps can be done by creating another custom html web page to replace it.
For now I came up with another solution, I made slight changes to @roquegv 's contribution to send webpage to same URL with “?_lang=de” addition
// add language picker in navbar
$("#navbarSupportedContent").after("<select id='language-select' class='form-control' style='margin-left: 10px; width: auto;'><option value='en'>English</option><option value='de'>Deutsch</option></select>");
// get the preferred language in this session and set it on the language picker
var preferred_language = frappe.get_cookie("preferred_language");
if (preferred_language){
$('#language-select').val(preferred_language);
}
// whenever the language changes, redirect to its corresponding page in the other language
$('#language-select').on('change', function() {
frappe.call("frappe.translate.set_preferred_language_cookie", {
preferred_language: this.value
}).then(function() {
var preferred_language = frappe.get_cookie("preferred_language");
var english_translation = window.location.href.split("?").shift().split("/").pop() + '?_lang=en'; // add the path to the english translation
var german_translation = window.location.href.split("?").shift().split("/").pop() + '?_lang=de'; // add the pathto the german translation
if (preferred_language === "en" && english_translation){
window.location = english_translation;
} else if (preferred_language === "de" && german_translation){
window.location = german_translation;
}
});
});
This works well if you couple it with the Translations doc and you edit your html as described in this post → Multilingual website feature - #3 by KanchanChauhan
Advantage is default website navbar also gets translated into German. I’ve translated the privacy policy page and when language is selected it goes to the same url with “?_lang=de” addition and it translates to German.
One problem now is that although the tag remembers the language cookie, I don’t know how to implement the JS logic to go to “?_lang=de” version of the next URL. Right now if I go to home page from the German Privacy Policy, I land on the english homepage.