diff --git a/types/i18n-js/i18n-js-tests.ts b/types/i18n-js/i18n-js-tests.ts new file mode 100644 index 0000000000..4f19dc4c02 --- /dev/null +++ b/types/i18n-js/i18n-js-tests.ts @@ -0,0 +1,81 @@ +/** + * Test suite created by Yuya Tanaka + * Created by using code examples from https://github.com/fnando/i18n-js + */ + +import I18n = require("i18n-js"); + +I18n.defaultLocale = "pt-BR"; +I18n.locale = "pt-BR"; +I18n.currentLocale() === "pt-BR"; + +I18n.t("some.scoped.translation"); +I18n.t("some.scoped.translation", { locale: "fr" }); +I18n.t("hello", { name: "John Doe" }); +I18n.t("some.missing.scope", { defaultValue: "A default message" }); +I18n.t("noun", { defaultValue: "I'm a {{noun}}", noun: "Mac" }); +I18n.t("some.missing.scope", { defaults: [{ scope: "some.existing.scope" }] }); +I18n.t("some.missing.scope", { defaults: [{ message: "Some message" }] }); + +I18n.fallbacks = true; +I18n.locales.no = ["nb", "en"]; +I18n.locales.no = "nb"; +I18n.locales.no = locale => ["nb"]; + +I18n.missingBehaviour = "guess"; +I18n.missingTranslationPrefix = "EE: "; +I18n.missingTranslation = (scope, options) => "foobar"; +I18n.missingTranslation = (scope, options) => null; +I18n.missingTranslation = (scope, options) => undefined; + +I18n.t("inbox.counting", { count: 10 }); +I18n.pluralization["ru"] = count => { + const key = count % 10 === 1 && count % 100 !== 11 ? "one" + : [2, 3, 4].indexOf(count % 10) >= 0 && [12, 13, 14].indexOf(count % 100) < 0 ? "few" + : count % 10 === 0 || [5, 6, 7, 8, 9].indexOf(count % 10) >= 0 || [11, 12, 13, 14].indexOf(count % 100) >= 0 ? "many" + : "other"; + return [key]; +}; + +const options = { scope: "activerecord.attributes.user" }; +I18n.t("name", options); +I18n.t(["greetings", "hello"]); + +I18n.l("currency", 1990.99); +I18n.l("number", 1990.99); +I18n.l("percentage", 123.45); + +I18n.toNumber(1000); +I18n.toCurrency(1000); +I18n.toPercentage(100); + +I18n.toNumber(1000, { precision: 0 }); +I18n.toNumber(1000, { delimiter: ".", separator: "," }); +I18n.toNumber(1000, { delimiter: ".", precision: 0 }); + +I18n.toCurrency(1000, { precision: 0 }); + +I18n.toHumanSize(1234); + +I18n.l("date.formats.short", "2009-09-18"); +I18n.l("time.formats.short", "2009-09-18 23:12:43"); +I18n.l("time.formats.short", "2009-11-09T18:10:34"); +I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); +I18n.l("date.formats.short", 1251862029000); +I18n.l("date.formats.short", "09/18/2009"); +I18n.l("date.formats.short", (new Date())); + +I18n.l("date.formats.ordinal_day", "2009-09-18", { day: "18th" }); + +I18n.strftime(new Date(), "%d/%m/%Y"); + +const point_in_number = 1000; +I18n.t("point", { count: point_in_number, formatted_number: I18n.toNumber(point_in_number) }); + +I18n.translations = {}; +I18n.translations["en"] = { + message: "Some special message for you" +}; +I18n.translations["pt-BR"] = { + message: "Uma mensagem especial para vocĂȘ" +}; diff --git a/types/i18n-js/index.d.ts b/types/i18n-js/index.d.ts new file mode 100644 index 0000000000..4800125087 --- /dev/null +++ b/types/i18n-js/index.d.ts @@ -0,0 +1,79 @@ +// Type definitions for i18n-js 3.0 +// Project: https://github.com/fnando/i18n-js +// Definitions by: Yuya Tanaka +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +// tslint:disable-next-line:export-just-namespace +export = I18n; +export as namespace I18n; + +declare namespace I18n { + type Scope = string | string[]; + + let defaultLocale: string; + let locale: string; + let defaultSeparator: string; + let placeholder: RegExp; + let fallbacks: boolean; + let missingBehaviour: "message" | "guess"; + let missingTranslationPrefix: string; + + // tslint:disable-next-line prefer-declare-function + let missingTranslation: (scope: string, options?: TranslateOptions) => string | null | undefined; + // tslint:disable-next-line prefer-declare-function + let missingPlaceholder: (placeholder: string, message: string, options?: InterpolateOptions) => string | null | undefined; + // tslint:disable-next-line prefer-declare-function + let nullPlaceholder: (placeholder: string, message: string, options?: InterpolateOptions) => string | null | undefined; + + let translations: { [locale: string]: object }; + let locales: { [key: string]: string | string[] | ((locale: string) => string | string[]) }; + let pluralization: { [locale: string]: (count: number) => string[] }; + + function reset(): void; + + function currentLocale(): string; + + interface InterpolateOptions { + [key: string]: any; // interpolation + } + + interface TranslateOptions extends InterpolateOptions { + scope?: Scope; + message?: string; + defaults?: Array<{ message: string } | { scope: Scope }>; + defaultValue?: string; + } + function translate(scope: Scope, options?: TranslateOptions): string; + function t(scope: Scope, options?: TranslateOptions): string; + + function localize(scope: "currency" | "number" | "percentage", value: number, options?: InterpolateOptions): string; + function localize(scope: Scope, value: string | number | Date, options?: InterpolateOptions): string; + function l(scope: "currency" | "number" | "percentage", value: number, options?: InterpolateOptions): string; + function l(scope: Scope, value: string | number | Date, options?: InterpolateOptions): string; + + interface ToNumberOptions { + precision?: number; + separator?: string; + delimiter?: string; + strip_insignificant_zeros?: boolean; + } + function toNumber(num: number, options?: ToNumberOptions): string; + + type ToPercentageOptions = ToNumberOptions; + function toPercentage(num: number, options?: ToPercentageOptions): string; + + interface ToCurrencyOptions extends ToNumberOptions { + format?: string; + unit?: string; + sign_first?: boolean; + } + function toCurrency(num: number, options?: ToCurrencyOptions): string; + + interface ToHumanSizeOptions extends ToNumberOptions { + format?: string; + } + function toHumanSize(num: number, options?: ToHumanSizeOptions): string; + + function strftime(date: Date, format: string): string; +} diff --git a/types/i18n-js/tsconfig.json b/types/i18n-js/tsconfig.json new file mode 100644 index 0000000000..4a9e4d110b --- /dev/null +++ b/types/i18n-js/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "i18n-js-tests.ts" + ] +} diff --git a/types/i18n-js/tslint.json b/types/i18n-js/tslint.json new file mode 100644 index 0000000000..299e93b32f --- /dev/null +++ b/types/i18n-js/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "indent": [true, "spaces", 4], + "quotemark": [true, "double", "avoid-escape"] + } +}