⇦ Ramen Development_

How I translated my portfolio with JavaScript and JSON

Cover Image for How I translated my portfolio with JavaScript and JSON

A couple of weeks ago, I made a post about language selection and translations (which you should read). So this week, I should make a post about how I translated my personal portfolio website using just vanilla JavaScript.

I mentioned in the other post that here we use i18n to auto-detect the user's language. However, I didn't want to get into any extra trouble on my personal portfolio to have it ready a bit faster and try something new. So, I came up with a straightforward solution using only JavaScript. Let's get into it.

Create an array

I have a really simple array with the IDs assigned to the different strings on my webpage like this:

texts = [
  "skillstxt",
  "experiencetxt",
  "projectstxt",
  "title",
];

Create JSONs

Each JSON corresponds to a language, and it has the IDs mentioned with the corresponding string in their language:

en = {
  skillstxt: "Skills",
  experiencetxt: "Experience",
  projectstxt: "Projects",
  title: "Software Engineer",
};
es = {
  skillstxt: "Habilidades",
  experiencetxt: "Experiencia",
  projectstxt: "Proyectos",
  title: "Ingeniero de software",
};
fr = {
  skillstxt: "Compétences",
  experiencetxt: "Expérience",
  projectstxt: "Projets",
  title: "Ingénieur logiciel",
};

Additionally, there is an empty JSON which will then have the translation for the selected language.

The translation function

With the array and the JSONs in place, we can now simply iterate through the array. For each element, we assign the string from the corresponding JSON key to the HTML element:

function translate() {
  texts.forEach((t) => {
    document.getElementById(t).innerHTML = json[t];
  });
}

As mentioned before, if we want to change the language, we just assign the language JSON to the current language JSON and call the translation function to replace the strings in the HTML:

function spanish() {
  json = es;
  translate();
}
function english() {
  json = en;
  translate();
}
function french() {
  json = fr;
  translate();
}

These options are available on the top of my page with each language represented with its ISO code, as mentioned in my previous post, not the best option but helps to save some space.

Full code on GitHub.

More entries

Cover Image for How I translated my portfolio with JavaScript and JSON

How I translated my portfolio with JavaScript and JSON

I didn't want to get into any extra trouble on my personal portfolio to have it ready a bit faster and try something new. So, I came up with a straightforward solution using only JavaScript. Let's get into it.

Ivan Orozco
Ivan Orozco
Cover Image for Flags to represent languages?

Flags to represent languages?

Should I use the flags of the countries with the most population speaking that language? what’s the best way to design the options for language switching?

Ivan Orozco
Ivan Orozco
Cover Image for Coding with emojis?

Coding with emojis?

Emojis are truly useful in some cases, they can add emotions to our messages so their true meaning doesn't fly over our recipient's head, but should we use them for coding?

Ivan Orozco
Ivan Orozco
Cover Image for Easy Simple Job backstory

Easy Simple Job backstory

The story behind our game isn´t complicated, inspired by a video narrating a story on Reddit that tells about a guy that gets a simple but suspicious job in an office, and certain scary things happen.

Ramen Development Team
Ramen Development Team
Cover Image for Dear LinkedIn recruiters...

Dear LinkedIn recruiters...

It is important for recruiters to take the time to review a candidate's profile before sending a job offer.

Ivan Orozco
Ivan Orozco
Cover Image for Lexical analyzer automaton

Lexical analyzer automaton

In this article you will understand how to code your own lexical analyzer.

Gustavo Padilla
Gustavo Padilla