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

@nanoi18n/core

v0.5.0

Published

**A minimal, yet flexible, type safe I18N framework.**

Downloads

11

Readme

NanoI18n

A minimal, yet flexible, type safe I18N framework.


license npm version npm package minimized gzipped size (select exports)

EXPERIMENTAL: NanoI18n is currently in early development and we appreciate all constructive feedback.

NanoI18n is being built with the following goals in mind:

  • Lightweight - No dependencies and minimal functionality.
  • Dev friendly - Uses types (and soon lint rules) to catch issues during development.
  • Flexible - You choose how to group your messages and when to load them.
  • Self-contained - No macros or CLI tools to run or configure.

When not to use NanoI18n?

  • Non TS code base - The value of NanoI18n comes through when using types for validation.
  • Require localization of special values done by framework - Most I18n frameworks support the ICU format which uses specially formatted messages in order to localize things like plurals, number formats, dates, etc. NanoI18n assumes that you will provide pre-formatted data to the parameterized messages.

Next

We will be adding support for:

  • Plurals - allows to choose a message based on a quantity associated with it.

Usage

Install

yarn add @nanoi18n/core

Basic Usage

  1. Create the file(s) containing the localized messages

    // messages.en.ts
    export const messages = {
      'component.hi': () => 'hi!',
      'component.hi-with-name': ({ fullName }: { fullName: string }) =>
        `hi ${a}!`,
    }
    // messages.es.ts
    export const messages = {
      'component.hi': () => '¡hola!',
      'component.hi-with-name': ({ fullName }: { fullName: string }) =>
        `¡hola = ${a}!`,
    }
  2. Export the localization messages loader

    // messages.ts
    import type { messages as enMessages } from './messages.en.js'
    import type { messages as esMessages } from './messages.es.js'
    import type { NanoI18nL10nImporters } from '@nanoi18n/core'
    import { loadMessages } from '@nanoi18n/core'
    
    // This can be exported here or defined globally if used in multiple places
    export enum Locale {
      EN = 'en',
      ES = 'es',
    }
    
    // `importers` is used to dynamically import the required locale
    
    // NOTE: Using the type as described here is important in order to get TS
    //  errors for missing keys or mismatched message types.
    const importers: NanoI18nL10nImporters<
      // Supported locales
      Locale,
      // Types of messages (ensure to include all messages)
      typeof enMessages & typeof esMessages
    > = {
      [Locale.EN]: async () => (await import('./messages.en.js')).messages,
      [Locale.ES]: async () => (await import('./messages.es.js')).messages,
    }
    
    export const getMessages = async (locales: Locale) => {
      const m = await loadMessages(locale, importers)
    
      return m
    }
  3. Localize your project

    // main.ts
    import { getMessages } from '/.messages.js'
    
    const doStuff = async () => {
      const m = await getLocalizer('es')
    
      console.log(m['hi']())
      // Output: ¡Hola!
    
      console.log(m['hi-with-name']({ name: 'Daniela' }))
      // Output: ¡Hola Daniela!
    }
    
    doStuff()