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

locale-essentials

v1.1.8

Published

Basic localization based on typescript and json.

Downloads

16

Readme

LocaleEssentials

LocaleEssentials is a localization library that provides functionality for easy localization in your web application. It allows you to load language files and dynamically update the content based on the selected language.

Features

  • Loads language files in JSON format from specified directory.
  • Allows users to switch between available languages.
  • Automatically detects user's preferred language and sets it as default.
  • Uses data-localize attribute to identify elements that need to be localized.
  • Falls back to default language if selected language file is not found.

Getting Started

To use LocaleEssentials, follow these steps:

  1. Install the LocaleEssentials library in your project:
npm install locale-essentials
  1. Import the localize function into your project:
import { localize } from 'locale-essentials';
  1. Call the localize function, passing in the following options as a single object parameter:
  • langDirectory (required): The path to the directory containing the language files.
  • defaultLanguage (optional): The default language to use as a fallback. If not provided, English ('en') will be used as the default.
  • langSwitch (optional): An optional language switch element that allows users to change the language.

Example usage:

localize({
  langDirectory: 'path/to/language/files/',
  defaultLanguage: 'fr',
  langSwitch: document.getElementById('lang-switch')
});

Language Files

LocaleEssentials expects language files to be in JSON format and follow a specific structure. Each language file should contain key-value pairs where the keys represent the translation keys and the values represent the translated strings.

Example language file (en.json):

{
  "greeting": "Hello!",
  "message": "Welcome to our website.",
  "button": "Click Me"
}

HTML Markup

To enable localization in your HTML markup, use the data-localize attribute on the elements that need to be localized. The value of the attribute should correspond to the translation key in the language files.

<h1 data-localize="greeting"></h1>
<p data-localize="message"></p>
<button data-localize="button"></button>

When the localize function is called, it will update the content of these elements based on the selected language.

Language Switch (optional)

If you provide a language switch element (langSwitch) in the localize function options, LocaleEssentials will enable the functionality to switch between different languages. The language switch element should contain buttons or links with a data-lang attribute specifying the language code.

<div id="lang-switch">
  <button data-lang="en">English</button>
  <button data-lang="fr">Français</button>
</div>

When a language button is clicked, the localize function will be called with the selected language, and the content will be updated accordingly.

Licence

This library is licensed under the MIT License.