Here is the updated README based on your request for using the I18n
class for importing and setting translations:
A Rust and WebAssembly project template for managing internationalization (i18n) translations in web applications using wasm-pack.
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.
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.
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();
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"
});
Gets all translations for a specific locale.
const translations = i18n.getTranslations('en');
console.log(translations);
Deletes all translations for a specific locale.
i18n.delTranslations('en');
Deletes a specific translation key for a locale.
i18n.delTranslation('en', 'hello');
Gets the translation for a specific key in a locale.
const translation = i18n.getTranslation('en', 'hello');
console.log(translation);
Checks if a specific translation key exists in a locale.
const exists = i18n.hasTranslation('en', 'hello');
console.log(exists);
Checks if a specific locale exists.
const exists = i18n.hasLocale('en');
console.log(exists);
Formats a translation string with provided arguments.
const formatted = i18n.formatTranslation('en', 'welcome', { username: 'Alice' });
console.log(formatted);
Loads translations from a remote URL.
await i18n.loadTranslations('https://example.com/translations.json');
Gets all available locales.
const locales = await i18n.getAllLocales();
console.log(locales);
Gets all translations for a specific locale.
const translations = await i18n.getAllTranslationsForLocale('en');
console.log(translations);
Clears all translations.
i18n.clearAllTranslations();
Updates a specific translation key for a locale.
i18n.updateTranslation('en', 'hello', 'Hello, World!');
Gets all translations for all locales.
const all_translations = i18n.getAllTranslations();
console.log(all_translations);
Checks if a specific key exists in the translations for a locale.
const exists = i18n.hasKeyInTranslations('en', 'hello');
console.log(exists);
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" }, ... } |
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.
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!