2.0.1 • Published 1 year ago

@ewb/translate-minify v2.0.1

Weekly downloads
9
License
MIT
Repository
github
Last release
1 year ago

Translate library minify

npm version CircleCI Coverage Status GZIP

Minified version of @ewb/translate
This should be used in production instead of @ewb/translate
Supports lazy loaded words and texts with 4 new functions. addWords addTexts addWordsTranslations addTextsTranslations

This library is much faster to lookup but has no add functions that changes your json files.

Install

yarn add @ewb/translate

npm install -s @ewb/translate

Quick guide

import TranslateMinify from '@ewb/translate-minify';
import words from './words.json';
import texts from './texts.json';

const translate = new TranslateMinify({
    defaultLocale: 'en',
    locale: 'no',
    words,
    texts
});

translate.word('Hello') // Hei
translate.text('Hello to you too') // Hei, til deg også

translate.setLocale('sv')
translate.w('Hello') // Hej
translate.t('Hello to you too') // Hej, till dig också

translate.w('Hello', 'no') // Hei
translate.t('Are you {{age}} years old?', { age: 10 }) // Är du 10 år gammal?
translate.t('Are you {{age}} years old?', { locale: 'no', age: 10 }) // Er du 10 år gammel?

Lazy load

Added mass loading of new words or texts with new functions addWords and addTexts
And translations with functions addWordsTranslations and addTextsTranslations

Lazy load then from disk or get them from a database. Your choice.

Interfaces

interface WordTranslations {
  [word: string]: {
    [translation: string]: string;
  };
}

interface TranslateMinifyOptions {
  defaultLocale: string;
  locale: string;
  words?: WordTranslations;
  texts?: WordTranslations;
}

interface Variables {
  [variable: string]: string | number | undefined;
}

interface TextOptions extends Variables {
  locale?: string;
}

Class definitions

export default class TranslateMinify {

  constructor(opts: TranslateMinifyOptions) : void;

  public w(word: string, locale?: string): string;
  public word(word: string, locale?: string): string;

  public t(text: string, opts?: TextOptions): string;
  public text(text: string, opts?: TextOptions): string;

  public setLocale(locale: string): void;

  public addWords(newWords: WordTranslations): void;
  public addTexts(newTexts: WordTranslations): void;

  public addWordsTranslations(translations: WordTranslations): void;
  public addTextsTranslations(translations: WordTranslations): void;
}
1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.0.0

5 years ago