# @locale-tools/localization

> Localization and internationalization tools

Latest version **0.2.5** (published 2021-11-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @locale-tools/localization
pnpm add @locale-tools/localization
yarn add @locale-tools/localization
bun add @locale-tools/localization
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2021-11-30 |
| First published | 2021-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 237.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sara Pope |
| Maintainers | gretzky |
| Keywords | i18n, i18next, locale, localization |

## Links

- npm: https://www.npmjs.com/package/@locale-tools/localization
- Repository: https://github.com/gretzky/locale-tools
- Homepage: https://github.com/gretzky/locale-tools#readme
- Issues: https://github.com/gretzky/locale-tools/issues
- npm.io page: https://npm.io/package/@locale-tools/localization

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2021-11-30
- 0.2.4 — 2021-11-30
- 0.2.0 — 2021-11-26

## README

# @locale-tools/localization

Localization and internationalization tools.

## Installation

Install a given package with npm or yarn.

```bash
npm install @locale-tools/localization

yarn add @locale-tools/localization
```

## Usage

### Types

```ts
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](https://www.i18next.com) 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

```ts
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.

```ts
import { i18n } from "./i18n";

.. // rest of the file
```

For more instructions, see the [i18next docs](https://www.i18next.com).

---
_Source: https://npm.io/package/@locale-tools/localization · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
