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

chromei18n.js

v1.0.4

Published

A Vanilla JS library for multi-language support, similar to Google Chrome's chrome.i18n API

Downloads

3

Readme

chromei18n.js

This is a vanilla JS library for multi-language support, similar to Google Chrome's chrome.i18n API. It supports all three arguments of chrome.i18n.getMessage() in vanilla JS.

How to use

  1. Similar to the chrome.i18n API, you must include the messages.json file in the following directory structure.
project-root/
│
├── _locales/
│   ├── en/
│   │   └── messages.json
│   ├── es/
│   │   └── messages.json
│   └── fr/
│       └── messages.json
...
  • Or you can hardcode the message information by the following way.
chromei18n.messages = {
  "en": {
    "prompt_for_name": {
      "message": "What's your name?",
      "description": "Ask for the user's name"
    },
    "hello": {
      "message": "Hello, $USER$",
      "description": "Greet the user",
      "placeholders": {
        "user": {
          "content": "$1",
          "example": "Cira"
        }
      }
    },
    "bye": {
      "message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!",
      "description": "Say goodbye to the user",
      "placeholders": {
        "our_site": {
          "content": "Example.com",
        },
        "user": {
          "content": "$1",
          "example": "Cira"
        }
      }
    }
  }
}
  1. You need to add the languages corresponding to the message.json files you created to your webpage using the link rel='alternate' tag.
<html lang="en"> <!-- Sets document.documentElement.lang to 'en' -->
<head>
    <title>Your Website Title</title>
    <!-- Other tags -->

    <!-- Alternate language links -->
    <link rel="alternate" hreflang="es" href="http://example.com/es/page.html">
    <link rel="alternate" hreflang="fr" href="http://example.com/fr/page.html">
    <!-- Additional links for other languages as needed -->
</head>
...
  • You don't need to add them if you've hardcoded the message information, but adding them is recommended for SEO purposes.
  1. Download src/chromei18n.js in this repository and add the script tag to your webpage or import it in your JS code.
<script type="text/javascript" src="./src/chromei18n.js"></script>
  • Or you can use this tag instead of downloading the code.
<script src="https://cdn.jsdelivr.net/gh/kangwooklee29/chromei18n.js@latest/src/chromei18n.js"></script>

For more information, please visit these links below.

Example

  • The code that fetches messages after all messages.json files are loaded
document.addEventListener("DOMContentLoaded", async () => {
    await chromei18n.loadMessages(); // necessary if you use the messages.json files, unnecessary if you hardcode the message information.
    document.querySelectorAll('[data-i18n]').forEach(el => {
        el.textContent = chromei18n.getMessage(el.getAttribute('data-i18n'));
    });
});
  • The code that fetches messages of a default language
document.addEventListener("DOMContentLoaded", async () => {
    await chromei18n.loadMessagesForLang(document.documentElement.lang); // This library sets document.documentElement.lang as navigator.language.split('-')[0] if you didn't specify it. You can change this code as needed.
    document.querySelectorAll('[data-i18n]').forEach(el => {
        el.textContent = chromei18n.getMessage(el.getAttribute('data-i18n'));
    });
});

License

MIT license