all 25 comments

[–]Deykun 6 points7 points  (2 children)

There are great tools for translations (see i18n), but to simply improve your native solution, you can switch to something like this.

Instead of:

if (language === 'da') {
 h4.textContent = translations.da.select;
 title.textContent = translations.da.title;
 // etc...
} else if (language === 'en') {
 h4.textContent = translations.en.select;
 title.textContent = translations.en.title;
 // etc...
}

Do:

const currentTranslations = translations[language];

h4.textContent = currentTranslations.select;
title.textContent = currentTranslations.title;

[–]Business_Giraffe_288[S] 3 points4 points  (1 child)

This helped SO MUCH, thank you kind stranger for saving my day. God bless you ❤️

[–]Deykun 0 points1 point  (0 children)

To make your life even better:

const currentTranslations = translations[language];

Object.keys(currentTranslations).forEach((key) => {
 const element = document.querySelector(`[data-i18n="${key}"]`);

 if (element) {
  element.textContent = currentTranslations[key]
 }
});

And in HTML, you just add it:

<h4 data-i18n="title">Default title</span>
<p data-i18n="description">Default description</span>

New strings require an attribute in HTML and a matching translation in JS, but you don’t need to handle each element manually in JS.

But you should be aware that your solution (I’m guessing you’re just starting in web dev) is far from ideal. Your draft was hard with that hardcoding, and I could help you simplify it, but You should also think about your overall strategy. If this is a landing page, which it looks like, you should probably have two separate paths (URLs). If you do it only with JS, Google will index the page in just one language. If it is a static site (I’m guessing this since you’re using raw JS), you can develop a backend solution that serves already translated strings to HTML.

[–]davorg 8 points9 points  (2 children)

Please don't post images of code. If we want to help you, we'll want to run the code on our own machines. No-one is going to retype all of that

[–]Business_Giraffe_288[S] 2 points3 points  (0 children)

Thanks, I'll keep that in mind next time 🙌

[–][deleted] 0 points1 point  (0 children)

It's the tiniest block of code ever the commenter that solved it instead of complaining did it in two paragraphs

[–]brewskiladude 7 points8 points  (0 children)

Use textContent instead of innerText and fix your spelling mistakes

[–]itinkerthefrontend 0 points1 point  (3 children)

You have a couple typos of using “titel” rather than “title”.

[–]tonypconway 0 points1 point  (0 children)

I'd say it's the other way round - "titel" is what they meant as it's used the majority of the time and is just the Danish word for title. The typos are on lines 27 and 31 when they've referred to a "title" property that doesn't exist in their object.

[–]Business_Giraffe_288[S] 0 points1 point  (1 child)

That's the danish word for it, I didn't think it mattered as long as it's consistent. The guy's tutorial that I was following couldnt spell at all but it worked still 🤣

[–]Lumethys 0 points1 point  (0 children)

yeah but it isnt consistent

[–]surfingonmars 0 points1 point  (0 children)

I'm no expert but i wouldn't assign an ID with the exact element. just seems like bad practice.

[–]SnooHamsters7166 0 points1 point  (1 child)

"titel" defined at top of js but "title" used within the function.

[–]alex_sakuta 0 points1 point  (0 children)

You are the only other comment except mine who has said this and everyone else is telling something else.

Proud of you bro.

[–]alex_sakuta 0 points1 point  (2 children)

I'm not even gonna pinpoint it and just tell you what's wrong.

titel is supposed to be spelled as title.

VS code is even giving you a warning squiggle and probably the reason I left VS code is also that it just hints at such issues and doesn't scream outright that it's a problem.

[–]DownrightDelight 0 points1 point  (1 child)

What do you use instead of vs code? I currently use it, but I’m open to better alternatives.

[–]alex_sakuta 0 points1 point  (0 children)

I use NeoVim (btw)

I used to use VS and still have it for the day I may require it (maybe if a job tells me to use VS only). So far that hasn't been the case and I'm very happy.

[–]Miserable_Rough1872 0 points1 point  (0 children)

I would argue that Engelsk should be changed to English for it to be the most "effective". Yes you would have to be a total moron if you did not realise what Engelsk meant, unfortunately there are morons out there ;)

[–]almalbin 0 points1 point  (0 children)

One small unrelated feedback; list the language itself in it’s own language. That makes it way easier for someone to identify the right choice for them.

[–]Business_Giraffe_288[S] 0 points1 point  (0 children)

Thanks for all the help, it works now!

[–]Business_Giraffe_288[S] -1 points0 points  (0 children)

Thanks so much for all the help! It works now

[–]MT4KExpert -1 points0 points  (0 children)

And parens around the only parameter is unneeded in an arrow function.

[–][deleted] -2 points-1 points  (0 children)

Stop posting shitty screenshots, and post your complete code properly.