npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xaro/svelte-i18n

v0.0.8

Published

Svelte i18n library with dynamic add/change translations

Downloads

17

Readme

@xaro/svelte-i18n

Library for Svelte for easy localization of your application

  • Dynamically add and remove translations
  • Pass params to translation
  • Availability of extended translation fields:
<script>
    init({
        en: {
            rules: {
                'required':             '{{attr}} cannot be empty',
                'required.name':        'Enter your name',          // Extended translation
                'required.name.first':  'Enter your first name',    // Extended translation
            }
        }
    }, 'en');
</script>

{$t('rules.required', { attr: 'Attribute' }) /* "Attribute cannot be empty" will be displayed */}
{$t([ 'rules', 'required.name' ])            /* "Enter your name" will be displayed */}
{$t([ 'rules', 'required.etc' ])             /* "{{attr}} cannot be empty" will be displayed */}
{$t([ 'rules', 'required.name.first' ])      /* "Enter your first name" will be displayed */}
{$t([ 'rules', 'required.name.second' ])     /* "Enter your name" will be displayed (used [ 'rules', 'required.name' ] for non-existing path) */}
{$t([ 'rules', 'smth' ])                     /* "rules.smth" will be displayed (non-existing path) */}
{$t([ 'rules', 'smth' ], {}, 'no exists')    /* "no exists" will be displayed (non-existing path) */}

Usage:

<script lang="ts">
    import {
        type Translation,       // Type of locale translations
        type InitOptions,       // Type of init method options argument type
        type CurrentLocaleStore,// currentLocale store (Extended Writable with setNullIf method)
        currentLocale,          // Writable locale store (returns string or null)
        allLocales,             // Readonly all loaded locales (Readonly< Set<string> >)
        init,                   // (Optionaly)
                                // - Load initial translations
                                // - Set currentLocale
                                // - Set defaultTranslation for non-existing paths
        hasLocale,              // Checks existing locale (returns boolean)
        removeLocale,           // Removes the locale and its translations
        addTranslation,         // Adds translation to path (Replace EXISTING translations in path)
        setTranslation,         // Sets translation to path (Replace ALL translations in path)
        removeTranslation,      // Remove locale's translation by path
        t                       // Main derived store to show translation
    } from '@xaro/svelte-i18n';

    const langs = {
        en: {
            caption: 'Hello',
            rules: {
                'required': '{{attr}} cannot be empty',
                /**
                 * Extended fields works only when path given in array!!!
                 * @example {$t([ 'rules', 'required.name' ])}
                 * @example {$t([ 'rules', 'required.name.first' ])}
                 */
                'required.name': 'Enter your name',
                'required.name.first': 'Enter your first name',
            },
            // Also valid:
            'rules.2': {
                /**
                 * works:
                 * @example {$t([ 'rules.2', 'required' ])}
                 * 
                 * @example {$t([ 'rules.2', 'required.name' ])}
                 */
                required: 'Attribute required'
            }
        }
    }

    // Here we load the initial translation
    // Set the locale
    // Set default translate for non-existing translation paths
    init({
        translations: langs,
        defaultTranslations: {
            en: 'No translation found'
        },
        locale: 'en',
    });
    // === OR ===
    setTranslation('en', '', langs.en);
    $currentLocale = 'en'; // OR currentLocale.set('en')
    // default translation can set in init function OR pass defaultValue to $t as 3rd parameter


    // Here we are adding translation to an existing locale:
    addTranslation('en', 'rules.unique', '{{attr}} must be unique');
    // Here we add extended translation (for email field in this case):
    addTranslation('en', [ 'rules', 'unique.email' ], 'A user with this email already exists');
    // We can only access the extended translation fields by passing the path in the array,
    // where the extended fields must be in one array element, in this case it is "apple"
    
    // Now the locale "en" has the value:
    // {
    //     caption: 'Hello',
    //     rules: {
    //         'required': '{{attr}} cannot be empty',
    //         'required.name': 'Enter your name',
    //         'unique': '{{attr}} must be unique',
    //         'unique.email': 'A user with this email already exists',
    //     },
    // }


    // We can also add the entire translation object:
    addTranslation('en', [ 'rules' ], {
        'min': 'The {{attr}} must be at least {{num}} characters long',
        'min.name': 'The name cannot be shorter than 5 characters'
    });
    // Now the locale "en" has the value:
    // {
    //     caption: 'Hello',
    //     rules: {
    //         'required': '{{attr}} cannot be empty',
    //         'required.name': 'Enter your name',
    //         'unique': '{{attr}} must be unique',
    //         'unique.email': 'A user with this email already exists',
    //         'min': 'The {{attr}} must be at least {{num}} characters long',
    //         'min.name': 'The name cannot be shorter than 5 characters'
    //     },
    // }


    // We can also clear the translation by specifying the required path and empty object as value:
    setTranslation('en', 'rules', {});
    // Now the locale "en" has the value:
    // {
    //     caption: 'Hello',
    // }
    // Inside code used `delete translations[locale][...path...]`


    // To add a new locale call "setTranslation" or "addTranslation" with the required locale and
    // empty string or array as path
    setTranslation('ua', [], {
        caption: 'Привіт',
    });
    // New locale "ua" has the value:
    // {
    //     caption: 'Привіт',
    // }

    // Removes a locale along with its translation
    removeLocale('ua');
</script>

{$t('caption') /* "Hello" will be displayed */}

<!-- "Hello 1" will be displayed here -->
{$t('rules.required') /* "{{attr}} cannot be empty" will be displayed */}
{$t('rules.required', { attr: 'Name' }) /* "Name cannot be empty" will be displayed */}
{$t([ 'rules', 'required.name' ]) /* "Enter your name" will be displayed. Extended fields works only when path given in array!!! */}
{$t([ 'rules', 'required.name.first' ]) /* "Enter your first name" will be displayed */}
{$t([ 'rules', 'required.name.second' ]) /* "Enter your name" will be displayed (used [ 'rules', 'required.name' ], because 'required.name.second' does not exist) */}
{$t([ 'rules', 'required.name.second' ], {}, 'default value will not be displayed') /* "Enter your name" will be displayed. Default value works only for general non-existing properties, not extended */}
{$t([ 'rules', 'required.email' ]) /* "{{attr}} cannot be empty" will be displayed from `fields.required` */}
{$t([ 'rules', 'required.email' ], {}, 'No translation') /* "No Translation" will be displayed */}

{Array.from($allLocales).join(', ') /* Get all locales (Readonly< Set<string> >) */}