@7y1o/localize

Localization utility

Usage no npm install needed!

<script type="module">
  import 7y1oLocalize from 'https://cdn.skypack.dev/@7y1o/localize';
</script>

README

Localize

Lightweight and very fast library for internationalizing projects

Setup

npm i @7y1o/localize

Usage

This example describes how to use the library in Svelte:

<script>
    import Localize from '@7y1o/localize';
    
    // Libs
    import LibActions from '...';
    import LibPage from '...';
    
    // Initialize library
    const loc = new Localize(
        ['ru', 'en', 'ua'],     // Languages list
        'ru',                   // Default language
        LibActions, LibPage     // Localization libraries
    );
    
    // Form controller
    const controller = {
        login: '',
        password: ''
    };
    
    // Submit action
    const submitForm = () => {
        // some actions
    };
</script>

<main>
    <div class="form">
        <h1>{loc.get('form_heading')}</h1>
        <p>{loc.get('form_subtext')}</p>
        
        <input type="text" placeholder={loc.get('form_login')} bind:value={controller.login}/>
        <input type="password" placeholder={loc.get('form_password')} bind:value={controller.password}/>
        
        <button on:click={submitForm}>{loc.get('action_submit')}</button>
    </div>
</main>

Available methods

  • constructor (langs, defLang, ...libs):
    • parameters:
      • langs { array(string) } - list of available languages
      • defLang { string } - default language if not defined in localstorage
      • libs { array(any) } - structure of translation library
  • get (key): returns translated string or key if not found
    • parameters:
      • key { string } - desired translation key
  • switch (lang): switch to language described in languages list
    • parameters:
      • lang { string } - language in languages list