A simple way to have internationalization on static websites.
You can install using yarn or by cloning the repo.
$ yarn add https://github.com/ianwelerson/multilanguagejs.git
$ git clone https://github.com/ianwelerson/multilanguagejs.git
- Import in js file
import MultilanguageJS from 'multilanguagejs';
- Initialize the MultilanguageJS
// As a first param you need to pass an array of accepted languages
// As a second param you need to a language to fallback.
let multilanguage = new MultilanguageJS(
['pt-BR', 'en-US'],
'pt-BR'
);
- Set the active language
// To use the browser language you can call 'setLanguageByBrowser()':
multilanguage.setLanguageByBrowser();
// To set manually a language you call 'setLanguage('language')':
multilanguage.setLanguage('pt-BR');
- Insert the HTML content
// Create a template tag with attr 'type="language-group"'
<template type="language-group">
// Inside then you put all your variants for this content using the attr 'language="your-Language"'
// The tag used can be any one, just put the attr for each language variation
<h3 language='pt-BR'>Olá Pessoal!</h3>
<h3 language='en-US'>Hi everyone!</h3>
</template>
- Others
// To change the language you can use 'setLanguage("your-Language")'
multilanguage.setLanguage('pt-BR');
// To get actual language you can use 'getActiveLanguage()'
multilanguage.getActiveLanguage();
// To get an array of accepted language you can use 'getAcceptedLanguages()'
multilanguage.getAcceptedLanguages();
// To get the default language you can use 'getDefaultLanguage()'
multilanguage.getDefaultLanguage();
// To use the browser language as page language you can use 'setLanguageByBrowser()'
multilanguage.setLanguageByBrowser();
- Ian Welerson IanWelerson.com
This project is licensed under the MIT License