Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
pluralizers on top of [make-plural](https://github.com/eemeli/make-plural/).
- [Changed] Use `make-plural`'s `en` function as the default pluralizer.
- [Changed] Mark `Scope` type as read-only.
- [Changed] Add Generic type Instead of `Scope`.

## V4.2.2 - Dec 16, 2022

Expand Down
30 changes: 15 additions & 15 deletions typings/I18n.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { DateTime, Dict, FormatNumberOptions, I18nOptions, MissingPlaceholderHandler, NullPlaceholderHandler, NumberToCurrencyOptions, NumberToDelimitedOptions, NumberToHumanOptions, NumberToHumanSizeOptions, NumberToPercentageOptions, NumberToRoundedOptions, Numeric, OnChangeHandler, Scope, StrftimeOptions, TimeAgoInWordsOptions, ToSentenceOptions, TranslateOptions } from "./typing";
import { DateTime, Dict, FormatNumberOptions, I18nOptions, MissingPlaceholderHandler, NestedKeyOf, NullPlaceholderHandler, NumberToCurrencyOptions, NumberToDelimitedOptions, NumberToHumanOptions, NumberToHumanSizeOptions, NumberToPercentageOptions, NumberToRoundedOptions, Numeric, OnChangeHandler, Scope, StrftimeOptions, TimeAgoInWordsOptions, ToSentenceOptions, TranslateOptions } from "./typing";
import { Locales } from "./Locales";
import { Pluralization } from "./Pluralization";
import { MissingTranslation } from "./MissingTranslation";
import { interpolate } from "./helpers";
export declare class I18n {
export declare class I18n<K extends object> {
private _locale;
private _defaultLocale;
private _version;
onChangeHandlers: OnChangeHandler[];
onChangeHandlers: OnChangeHandler<K>[];
defaultSeparator: string;
enableFallback: boolean;
locales: Locales;
pluralization: Pluralization;
missingBehavior: string;
missingPlaceholder: MissingPlaceholderHandler;
missingPlaceholder: MissingPlaceholderHandler<K>;
missingTranslationPrefix: string;
nullPlaceholder: NullPlaceholderHandler;
nullPlaceholder: NullPlaceholderHandler<K>;
missingTranslation: MissingTranslation;
placeholder: RegExp;
translations: Dict;
translations: Dict<K>;
transformKey: (key: string) => string;
interpolate: typeof interpolate;
constructor(translations?: Dict, options?: Partial<I18nOptions>);
store(translations: Dict): void;
constructor(translations?: Dict<K>, options?: Partial<I18nOptions<K>>);
store(translations: Dict<K>): void;
get locale(): string;
set locale(newLocale: string);
get defaultLocale(): string;
set defaultLocale(newLocale: string);
translate<T = string>(scope: Scope, options?: TranslateOptions): string | T;
t: <T = string>(scope: Scope, options?: TranslateOptions) => string | T;
pluralize(count: number, scope: Scope, options?: TranslateOptions): string;
p: (count: number, scope: Scope, options?: TranslateOptions) => string;
localize(type: string, value: string | number | Date | null | undefined, options?: Dict): string;
l: (type: string, value: string | number | Date | null | undefined, options?: Dict) => string;
translate<T = string>(scope: Scope, options?: TranslateOptions<K>): string | T;
t: <T = string>(scope: NestedKeyOf<K>, options?: TranslateOptions<K>) => string | T;
pluralize(count: number, scope: Scope, options?: TranslateOptions<K>): string;
p: (count: number, scope: Scope, options?: TranslateOptions<K>) => string;
localize(type: string, value: string | number | Date | null | undefined, options?: Dict<K>): string;
l: (type: string, value: string | number | Date | null | undefined, options?: Dict<K>) => string;
toTime(scope: Scope, input: DateTime): string;
numberToCurrency(input: Numeric, options?: Partial<NumberToCurrencyOptions>): string;
numberToPercentage(input: Numeric, options?: Partial<NumberToPercentageOptions>): string;
Expand All @@ -48,7 +48,7 @@ export declare class I18n {
toSentence(items: any[], options?: Partial<ToSentenceOptions>): string;
timeAgoInWords(fromTime: DateTime, toTime: DateTime, options?: TimeAgoInWordsOptions): string;
distanceOfTimeInWords: (fromTime: DateTime, toTime: DateTime, options?: TimeAgoInWordsOptions) => string;
onChange(callback: OnChangeHandler): () => void;
onChange(callback: OnChangeHandler<K>): () => void;
get version(): number;
formatNumber(input: Numeric, options: FormatNumberOptions): string;
get(scope: Scope): any;
Expand Down
33 changes: 20 additions & 13 deletions typings/typing.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from "bignumber.js";
import { I18n } from "./I18n";
export interface Dict {
[key: string]: any;
export interface Dict<T> {
[key: string]: T;
}
export type DateTime = string | number | Date;
export interface TimeAgoInWordsOptions {
Expand Down Expand Up @@ -51,32 +51,32 @@ export interface ObjectType {
[key: string]: PrimitiveType | ArrayType | ObjectType;
}
type MissingBehavior = "message" | "guess" | "error";
export interface I18nOptions {
export interface I18nOptions<T extends object> {
defaultLocale: string;
defaultSeparator: string;
enableFallback: boolean;
locale: string;
missingBehavior: MissingBehavior;
missingPlaceholder: MissingPlaceholderHandler;
nullPlaceholder: NullPlaceholderHandler;
missingPlaceholder: MissingPlaceholderHandler<T>;
nullPlaceholder: NullPlaceholderHandler<T>;
missingTranslationPrefix: string;
placeholder: RegExp;
transformKey: (key: string) => string;
}
export type Scope = string | string[];
export type LocaleResolver = (i18n: I18n, locale: string) => string[];
export type Pluralizer = (i18n: I18n, count: number) => string[];
export type MissingTranslationStrategy = (i18n: I18n, scope: Scope, options: Dict) => string;
export interface TranslateOptions {
export type LocaleResolver<T extends object> = (i18n: I18n<T>, locale: string) => string[];
export type Pluralizer<T extends object> = (i18n: I18n<T>, count: number) => string[];
export type MissingTranslationStrategy<T extends object> = (i18n: I18n<T>, scope: Scope, options: Dict<T>) => string;
export interface TranslateOptions<T extends object> {
defaultValue?: any;
count?: number;
scope?: Scope;
defaults?: Dict[];
defaults?: Dict<T>[];
missingBehavior?: MissingBehavior | string;
[key: string]: any;
}
export type MissingPlaceholderHandler = (i18n: I18n, placeholder: string, message: string, options: Dict) => string;
export type NullPlaceholderHandler = (i18n: I18n, placeholder: string, message: string, options: Dict) => string;
export type MissingPlaceholderHandler<T extends object> = (i18n: I18n<T>, placeholder: string, message: string, options: Dict<T>) => string;
export type NullPlaceholderHandler<T extends object> = (i18n: I18n<T>, placeholder: string, message: string, options: Dict<T>) => string;
export type DayNames = [string, string, string, string, string, string, string];
export type MonthNames = [
null,
Expand All @@ -103,5 +103,12 @@ export interface StrftimeOptions {
monthNames: MonthNames;
abbrMonthNames: MonthNames;
}
export type OnChangeHandler = (i18n: I18n) => void;
export type OnChangeHandler<T extends object> = (i18n: I18n<T>) => void;

export type NestedKeyOf<ObjectType extends object> =
{[Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
: `${Key}`
}[keyof ObjectType & (string | number)];

export {};