Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @kogai/typed_i18n with all npm packages installed. Try it out:

var typedI18n = require("@kogai/typed_i18n")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@kogai/typed_i18n v0.6.0

Type safe i18n with Flow and TypeScript

typed_i18n

npm version CircleCI

Generate strictly typed definition of TFunction from own i18next dictionary file.

It generate from

{
  "en": {
    "translation": {
      "foo": {
        "bar": "some text",
        "buzz": 999
      }
    }
  }
}

as

// @flow
declare function t(_: "foo"): {
  +bar: string,
  +buzz: number
};
declare function t(_: "foo.bar"): string;
declare function t(_: "foo.buzz"): number;

export type TFunction = typeof t;

or

declare namespace typed_i18n {
  interface TFunction {
    t(_: "foo"): {
      +bar: string,
      +buzz: number,
    };
    t(_: "foo.bar"): string;
    t(_: "foo.buzz"): number;
  }
}
export = typed_i18n;

then if you use TFunction like below, type-checker warn you function call by invalid path

// @flow

import type { TFunction } from "./locale.translation"; // Definition file generated

declare var t: TFunction;

// Those are ok
const x: { bar: string, buzz: number } = t("foo");
const x1: string = x.bar;
const x2: number = x.buzz;
// Expect error
const x3 = x.buzzz;

// Expect error
const y = t("fooo");

// Those are also strictly typed too
const z1: string = t("foo.bar");
const z2: number = t("foo.buzz");

Usage

# Basic usage
$ typed_i18n -i path/to/your.json -o path/to/out/dir

# Support also typescript
$ typed_i18n -i path/to/your.json -o path/to/out/dir -l typescript

# You can specify namespaces instead of default "translation"
$ typed_i18n -i path/to/your.json -o path/to/out/dir -n translate -n my-namespace -n other-namespace
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free