traduire
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

traduire

simple i18n translator using explicit typescript models

import en from './en.json';
import fr from './fr.json';
import { Traduire, TransDataToInject } from 'traduire';

// this is expected structure in JSON files: Record<string, string>
const sample = {
  welcome: '',
  title: '',
  label: '',
};

// infer type from sample data
type TranslationSet = typeof sample;

interface TranslationSets {
  [code: string]: TranslationSet;
}

// this line would tell you if JSON files are incorrect
const translations: TranslationSets = {
  en,
  fr,
};

const t = new Traduire<TranslationSet>('fr', fr);

// this is a flat object Record<string, string> or null;
const params: TransDataToInject = {
  username: 'Haci',
};

// this line would tell you if key 'welcome' is incorrect
const text: string = t._('welcome', params);

Readme

Keywords

Package Sidebar

Install

npm i traduire

Weekly Downloads

0

Version

1.1.0

License

LICENSE

Unpacked Size

4.59 kB

Total Files

5

Last publish

Collaborators

  • muratyaman