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-tools/localization

v0.2.5

Published

Localization and internationalization tools

Downloads

2

Readme

@locale-tools/localization

Localization and internationalization tools.

Installation

Install a given package with npm or yarn.

npm install @locale-tools/localization

yarn add @locale-tools/localization

Usage

Types

type Locale = {
  ietf: IETF[];
  drivingSide: DrivingSide | DrivingSides;
  measurementSystem: MeasurementSystem | MeasurementSystems;
  hourClock: HourClock | HourClocks;
  timezones: Timezone[] | TZTimezones[];
  dateFormats: {
    [key in IETF]: string;
  };
  weekStartsOn: WeekStartDay | WeekStartDays;
  distanceUnit: DistanceUnit | DistanceUnits;
  temperatureUnit: TemperatureUnit | TemperatureUnit;
};

// distance unit -- both strings and enum valid
enum DistanceUnits {
  KM = "kilometer",
  MI = "mile",
}
type DistanceUnit = `${DistanceUnits}` | DistanceUnits;

// temp unit -- both strings and enum valid
enum TemperatureUnits {
  C = "celsius",
  F = "fahrenheit",
}
type TemperatureUnit = `${TemperatureUnits}` | TemperatureUnits;

// driving side -- both strings and enum valid
enum DrivingSides {
  LEFT = "left",
  RIGHT = "right",
}
type DrivingSide = `${DrivingSides}` | DrivingSides;

// measurement system -- both strings and enum valid
enum MeasurementSystems {
  METRIC = "metric",
  IMPERIAL = "imperial",
}
type MeasurementSystem = `${MeasurementSystems}` | MeasurementSystems;

// supported locales
export type SupportedLocale = {
  code: string;
  name: string;
  translationFile: JsonObject;
};

Methods

buildI18n({ supportedLanguages, namespaces, defaultNamespace, fallbackLanguage, languageStorageKey, errorCallback }): I18nInstance

Builds an instance of i18next to use in an application for translations.

| Parameters | Type | Description | Default | | ------------------ | ------------------- | -------------------------------------------------------------------------------------------- | ------------ | | supportedLanguages | SupportedLocale[] | An array of supported languages | Required | | namespaces | string[] | An array of namespace keys in your translation file | Required | | defaultNamespace | string | A default namespace key | common | | fallbackLanguage | string | A fallback IETF language code | en-US | | languageStorageKey | string | Key for storing user's chosen language (localStorage for web, AsyncStorage for React Native) | language | | errorCallback | void | Function to handle any errors | undefined |

To use, you will need to install i18next and react-i18next into your application. You'll then need to do some configuration:

i18n.ts - this file can be anywhere in your app

import { buildI18n } from "@locale-tools/localization";
import { IETF } from "@locale-tools/languages";
import enTranslation from "./en.json"; // your local translation file

const supportedLanguages = [
  {
    code: IETF.en_US,
    name: "English (US)",
    translationFile: en,
  },
];

export const i18n = buildI18n({
  supportedLanguages,
  namespaces: ["common", "validation", "a11y"],
});

Ensure that you have JSON translation files stored somewhere in your app for your i18n instance to reference. This file should be keyed with the namespaces you specify.

Then, you'll need to import your i18n instance in your app entry file.

import { i18n } from "./i18n";

.. // rest of the file

For more instructions, see the i18next docs.