Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to create multilingual static pages without frameworks
Federico Navarrete
Federico Navarrete

Posted on • Edited on • Originally published atsupernovaic.blogspot.com

     

How to create multilingual static pages without frameworks

From time to time, we have the experience of building websites without frameworks like Angular, Vue.js, or React. Perhaps, for fun, we are new or we just want something extremely simple.

In my case, I started building mypersonal website several years ago. In those days, I didn't know Angular, and it was a little bit messy to support everything I needed. I didn't consider myself a front-end developer. It was more like my hobby.

Something important in my case was to add multilingual support since I speak English and Spanish fluently. This was a little bit messy since I couldn't find any good library or solution. After several attempts, I came up with the following pieces of code that can simplify your life also.

I have two files, one for English and one for Spanish, where I store the translations in JSONs as constants.If you wonder why I don't store only the JSONs, I had several issues consuming the files later. I got several errors, and most solutions were for Node.js only.

lang-en.js

consttranslations={"telephone":"Telephone","email":"Email","contactMe":"Contact me"};
Enter fullscreen modeExit fullscreen mode

lang-es.js

consttranslations={"telephone":"Teléfono","email":"Email","contactMe":"Contáctame"};
Enter fullscreen modeExit fullscreen mode

The following script is for reading the scripts:

get-script.js

constgetScript=url=>newPromise((resolve,reject)=>{constscript=document.createElement('script');script.src=url;script.async=true;script.onerror=reject;script.onload=script.onreadystatechange=function(){constloadState=this.readyState;if(loadState&&loadState!=='loaded'&&loadState!=='complete')return;script.onload=script.onreadystatechange=null;resolve();}document.head.appendChild(script);});
Enter fullscreen modeExit fullscreen mode

And the last script is for setting the values in your HTML:

translations.js

letlang='en';letnLang=(navigator.languages?navigator.languages[0]:(navigator.language||navigator.userLanguage)).split('-')[0];letsupportedLang=['en','es'];lang=supportedLang.includes(nLang)?nLang:lang;getScript(`lang-${lang}.js`).then(()=>{document.querySelectorAll('[data-translation]').forEach(item=>{item.innerHTML=translations[`${item.dataset.translation}`];});}).catch((e)=>{console.error(e);});
Enter fullscreen modeExit fullscreen mode

So, how do you consume and configure your HTML? You define adata-* attribute calleddata-translation with the value you expect from the "JSON" like this:

<scriptsrc="get-script.js"></script><scriptsrc="translations.js"></script><pdata-translation="contactMe"></p><pdata-translation="telephone"></p><pdata-translation="email"></p>
Enter fullscreen modeExit fullscreen mode

And that's all. Now, you can create your static multilingual pages without frameworks.

Follow me on:

PersonalLinkedInYouTubeInstagramCyber ProphetsSharing Your Stories
PersonalLinkedInYouTubeInstagramRedCircle PodcastRedCircle Podcast

sponsor me

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

👨‍💻 Technovator, ✍️ Author and 🎤 Speaker
  • Location
    Luxembourg
  • Education
    MSc in Computer Science and Information Technologies
  • Pronouns
    he/him
  • Work
    Cloud Architect and IT Strategy Consultant at the European Commission
  • Joined

More fromFederico Navarrete

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp