Possibility to translate weekdays and months

You are able translate almost everything but weekdays and months.

2 Likes

Hi @Jan_Hansen :wave:

Thank you for bringing this to our attention!

Our devs are working on it. I’ll keep you in the loop regarding any changes :slightly_smiling_face:

As I was a bit in a hurry, I added the following code to my website to overwrite the text in the opening hour widget. In case it takes longer, and you want to get it done in the meantime.

// Function to translate text
function translateText(text) {
  const translations = {
    // Weekdays
    'Monday': 'Montag',
    'Tuesday': 'Dienstag',
    'Wednesday': 'Mittwoch',
    'Thursday': 'Donnerstag',
    'Friday': 'Freitag',
    'Saturday': 'Samstag',
    'Sunday': 'Sonntag',
		// Add translations for other stuff if needed
    '24 December': '24. Dezember',
    '25 December': '25. Dezember',
    '26 December - 29 December': '26. - 29. Dezember',
    '30 December - 31 December': '30. - 31. Dezember',
    '1 January': '1. Januar',
    '2 January - 5 January': '2. - 5 Januar',
  };

  return translations[text] || text;
}

// Function to observe changes in the document and translate text when needed
function observeChanges() {
  const targetNode = document.body;

  const config = { childList: true, subtree: true };

  const callback = function (mutationsList) {
    for (const mutation of mutationsList) {
      if (mutation.type === 'childList') {
        translateTextNodes(mutation.target);
      }
    }
  };

  const observer = new MutationObserver(callback);

  observer.observe(targetNode, config);

  // Translate text nodes in the initial document
  translateTextNodes(document);
}

// Function to translate text nodes
function translateTextNodes(node) {
  if (node.nodeType === Node.TEXT_NODE) {
    node.textContent = translateText(node.textContent.trim());
  } else if (node.nodeType === Node.ELEMENT_NODE) {
    node.childNodes.forEach(translateTextNodes);
  }
}

document.addEventListener('DOMContentLoaded', function () {
  // Observe changes in the document and translate text when needed
  observeChanges();
});

Not sure if it’s the best solution, but it works.
The weekdays was easy, as they are seperate. For the months that was not the case, so I just took the actual text in the “special business hours” and translated the entire string.

2 Likes

Hey @Maic_Fankhauser :wave:

That’s fantastic, thanks a million for sharing your workaround with us!

Our devs are doing their best to resolve the issue as soon as possible. We’ll keep you updated :slightly_smiling_face:

@Maic_Fankhauser Our devs have checked your code and they’ve confirmed that it’s correct. However, this solution might have a negative effect on your website’s performance, as observer is applied to the whole document.

Anyway, thank you so much for sharing your solution, it’s always very much appreciated! :slight_smile:

@Jan_Hansen you can use this code, but it may slow down the loading speed of your website.

@Jan_Hansen @Maic_Fankhauser Guys, the issue is fixed!

Please check your widgets and let me know how they work :slightly_smiling_face:

Hey max
I just had a look at the business hours widgets in settings → Language and i can’t find the entries for Weekdays and months.

1 Like

Hi @Maic_Fankhauser :wave:

There is no option to translate the weekdays and months manually.

However, they automatically get translated when choosing a specific language:

The other day it did not translate to Danish, but I see that does now, so problem solved :blush:

Merry christmas

You are always welcome @Jan_Hansen

Merry Christmas :wink:

Thanks for the christmas present :wink: and sorry for overseeing the changes

No sweat!

Merry Christmas :wink: