@7y1o/localize 中文文档教程

发布于 3年前 浏览 26 更新于 3年前

Localize

用于国际化项目的轻量级且非常快速的库

Setup

npm i @7y1o/localize

Usage

此示例描述了如何在 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

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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文