Reply To: best way to add translations?
Hey there, I tried using the translation part of the bbp style pack plugin, while assuming, that once I was able to fix the translations I would then fix the CSS styling issues I have mentioned above (this would just take a little work on my behalf). Anyway, even though it says you have to play around with it to get it right (e.g. add the full stop if there is one etc.), I was not able to implement the translations. I am sure it has something to do with the way my site is set up and not the plugin itself, but alas, I had to find a different option.
So, here is my frontend Javascript solution. I noticed that I have to put e.g. “weeks” before “week” in the object for it to work (i.e. plural before singular). For anyone who wants to know, the word “aktualisiert” refers to the English word “updated”, (as the verb goes to the end of the sentence in German it is slightly tricky to just translate “this was last updated…..”).
I am not sure if anyone needs this, but as I require help with my website from time to time from people like those at bbpress (thx btw), if I can be of any help, I would like to. This is an IIFE btw:
/* WB- translate some dodgy sentences (part English/ part German) */
var translate = function () {
//check if the page is a forum page. If not, return function
var isForum = document.querySelector("#bbpress-forums");
if (!isForum) return;
// find if either forum or topic description (topic is one level lower in the forum where people can answer individual posts. forum is where all the sub-topics are listed
var forum = document.querySelector(".bbp-forum-description");
var description = document.querySelector(".bbp-topic-description");
//If not on either the forum description page or the forum topic page, return
if (!forum && !description) return;
//set a variable to add the innerHTML to
var d_text;
if (forum) {
// if forum has no topics make no tranlation changes and hide the "Oh bother.... div"
if (forum.innerText === "Dieses Forum ist leer."){
var oh_both_text = document.querySelectorAll(".bbp-template-notice");
oh_both_text[1].style.display = "none";
return;
}
d_text = forum.innerHTML;
}
if (description) {
d_text = description.innerHTML;
}
// create object with list to translate (name: = English, value = Deutsch)
var d_obj = {
"This forum has": "Dieses Forum hat",
"This topic has": "Dieses Thema hat",
"This category has":"Diese Kategorie hat",
"This topic is empty":"Dieses Thema ist leer",
"hidden":"verdeckt",
"and was last updated": "und wurde zuletzt",
"seconds": "Sekunden",
"minutes": "Minuten",
"hours": "Stunden",
"days":"Tagen",
"day": "Tag",
"weeks":"Wochen",
"week": "Woche",
"months": "Monaten",
"month": "Monat",
"years":"Jahren",
"year":"Jahr",
"by": "von"
}
// use Replace method to run through innerHTML and replace English text with German
d_text = d_text.replace(/This forum has|This topic has|This category has|This topic is empty|hidden|and was last updated|seconds|minutes|hours|days|day|weeks|week|months|month|years|year|by/gi, function (matched) { return d_obj[matched]; });
//add "aktualisiert" to end of sentence
if (d_text.indexOf("und wurde zuletzt") > -1) {
d_text = d_text.slice(0,-1).concat(" aktualisiert."); }
//update the innerHTML of either the forum or the topic page
if (forum) { forum.innerHTML = d_text; }
if (description) { description.innerHTML = d_text; }
}();