diff --git a/gettext.js/gettext.js-tests.ts b/gettext.js/gettext.js-tests.ts new file mode 100644 index 0000000000..6e5b4448ae --- /dev/null +++ b/gettext.js/gettext.js-tests.ts @@ -0,0 +1,21 @@ +import * as Gettext from 'gettext.js'; + +const json: Gettext.JsonData = { + "": { + "locale": "fr", + "plural-forms": "nplurals=2; plural=n>1;" + }, + "Welcome": "Bienvenue", + "There is %1 apple": [ + "Il y a %1 pomme", + "Il y a %1 pommes" + ] +}; + +const instance: Gettext.Gettext = Gettext.i18n(); + +instance.loadJSON(json, 'messages'); +instance.setLocale('fr'); +if (instance.ngettext('There is %1 apple', 'There are %1 apples', 0) === 'Il y a %1 pomme') { + throw new Error('Failed test'); +} diff --git a/gettext.js/index.d.ts b/gettext.js/index.d.ts new file mode 100644 index 0000000000..5096490721 --- /dev/null +++ b/gettext.js/index.d.ts @@ -0,0 +1,45 @@ +// Type definitions for gettext.js 0.5 +// Project: https://github.com/guillaumepotier/gettext.js +// Definitions by: Julien Crouzet +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type PluralForm = (n: number) => number; + +export type GettextStatic = (options?: GettextOptions) => Gettext; + +export interface GettextOptions { + domain?: string; + locale?: string; + plural_func?: PluralForm; + ctxt_delimiter?: string; +} + +export interface JsonDataHeader { + locale: string; + "plural-forms": string; +} + +export interface JsonDataMessages { + [key: string]: string | string[] | JsonDataHeader; +} + +export interface JsonData extends JsonDataMessages { + "": JsonDataHeader; +} + +export interface Gettext { + setMessages: (domain: string, locale: string, messages: JsonDataMessages, plural_forms?: PluralForm) => Gettext; + loadJSON: (jsonData: JsonData, domain?: string) => Gettext; + setLocale: (locale: string) => Gettext; + getLocale: () => string; + textdomain: (domain?: string) => Gettext | string; + gettext: (msgid: string, ...args: any[]) => string; + ngettext: (msgid: string, msgid_plural: string, n: number, ...args: any[]) => string; + pgettext: (msgctxt: string, msgid: string, ...args: any[]) => string; + dcnpgettext: (domain: string, msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]) => string; + __: (msgid: string, ...args: any[]) => string; + _n: (msgid: string, msgid_plural: string, n: number, ...args: any[]) => string; + _p: (msgctxt: string, msgid: string, ...args: any[]) => string; +} + +export const i18n: GettextStatic; diff --git a/gettext.js/tsconfig.json b/gettext.js/tsconfig.json new file mode 100644 index 0000000000..d01ec061ab --- /dev/null +++ b/gettext.js/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "gettext.js-tests.ts" + ] +} diff --git a/gettext.js/tslint.json b/gettext.js/tslint.json new file mode 100644 index 0000000000..2221e40e4a --- /dev/null +++ b/gettext.js/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } \ No newline at end of file