1.0.18 • Published 3 years ago

sp-trans v1.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Lib for localisation

Simple translate manager with supporting of dynamic imports, variables and plural.

Creating

import ru from './lang_ru.json';
import en from './lang_en.json';

enum Locale {
  ru = 'ru',
  en = 'en',
}

const trans = new Trans<Locale>();
(async () => {
  await trans.init({
    translations: { ru, en },
    locale: Locale.en,
  });
  const translate = trans.createTranslate`screens`;
  console.log(translate`Home`);
})();

Creating with dynamic imports

enum Locale {
  ru = 'ru',
  en = 'en',
}

const trans = new Trans<Locale>();
(async () => {
  await trans.init({
    translations: { ru: () => import('./lang_ru.json'), en: () => import('./lang_en.json') },
    locale: Locale.en,
  });
  const translate = trans.createTranslate`screens`;
  console.log(translate`Home`);
})();

Methods

init

export type PluralFn = (count: number, locale: string) => "zero" | "one" | "two" | "few" | "many" | "other";
init(params: {
    /**
    * for example: { ru: { test: 'тест' }, en: { test: 'test' } } 
    */
    translations: Record<Locale, unknown>;
    /**
    * string
    */
    locale: Locale;
    /**
    * If you want you can to pass custom plural hanlders
    */
    pluralRecord?: Record<Locale, PluralFn>;
  }): Promise<void> 

changeLocale

changeLocale(locale: Locale): Promise<void>

createTranslate

createTranslate<T extends Variables = Variables>(module: string | TemplateStringsArray): Translate<T>

translate

export type Variables = Record<string, string>;
export type ErrorsMode = 'ignore' | 'throw' | ((error: TransError) => string);

export type TranslateOptions<T extends Variables = Variables> = {
  /**
   * ignore the error, handle error or throw error (by default)
   * */
  errorsMode?: ErrorsMode;
  /**
   * it uses for the plural if it exists
   * */
  count?: number;
  /**
   * for replaced the variable patters - {#variable}
   * */
  variables?: T;
};

export type Translate<T extends Variables = Variables> = (
  path: string | TemplateStringsArray,
  options?: TranslateOptions<T>
) => string;
1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.5

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago