Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
How to set html lang and base using static-i18n for translations

I create landing page in one .html file using pure html-css-js (to be able to run it on simple file serwer like apache). For SEO I decide to generate separate .html files for each supported language - to do it I use static-i18n which take template html file with translations keys and change it to separate html files (one for each language). My code with translaton keys is inside ./src directory, the translations shoud be generated inside ./dist folder using

static-i18n --fixPaths false --selector [i18n] --useAttr false -o ./dist -l en -i en -i pl ./src

The ./dist structure is following (I copy there assets and .htaccess in separate way in my build script):

enter image description here

the main language (english) is in ./dist/index.html file, and other languages are in subdirecotry e.g. ./dist/pl/index.html. Example translation inside ./src/index.html (the i18n attribute means that inside tag content is translation key)

<div class="main__title" i18n >main.title</div>

I have two problems - how to set in dynamic way:

  • the lang inside tag <html lang="??"> (but without changing the way of giving translations keys as in html example above)
  • proper href value inside <base href="..."> tag (for properly reading assets) - in main language should be <base href="./"> but in other languages (which are in subdirectories) shoud be <base href="../">
almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Here are my solutons - but may be there exists better

  • add key "lang" to your translation *.json files and use it in tag as follows
<html data-attr-t lang-t="lang">
  • remove tag from html and use following js code at the top of tag to generate proper <base>

<script defer>
  // CAUTION! This script must be in <head> tag 
  // (<base> must be set before <body> loading)
  const mainLanguage = 'en'; 

  // set base in dynamic way
  const newBase = document.createElement('base');
  const lang = document.documentElement.lang;
  newBase.setAttribute('href', lang == 'en' ? './' : '../');
  document.getElementsByTagName('head')[0].appendChild(newBase);
</script>

or in same way like in lang use <base data-attr-t href-t="base"></base> (and add key "base":"./ to en.json and "base":"../" pl.json)

almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda