
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@profullstack/localizer
Advanced tools
Simple localization and internationalization library with RTL support
A simple localization and internationalization library with RTL support.
npm install @profullstack/localizer
# or
yarn add @profullstack/localizer
# or
pnpm add @profullstack/localizer
import { localizer, _t } from '@profullstack/localizer';
// Load translations
localizer.loadTranslations('en', {
'hello': 'Hello',
'welcome': 'Welcome, ${name}!'
});
localizer.loadTranslations('fr', {
'hello': 'Bonjour',
'welcome': 'Bienvenue, ${name}!'
});
localizer.loadTranslations('ar', {
'hello': 'مرحبا',
'welcome': 'مرحبًا، ${name}!'
});
// Set the current language
localizer.setLanguage('fr');
// Translate a key
console.log(_t('hello')); // Output: Bonjour
// Translate with interpolation
console.log(_t('welcome', { name: 'John' })); // Output: Bienvenue, John!
The library provides utilities for automatically translating DOM elements:
// Add data-i18n attributes to your HTML
// <h1 data-i18n="welcome">Welcome</h1>
// <input placeholder="Enter your name" data-i18n-placeholder="enter_name">
// Translate all elements with i18n attributes in the document
localizer.translateDOM();
// Translate elements within a specific container
const container = document.querySelector('.my-container');
localizer.translateContainer(container);
// Set up automatic translation of dynamically added elements
localizer.observeDOM();
The library automatically detects Right-to-Left (RTL) languages and provides methods to check if a language is RTL:
// Check if the current language is RTL
const isRTL = localizer.isRTL();
// Check if a specific language is RTL
const isArabicRTL = localizer.isLanguageRTL('ar'); // true
const isEnglishRTL = localizer.isLanguageRTL('en'); // false
// Apply RTL direction to the document
localizer.applyRTLToDocument();
// The library dispatches an event when the language changes
window.addEventListener('language-changed', (event) => {
const { language, previousLanguage, isRTL } = event.detail;
// RTL direction is automatically applied when using applyRTLToDocument()
// but you can also handle it manually:
document.documentElement.dir = isRTL ? 'rtl' : 'ltr';
if (isRTL) {
document.body.classList.add('rtl');
} else {
document.body.classList.remove('rtl');
}
});
// In a browser environment
async function loadTranslations() {
await localizer.loadTranslationsFromUrl('en', '/i18n/en.json');
await localizer.loadTranslationsFromUrl('fr', '/i18n/fr.json');
await localizer.loadTranslationsFromUrl('ar', '/i18n/ar.json');
// Set initial language based on browser preference
const browserLang = navigator.language.split('-')[0];
if (localizer.getAvailableLanguages().includes(browserLang)) {
localizer.setLanguage(browserLang);
} else {
localizer.setLanguage('en'); // fallback
}
}
const localizer = new Localizer(options);
Options:
defaultLanguage
: The default language to use (default: 'en')fallbackLanguage
: The fallback language to use when a translation is not found (default: 'en')translations
: Initial translations object (default: {})interpolationStart
: The start delimiter for interpolation (default: '${')interpolationEnd
: The end delimiter for interpolation (default: '}')rtlLanguages
: Array of RTL language codes (default: ['ar', 'he', 'fa', 'ur'])loadTranslations(language, translations)
: Load translations for a languagesetLanguage(language)
: Set the current languagegetLanguage()
: Get the current languagegetAvailableLanguages()
: Get an array of available language codestranslate(key, options)
: Translate a key with optional interpolation and pluralizationloadTranslationsFromUrl(language, url)
: Load translations from a JSON file (browser only)isRTL()
: Check if the current language is RTLisLanguageRTL(language)
: Check if a specific language is RTLtranslateDOM(container?)
: Translate all elements with i18n attributes in the document or containertranslateContainer(container)
: Translate all elements with i18n attributes in a specific containerobserveDOM(rootElement?)
: Set up a MutationObserver to automatically translate new elementsstopObservingDOM()
: Stop observing DOM changesapplyRTLToDocument()
: Apply RTL direction to the document based on the current language_t(key, options);
Parameters:
key
: The translation keyoptions
: Options for translation (interpolation values, count for pluralization, etc.)The library supports the following data attributes for DOM translation:
data-i18n
: Translates the element's text contentdata-i18n-placeholder
: Translates the element's placeholder attributedata-i18n-title
: Translates the element's title attributedata-i18n-html
: Translates the element's innerHTML (use with caution)data-i18n-params
: JSON string of parameters for interpolation (used with data-i18n)Example:
<h1 data-i18n="welcome">Welcome</h1>
<input placeholder="Enter your name" data-i18n-placeholder="enter_name">
<button title="Click me" data-i18n="button.click" data-i18n-title="button.tooltip">Click me</button>
<p data-i18n="greeting" data-i18n-params='{"name":"John"}'>Hello, John!</p>
MIT
FAQs
Simple localization and internationalization library with RTL support
The npm package @profullstack/localizer receives a total of 26 weekly downloads. As such, @profullstack/localizer popularity was classified as not popular.
We found that @profullstack/localizer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.