Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n: Add types #19099

Merged
merged 3 commits into from
Dec 12, 2019
Merged
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
12 changes: 6 additions & 6 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ _Related_

_Parameters_

- _data_ `?Object`: Locale data configuration.
- _domain_ `?string`: Domain for which configuration applies.
- _data_ `[LocaleData]`: Locale data configuration.
- _domain_ `[string]`: Domain for which configuration applies.

<a name="sprintf" href="#sprintf">#</a> **sprintf**

Expand Down Expand Up @@ -73,7 +73,7 @@ _Parameters_
- _single_ `string`: The text to be used if the number is singular.
- _plural_ `string`: The text to be used if the number is plural.
- _number_ `number`: The number to compare against to use either the singular or plural form.
- _domain_ `?string`: Domain to retrieve the translated text.
- _domain_ `[string]`: Domain to retrieve the translated text.

_Returns_

Expand All @@ -94,7 +94,7 @@ _Parameters_
- _plural_ `string`: The text to be used if the number is plural.
- _number_ `number`: The number to compare against to use either the singular or plural form.
- _context_ `string`: Context information for the translators.
- _domain_ `?string`: Domain to retrieve the translated text.
- _domain_ `[string]`: Domain to retrieve the translated text.

_Returns_

Expand All @@ -112,7 +112,7 @@ _Parameters_

- _text_ `string`: Text to translate.
- _context_ `string`: Context information for the translators.
- _domain_ `?string`: Domain to retrieve the translated text.
- _domain_ `[string]`: Domain to retrieve the translated text.

_Returns_

Expand All @@ -129,7 +129,7 @@ _Related_
_Parameters_

- _text_ `string`: Text to translate.
- _domain_ `?string`: Domain to retrieve the translated text.
- _domain_ `[string]`: Domain to retrieve the translated text.

_Returns_

Expand Down
61 changes: 33 additions & 28 deletions packages/i18n/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import Tannin from 'tannin';
import memoize from 'memize';
import sprintfjs from 'sprintf-js';

/**
* @typedef {{[key: string]: any}} LocaleData
*/

/**
* Default locale data to use for Tannin domain when not otherwise provided.
* Assumes an English plural forms expression.
*
* @type {Object}
* @type {LocaleData}
*/
const DEFAULT_LOCALE_DATA = {
'': {
plural_forms: ( n ) => n === 1 ? 0 : 1,
plural_forms: ( n ) => ( n === 1 ? 0 : 1 ),
},
};

Expand All @@ -39,8 +43,8 @@ const i18n = new Tannin( {} );
*
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} data Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
* @param {LocaleData} [data] Locale data configuration.
* @param {string} [domain] Domain for which configuration applies.
*/
export function setLocaleData( data, domain = 'default' ) {
i18n.data[ domain ] = {
Expand All @@ -61,13 +65,14 @@ export function setLocaleData( data, domain = 'default' ) {
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
* otherwise previously assigned.
*
* @param {?string} domain Domain to retrieve the translated text.
* @param {?string} context Context information for the translators.
* @param {string} single Text to translate if non-plural. Used as fallback
* return value on a caught error.
* @param {?string} plural The text to be used if the number is plural.
* @param {?number} number The number to compare against to use either the
* singular or plural form.
* @param {string|undefined} domain Domain to retrieve the translated text.
* @param {string|undefined} context Context information for the translators.
* @param {string} single Text to translate if non-plural. Used as
* fallback return value on a caught error.
* @param {string} [plural] The text to be used if the number is
* plural.
* @param {number} [number] The number to compare against to use
* either the singular or plural form.
*
* @return {string} The translated string.
*/
Expand All @@ -84,8 +89,8 @@ function dcnpgettext( domain = 'default', context, single, plural, number ) {
*
* @see https://developer.wordpress.org/reference/functions/__/
*
* @param {string} text Text to translate.
* @param {?string} domain Domain to retrieve the translated text.
* @param {string} text Text to translate.
* @param {string} [domain] Domain to retrieve the translated text.
*
* @return {string} Translated text.
*/
Expand All @@ -98,9 +103,9 @@ export function __( text, domain ) {
*
* @see https://developer.wordpress.org/reference/functions/_x/
*
* @param {string} text Text to translate.
* @param {string} context Context information for the translators.
* @param {?string} domain Domain to retrieve the translated text.
* @param {string} text Text to translate.
* @param {string} context Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
*
* @return {string} Translated context string without pipe.
*/
Expand All @@ -114,11 +119,11 @@ export function _x( text, context, domain ) {
*
* @see https://developer.wordpress.org/reference/functions/_n/
*
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* singular or plural form.
* @param {?string} domain Domain to retrieve the translated text.
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* singular or plural form.
* @param {string} [domain] Domain to retrieve the translated text.
*
* @return {string} The translated singular or plural form.
*/
Expand All @@ -132,12 +137,12 @@ export function _n( single, plural, number, domain ) {
*
* @see https://developer.wordpress.org/reference/functions/_nx/
*
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* singular or plural form.
* @param {string} context Context information for the translators.
* @param {?string} domain Domain to retrieve the translated text.
* @param {string} context Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
*
* @return {string} The translated singular or plural form.
*/
Expand All @@ -149,8 +154,8 @@ export function _nx( single, plural, number, context, domain ) {
* Returns a formatted string. If an error occurs in applying the format, the
* original format string is returned.
*
* @param {string} format The format of the string to generate.
* @param {...string} args Arguments to apply to the format.
* @param {string} format The format of the string to generate.
* @param {...string} args Arguments to apply to the format.
*
* @see http://www.diveintojavascript.com/projects/javascript-sprintf
*
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

},
"include": [
"./packages/i18n/**/*.js",
"./packages/priority-queue/**/*.js",
"./packages/token-list/**/*.js",
"./packages/url/**/*.js"
],
"exclude": [
"./packages/*/benchmark",
"./packages/**/test/**",
"./packages/**/build/**",
"./packages/**/build-module/**"
Expand Down