si18n.js

v





#
#
npm
npm i si18n.js
yarn add si18n.js
CDN ES6 Modules
<!-- si18n.js latest version @latest -->
<script type="module" src="https://si18n.js.bruxelles.dev/si18n.js"></script>
<script type="module" src="https://si18n.js.bruxelles.dev/si18n.min.js"></script>

<!-- UNPKG -->
<script type="module" src="https://unpkg.com/si18n.js"></script>
<script type="module" src="https://unpkg.com/si18n.js@latest/si18n.js"></script>
<script type="module" src="https://unpkg.com/si18n.js@latest/si18n.min.js"></script>

<!-- jsDelivr -->
<script type="module" src="https://cdn.jsdelivr.net/npm/si18n.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/si18n.js@latest"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/si18n.js@latest/si18n.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/si18n.js@latest/si18n.min.js"></script>

<!-- ESM -->
<!-- (?target=es2015|es2022|esnext) -->
<script type="module" src="https://esm.sh/si18n.js@latest"></script>

<script type="module" src="https://cdn.jsdelivr.net/npm/si18n.js@latest/+esm"></script>

<!-- UNPKG -->
<script type="module">
  import Si18n from "https://cdn.jsdelivr.net/npm/si18n.js@latest/si18n.min.js";
  // ...
</script>
#
NPM
import Si18n from "si18n";
let loc = new Si18n();
loc.init({
  locales: {}, // { "en": {...}, "fr": {...}}
  lang: "fr",
  fallbackLang: "en",
  translate() {
    document.querySelector(".lang").innerText = loc.getLocale();
    document.querySelector(".title").innerText = loc.t("nested.title");
    // ...
  },
  onLocaleChanged() {
    // ...
  }
});
<div data-si18n="site.title"></div>
<div data-si18n="site.title" data-si18n-default="false" data-si18n-title="true"></div>

    <!-- 1. HTML -->
    <p id="intro"></p>
    // 2. JAVASCRIPT
    // 2.1.
    let data = {
      "fr": {
        "intro": "%{name} a eu %{age} ans cette année.",
        "test": "Ceci est un test."
      },
      "en": {
        "intro": "%{name} turned %{age} this year.",
        "test": "This is a test."
      }
    };
    
    loc.init({
      // ...
      translate() {
        document.querySelector("#intro").innerText = loc.t("intro", {
          age: (new Date().getFullYear()) - birthYear,
          name: "John Doe"
        });
    
        console.log(loc.t("test")) // "Ceci est un test."
        // ...
      }
    });
    // 2.2.
    let data = {
      "fr": { "intro": "Mon ami s'appelle %{0} et travaille chez %{1}." },
      "en": { "intro": "My friend's name is %{0} and he works at %{1}." }
    };
    
    loc.init({
      // ...
      translate() {
        document.querySelector("#intro").innerText = loc.t("intro", {
          0: "Georges",
          1: "CRFEV"
        });
        // ...
      }
    });
    # (7)
    constructor options:object
    init options:object
    setLocale lang:string
    getLocale void
    getLocales void
    t JSONPath:string,
    :object
    static async getJSON URL: string,
    callback(res: object): fn
    toJSON void
    locales {}
    path null
    availableLocales []
    lang ''
    fallbackLang ''
    activeClass ''
    togglersSelector ''
    isTogglerSelect false
    saveLang true
    saveAs 'lang'
    reloadPage false
    translate function() {}
    onLocaleChanged function() {}