From 18b549ebcd70e58c6f56772cec3af9c953440879 Mon Sep 17 00:00:00 2001 From: Derek Sifford Date: Mon, 3 Jun 2019 20:56:46 -0400 Subject: [PATCH] [@wordpress/i18n] add new definitions (#35908) * [@wordpress/i18n] add new definitions * fix lint warnings --- types/wordpress__i18n/index.d.ts | 87 +++++++++++++++++++ types/wordpress__i18n/tsconfig.json | 19 ++++ types/wordpress__i18n/tslint.json | 1 + .../wordpress__i18n/wordpress__i18n-tests.ts | 4 + 4 files changed, 111 insertions(+) create mode 100644 types/wordpress__i18n/index.d.ts create mode 100644 types/wordpress__i18n/tsconfig.json create mode 100644 types/wordpress__i18n/tslint.json create mode 100644 types/wordpress__i18n/wordpress__i18n-tests.ts diff --git a/types/wordpress__i18n/index.d.ts b/types/wordpress__i18n/index.d.ts new file mode 100644 index 0000000000..e9e9057aef --- /dev/null +++ b/types/wordpress__i18n/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for @wordpress/i18n 3.4 +// Project: https://github.com/WordPress/gutenberg/tree/master/packages/i18n/README.md +// Definitions by: Derek Sifford +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Merges locale data into the Tannin instance by domain. Accepts data in a + * Jed-formatted JSON object shape. + * + * @see http://messageformat.github.io/Jed/ + * + * @param data Locale data configuration. + * @param domain Domain for which configuration applies. + */ +export function setLocaleData(data: any, domain?: string): void; + +/** + * Retrieve the translation of text. + * + * @see https://developer.wordpress.org/reference/functions/__/ + * + * @param text Text to translate. + * @param domain Domain to retrieve the translated text. + */ +export function __(text: string, domain?: string): string; + +/** + * Retrieve translated string with gettext context. + * + * @see https://developer.wordpress.org/reference/functions/_x/ + * + * @param text Text to translate. + * @param context Context information for the translators. + * @param domain Domain to retrieve the translated text. + */ +export function _x(text: string, context: string, domain?: string): string; + +/** + * Translates and retrieves the singular or plural form based on the supplied + * number. + * + * @see https://developer.wordpress.org/reference/functions/_n/ + * + * @param single The text to be used if the number is singular. + * @param plural The text to be used if the number is plural. + * @param number The number to compare against to use either the + * singular or plural form. + * @param domain Domain to retrieve the translated text. + */ +export function _n( + single: string, + plural: string, + n: number, + domain?: string +): string; + +/** + * Translates and retrieves the singular or plural form based on the supplied + * number, with gettext context. + * + * @see https://developer.wordpress.org/reference/functions/_nx/ + * + * @param single The text to be used if the number is singular. + * @param plural The text to be used if the number is plural. + * @param number The number to compare against to use either the + * singular or plural form. + * @param context Context information for the translators. + * @param domain Domain to retrieve the translated text. + */ +export function _nx( + single: string, + plural: string, + n: number, + context: string, + domain?: string +): string; + +/** + * Returns a formatted string. If an error occurs in applying the format, the + * original format string is returned. + * + * @param format The format of the string to generate. + * @param args Arguments to apply to the format. + * + * @see http://www.diveintojavascript.com/projects/javascript-sprintf + */ +export function sprintf(format: string, ...args: any[]): string; diff --git a/types/wordpress__i18n/tsconfig.json b/types/wordpress__i18n/tsconfig.json new file mode 100644 index 0000000000..48a9bd7282 --- /dev/null +++ b/types/wordpress__i18n/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@wordpress/i18n": ["wordpress__i18n"] + } + }, + "files": ["index.d.ts", "wordpress__i18n-tests.ts"] +} diff --git a/types/wordpress__i18n/tslint.json b/types/wordpress__i18n/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/wordpress__i18n/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/wordpress__i18n/wordpress__i18n-tests.ts b/types/wordpress__i18n/wordpress__i18n-tests.ts new file mode 100644 index 0000000000..9df450f694 --- /dev/null +++ b/types/wordpress__i18n/wordpress__i18n-tests.ts @@ -0,0 +1,4 @@ +import * as i18n from "@wordpress/i18n"; + +i18n.sprintf(i18n._n("%d hat", "%d hats", 4, "text-domain"), 4); // 4 hats +i18n.__("foobar", "foo-bar");