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

wasm-i18n

v0.6.1

Published

A lightweight, high-performance internationalization module for web applications using WebAssembly and Rust.

Downloads

1,110

Readme

Here is the updated README based on your request for using the I18n class for importing and setting translations:


wasm-i18n

A Rust and WebAssembly project template for managing internationalization (i18n) translations in web applications using wasm-pack.

About

This project provides a set of functions to manage internationalization (i18n) translations in web applications. It allows you to set, get, delete, and update translations for different locales, as well as load translations from a remote URL.

📦 Install

You can install the package via npm:

npm install wasm-i18n

Or if you are using yarn:

yarn add wasm-i18n

Make sure you have wasm-bindgen and wasm-pack set up in your project to build and run this package.

🚴 Usage

Example Usage

import { I18n } from 'wasm-i18n';

let i18n = new I18n();

async function run() {
    await i18n.setTranslations('en', {
        "welcome": "Hello {username}"
    });

    await i18n.setTranslations('en', {
        "test": {
            "data": '1111'
        }
    });

    const tr = i18n.getTranslations('en');
    console.log(tr);

    const translation = i18n.getTranslation('en', "welcome");
    console.log(translation);

    const formatted = i18n.formatTranslation('en', 'welcome', { username: 'Alice' });
    console.log(formatted);

    document.getElementById('welcome-message').innerText = formatted;

    const test = i18n.getTranslation('en', "test.data");
    console.log(test);
}

run();

API Documentation

setTranslations(locale: string, obj: any): void

Sets translations for a specific locale. If translations already exist for the locale, they will be merged with the new translations.

i18n.setTranslations('en', {
    "hello": "Hello",
    "world": "World"
});

getTranslations(locale: string): any

Gets all translations for a specific locale.

const translations = i18n.getTranslations('en');
console.log(translations);

delTranslations(locale: string): void

Deletes all translations for a specific locale.

i18n.delTranslations('en');

delTranslation(locale: string, key: string): void

Deletes a specific translation key for a locale.

i18n.delTranslation('en', 'hello');

getTranslation(locale: string, key: string): any

Gets the translation for a specific key in a locale.

const translation = i18n.getTranslation('en', 'hello');
console.log(translation);

hasTranslation(locale: string, key: string): boolean

Checks if a specific translation key exists in a locale.

const exists = i18n.hasTranslation('en', 'hello');
console.log(exists);

hasLocale(locale: string): boolean

Checks if a specific locale exists.

const exists = i18n.hasLocale('en');
console.log(exists);

formatTranslation(locale: string, key: string, args: any): string

Formats a translation string with provided arguments.

const formatted = i18n.formatTranslation('en', 'welcome', { username: 'Alice' });
console.log(formatted);

loadTranslations(url: string): Promise<void>

Loads translations from a remote URL.

await i18n.loadTranslations('https://example.com/translations.json');

getAllLocales(): Promise<Array<string>>

Gets all available locales.

const locales = await i18n.getAllLocales();
console.log(locales);

getAllTranslationsForLocale(locale: string): Promise<any>

Gets all translations for a specific locale.

const translations = await i18n.getAllTranslationsForLocale('en');
console.log(translations);

clearAllTranslations(): void

Clears all translations.

i18n.clearAllTranslations();

updateTranslation(locale: string, key: string, value: any): void

Updates a specific translation key for a locale.

i18n.updateTranslation('en', 'hello', 'Hello, World!');

getAllTranslations(): any

Gets all translations for all locales.

const all_translations = i18n.getAllTranslations();
console.log(all_translations);

hasKeyInTranslations(locale: string, key: string): boolean

Checks if a specific key exists in the translations for a locale.

const exists = i18n.hasKeyInTranslations('en', 'hello');
console.log(exists);

Getter Methods

| Method | Description | Example | |----------------|-----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------| | locales | Retrieves all available locales. | js let locales = i18n.locales; console.log(locales); // ["en", "fr", "de", ...] | | translations | Retrieves all translations for all locales. | js let translations = i18n.translations; console.log(translations); // { "en": { "hello": "Hello" }, ... } |

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


Let me know if you need further modifications!