1.1.0 • Published 4 years ago

traduire v1.1.0

Weekly downloads
137
License
LICENSE
Repository
github
Last release
4 years ago

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);
1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago