Socket
Socket
Sign inDemoInstall

@wordpress/i18n

Package Overview
Dependencies
16
Maintainers
16
Versions
146
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.18.0 to 3.19.1-next.53e2173694.0

src/test/subscribe-i18n.js

159

build-module/create-i18n.js

@@ -30,6 +30,20 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

};
/* eslint-disable jsdoc/valid-types */
/*
* Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,
* `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.
*/
var I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
/**
* @typedef {(domain?: string) => LocaleData} GetLocaleData
*
* Returns locale data by domain in a
* Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*/
/**
* @typedef {(data?: LocaleData, domain?: string) => void} SetLocaleData
*
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -41,3 +55,13 @@ * Jed-formatted JSON object shape.

/** @typedef {() => void} SubscribeCallback */
/** @typedef {() => void} UnsubscribeCallback */
/**
* @typedef {(callback: SubscribeCallback) => UnsubscribeCallback} Subscribe
*
* Subscribes to changes of locale data
*/
/**
* @typedef {(domain?: string) => string} GetFilterDomain

@@ -93,6 +117,8 @@ * Retrieve the domain to use when calling domain-specific filters.

/**
* @typedef {{ applyFilters: (hookName:string, ...args: unknown[]) => unknown}} ApplyFiltersInterface
* @typedef {(single: string, context?: string, domain?: string) => boolean} HasTranslation
*
* Check if there is a translation for a given string in singular form.
*/
/* eslint-enable jsdoc/valid-types */
/** @typedef {import('@wordpress/hooks').Hooks} Hooks */

@@ -103,4 +129,6 @@ /**

* @typedef I18n
* @property {GetLocaleData} getLocaleData Returns locale data by domain in a Jed-formatted JSON object shape.
* @property {SetLocaleData} setLocaleData Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
* @property {Subscribe} subscribe Subscribes to changes of Tannin locale data.
* @property {__} __ Retrieve the translation of text.

@@ -113,2 +141,3 @@ * @property {_x} _x Retrieve translated string with gettext context.

* @property {IsRtl} isRTL Check if current locale is RTL.
* @property {HasTranslation} hasTranslation Check if there is a translation for a given string.
*/

@@ -121,3 +150,3 @@

* @param {string} [initialDomain] Domain for which configuration applies.
* @param {ApplyFiltersInterface} [hooks] Hooks implementation.
* @param {Hooks} [hooks] Hooks implementation.
* @return {I18n} I18n instance

@@ -133,5 +162,37 @@ */

var tannin = new Tannin({});
/** @type {SetLocaleData} */
var listeners = new Set();
var setLocaleData = function setLocaleData(data) {
var notifyListeners = function notifyListeners() {
listeners.forEach(function (listener) {
return listener();
});
};
/**
* Subscribe to changes of locale data.
*
* @param {SubscribeCallback} callback Subscription callback.
* @return {UnsubscribeCallback} Unsubscribe callback.
*/
var subscribe = function subscribe(callback) {
listeners.add(callback);
return function () {
return listeners.delete(callback);
};
};
/** @type {GetLocaleData} */
var getLocaleData = function getLocaleData() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
return tannin.data[domain];
};
/**
* @param {LocaleData} [data]
* @param {string} [domain]
*/
var doSetLocaleData = function doSetLocaleData(data) {
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';

@@ -143,2 +204,9 @@ tannin.data[domain] = _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_LOCALE_DATA), tannin.data[domain]), data); // Populate default domain configuration (supported locale date which omits

};
/** @type {SetLocaleData} */
var setLocaleData = function setLocaleData(data, domain) {
doSetLocaleData(data, domain);
notifyListeners();
};
/**

@@ -169,3 +237,4 @@ * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not

if (!tannin.data[domain]) {
setLocaleData(undefined, domain);
// use `doSetLocaleData` to set silently, without notifying listeners
doSetLocaleData(undefined, domain);
}

@@ -178,7 +247,4 @@

var getFilterDomain = function getFilterDomain(domain) {
if (typeof domain === 'undefined') {
return 'default';
}
var getFilterDomain = function getFilterDomain() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
return domain;

@@ -191,2 +257,6 @@ };

var translation = dcnpgettext(domain, undefined, text);
if (!hooks) {
return translation;
}
/**

@@ -200,5 +270,2 @@ * Filters text with its translation.

if (typeof hooks === 'undefined') {
return translation;
}

@@ -222,2 +289,6 @@ translation =

var translation = dcnpgettext(domain, context, text);
if (!hooks) {
return translation;
}
/**

@@ -232,5 +303,2 @@ * Filters text with its translation based on context information.

if (typeof hooks === 'undefined') {
return translation;
}

@@ -255,3 +323,3 @@ translation =

if (typeof hooks === 'undefined') {
if (!hooks) {
return translation;

@@ -288,3 +356,3 @@ }

if (typeof hooks === 'undefined') {
if (!hooks) {
return translation;

@@ -322,3 +390,35 @@ }

};
/** @type {HasTranslation} */
var hasTranslation = function hasTranslation(single, context, domain) {
var _tannin$data, _tannin$data2;
var key = context ? context + "\x04" + single : single;
var result = !!((_tannin$data = tannin.data) !== null && _tannin$data !== void 0 && (_tannin$data2 = _tannin$data[domain !== null && domain !== void 0 ? domain : 'default']) !== null && _tannin$data2 !== void 0 && _tannin$data2[key]);
if (hooks) {
/**
* Filters the presence of a translation in the locale data.
*
* @param {boolean} hasTranslation Whether the translation is present or not..
* @param {string} single The singular form of the translated text (used as key in locale data)
* @param {string} context Context information for the translators.
* @param {string} domain Text domain. Unique identifier for retrieving translated strings.
*/
result =
/** @type { boolean } */
/** @type {*} */
hooks.applyFilters('i18n.has_translation', result, single, context, domain);
result =
/** @type { boolean } */
/** @type {*} */
hooks.applyFilters('i18n.has_translation_' + getFilterDomain(domain), result, single, context, domain);
}
return result;
};
if (initialData) {

@@ -328,4 +428,20 @@ setLocaleData(initialData, initialDomain);

if (hooks) {
/**
* @param {string} hookName
*/
var onHookAddedOrRemoved = function onHookAddedOrRemoved(hookName) {
if (I18N_HOOK_REGEXP.test(hookName)) {
notifyListeners();
}
};
hooks.addAction('hookAdded', 'core/i18n', onHookAddedOrRemoved);
hooks.addAction('hookRemoved', 'core/i18n', onHookAddedOrRemoved);
}
return {
getLocaleData: getLocaleData,
setLocaleData: setLocaleData,
subscribe: subscribe,
__: __,

@@ -335,5 +451,6 @@ _x: _x,

_nx: _nx,
isRTL: isRTL
isRTL: isRTL,
hasTranslation: hasTranslation
};
};
//# sourceMappingURL=create-i18n.js.map
/**
* Internal dependencies
*/
import { createI18n } from './create-i18n';
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
import { defaultHooks } from '@wordpress/hooks';
var i18n = createI18n(undefined, undefined, defaultHooks);
/**
* Internal dependencies
* Default, singleton instance of `I18n`.
*/
import { createI18n } from './create-i18n';
var i18n = createI18n(undefined, undefined, {
applyFilters: applyFilters
});
export default i18n;
/*

@@ -20,5 +23,17 @@ * Comments in this file are duplicated from ./i18n due to

* @typedef {import('./create-i18n').LocaleData} LocaleData
* @typedef {import('./create-i18n').SubscribeCallback} SubscribeCallback
* @typedef {import('./create-i18n').UnsubscribeCallback} UnsubscribeCallback
*/
/**
* Returns locale data by domain in a Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*
* @param {string} [domain] Domain for which to get the data.
* @return {LocaleData} Locale data.
*/
export var getLocaleData = i18n.getLocaleData.bind(i18n);
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -35,2 +50,10 @@ * Jed-formatted JSON object shape.

/**
* Subscribes to changes of locale data
*
* @param {SubscribeCallback} callback Subscription callback
* @return {UnsubscribeCallback} Unsubscribe callback
*/
export var subscribe = i18n.subscribe.bind(i18n);
/**
* Retrieve the translation of text.

@@ -105,2 +128,12 @@ *

export var isRTL = i18n.isRTL.bind(i18n);
/**
* Check if there is a translation for a given string (in singular form).
*
* @param {string} single Singular form of the string to look up.
* @param {string} [context] Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
* @return {boolean} Whether the translation exists or not.
*/
export var hasTranslation = i18n.hasTranslation.bind(i18n);
//# sourceMappingURL=default-i18n.js.map

2

build-module/index.js
export { sprintf } from './sprintf';
export * from './create-i18n';
export { setLocaleData, __, _x, _n, _nx, isRTL } from './default-i18n';
export { default as defaultI18n, setLocaleData, getLocaleData, subscribe, __, _x, _n, _nx, isRTL, hasTranslation } from './default-i18n';
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

export function createI18n(initialData?: Record<string, any> | undefined, initialDomain?: string | undefined, hooks?: ApplyFiltersInterface | undefined): I18n;
export function createI18n(initialData?: Record<string, any> | undefined, initialDomain?: string | undefined, hooks?: import("@wordpress/hooks/build-types/createHooks")._Hooks | undefined): I18n;
export type LocaleData = {

@@ -6,2 +6,7 @@ [x: string]: any;

/**
* Returns locale data by domain in a
* Jed-formatted JSON object shape.
*/
export type GetLocaleData = (domain?: string | undefined) => LocaleData;
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -11,3 +16,9 @@ * Jed-formatted JSON object shape.

export type SetLocaleData = (data?: Record<string, any> | undefined, domain?: string | undefined) => void;
export type SubscribeCallback = () => void;
export type UnsubscribeCallback = () => void;
/**
* Subscribes to changes of locale data
*/
export type Subscribe = (callback: SubscribeCallback) => UnsubscribeCallback;
/**
* Retrieve the domain to use when calling domain-specific filters.

@@ -43,6 +54,8 @@ */

export type IsRtl = () => boolean;
export type ApplyFiltersInterface = {
applyFilters: (hookName: string, ...args: unknown[]) => unknown;
};
/**
* Check if there is a translation for a given string in singular form.
*/
export type HasTranslation = (single: string, context?: string | undefined, domain?: string | undefined) => boolean;
export type Hooks = import("@wordpress/hooks/build-types/createHooks")._Hooks;
/**
* An i18n instance

@@ -52,2 +65,6 @@ */

/**
* Returns locale data by domain in a Jed-formatted JSON object shape.
*/
getLocaleData: GetLocaleData;
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -58,2 +75,6 @@ * Jed-formatted JSON object shape.

/**
* Subscribes to changes of Tannin locale data.
*/
subscribe: Subscribe;
/**
* Retrieve the translation of text.

@@ -80,3 +101,7 @@ */

isRTL: IsRtl;
/**
* Check if there is a translation for a given string.
*/
hasTranslation: HasTranslation;
};
//# sourceMappingURL=create-i18n.d.ts.map

@@ -0,5 +1,17 @@

export default i18n;
/**
* @typedef {import('./create-i18n').LocaleData} LocaleData
* @typedef {import('./create-i18n').SubscribeCallback} SubscribeCallback
* @typedef {import('./create-i18n').UnsubscribeCallback} UnsubscribeCallback
*/
/**
* Returns locale data by domain in a Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*
* @param {string} [domain] Domain for which to get the data.
* @return {LocaleData} Locale data.
*/
export const getLocaleData: import("./create-i18n").GetLocaleData;
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -15,2 +27,9 @@ * Jed-formatted JSON object shape.

/**
* Subscribes to changes of locale data
*
* @param {SubscribeCallback} callback Subscription callback
* @return {UnsubscribeCallback} Unsubscribe callback
*/
export const subscribe: import("./create-i18n").Subscribe;
/**
* Retrieve the translation of text.

@@ -80,5 +99,17 @@ *

export const isRTL: import("./create-i18n").IsRtl;
/**
* Check if there is a translation for a given string (in singular form).
*
* @param {string} single Singular form of the string to look up.
* @param {string} [context] Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
* @return {boolean} Whether the translation exists or not.
*/
export const hasTranslation: import("./create-i18n").HasTranslation;
export type LocaleData = {
[x: string]: any;
};
export type SubscribeCallback = () => void;
export type UnsubscribeCallback = () => void;
declare const i18n: import("./create-i18n").I18n;
//# sourceMappingURL=default-i18n.d.ts.map
export { sprintf } from "./sprintf";
export * from "./create-i18n";
export { setLocaleData, __, _x, _n, _nx, isRTL } from "./default-i18n";
export { default as defaultI18n, setLocaleData, getLocaleData, subscribe, __, _x, _n, _nx, isRTL, hasTranslation } from "./default-i18n";
//# sourceMappingURL=index.d.ts.map

@@ -36,6 +36,20 @@ "use strict";

};
/* eslint-disable jsdoc/valid-types */
/*
* Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,
* `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.
*/
var I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
/**
* @typedef {(domain?: string) => LocaleData} GetLocaleData
*
* Returns locale data by domain in a
* Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*/
/**
* @typedef {(data?: LocaleData, domain?: string) => void} SetLocaleData
*
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -47,3 +61,13 @@ * Jed-formatted JSON object shape.

/** @typedef {() => void} SubscribeCallback */
/** @typedef {() => void} UnsubscribeCallback */
/**
* @typedef {(callback: SubscribeCallback) => UnsubscribeCallback} Subscribe
*
* Subscribes to changes of locale data
*/
/**
* @typedef {(domain?: string) => string} GetFilterDomain

@@ -99,6 +123,8 @@ * Retrieve the domain to use when calling domain-specific filters.

/**
* @typedef {{ applyFilters: (hookName:string, ...args: unknown[]) => unknown}} ApplyFiltersInterface
* @typedef {(single: string, context?: string, domain?: string) => boolean} HasTranslation
*
* Check if there is a translation for a given string in singular form.
*/
/* eslint-enable jsdoc/valid-types */
/** @typedef {import('@wordpress/hooks').Hooks} Hooks */

@@ -109,4 +135,6 @@ /**

* @typedef I18n
* @property {GetLocaleData} getLocaleData Returns locale data by domain in a Jed-formatted JSON object shape.
* @property {SetLocaleData} setLocaleData Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
* @property {Subscribe} subscribe Subscribes to changes of Tannin locale data.
* @property {__} __ Retrieve the translation of text.

@@ -119,2 +147,3 @@ * @property {_x} _x Retrieve translated string with gettext context.

* @property {IsRtl} isRTL Check if current locale is RTL.
* @property {HasTranslation} hasTranslation Check if there is a translation for a given string.
*/

@@ -127,3 +156,3 @@

* @param {string} [initialDomain] Domain for which configuration applies.
* @param {ApplyFiltersInterface} [hooks] Hooks implementation.
* @param {Hooks} [hooks] Hooks implementation.
* @return {I18n} I18n instance

@@ -139,5 +168,37 @@ */

var tannin = new _tannin.default({});
/** @type {SetLocaleData} */
var listeners = new Set();
var setLocaleData = function setLocaleData(data) {
var notifyListeners = function notifyListeners() {
listeners.forEach(function (listener) {
return listener();
});
};
/**
* Subscribe to changes of locale data.
*
* @param {SubscribeCallback} callback Subscription callback.
* @return {UnsubscribeCallback} Unsubscribe callback.
*/
var subscribe = function subscribe(callback) {
listeners.add(callback);
return function () {
return listeners.delete(callback);
};
};
/** @type {GetLocaleData} */
var getLocaleData = function getLocaleData() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
return tannin.data[domain];
};
/**
* @param {LocaleData} [data]
* @param {string} [domain]
*/
var doSetLocaleData = function doSetLocaleData(data) {
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';

@@ -149,2 +210,9 @@ tannin.data[domain] = _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_LOCALE_DATA), tannin.data[domain]), data); // Populate default domain configuration (supported locale date which omits

};
/** @type {SetLocaleData} */
var setLocaleData = function setLocaleData(data, domain) {
doSetLocaleData(data, domain);
notifyListeners();
};
/**

@@ -175,3 +243,4 @@ * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not

if (!tannin.data[domain]) {
setLocaleData(undefined, domain);
// use `doSetLocaleData` to set silently, without notifying listeners
doSetLocaleData(undefined, domain);
}

@@ -184,7 +253,4 @@

var getFilterDomain = function getFilterDomain(domain) {
if (typeof domain === 'undefined') {
return 'default';
}
var getFilterDomain = function getFilterDomain() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
return domain;

@@ -197,2 +263,6 @@ };

var translation = dcnpgettext(domain, undefined, text);
if (!hooks) {
return translation;
}
/**

@@ -206,5 +276,2 @@ * Filters text with its translation.

if (typeof hooks === 'undefined') {
return translation;
}

@@ -228,2 +295,6 @@ translation =

var translation = dcnpgettext(domain, context, text);
if (!hooks) {
return translation;
}
/**

@@ -238,5 +309,2 @@ * Filters text with its translation based on context information.

if (typeof hooks === 'undefined') {
return translation;
}

@@ -261,3 +329,3 @@ translation =

if (typeof hooks === 'undefined') {
if (!hooks) {
return translation;

@@ -294,3 +362,3 @@ }

if (typeof hooks === 'undefined') {
if (!hooks) {
return translation;

@@ -328,3 +396,35 @@ }

};
/** @type {HasTranslation} */
var hasTranslation = function hasTranslation(single, context, domain) {
var _tannin$data, _tannin$data2;
var key = context ? context + "\x04" + single : single;
var result = !!((_tannin$data = tannin.data) !== null && _tannin$data !== void 0 && (_tannin$data2 = _tannin$data[domain !== null && domain !== void 0 ? domain : 'default']) !== null && _tannin$data2 !== void 0 && _tannin$data2[key]);
if (hooks) {
/**
* Filters the presence of a translation in the locale data.
*
* @param {boolean} hasTranslation Whether the translation is present or not..
* @param {string} single The singular form of the translated text (used as key in locale data)
* @param {string} context Context information for the translators.
* @param {string} domain Text domain. Unique identifier for retrieving translated strings.
*/
result =
/** @type { boolean } */
/** @type {*} */
hooks.applyFilters('i18n.has_translation', result, single, context, domain);
result =
/** @type { boolean } */
/** @type {*} */
hooks.applyFilters('i18n.has_translation_' + getFilterDomain(domain), result, single, context, domain);
}
return result;
};
if (initialData) {

@@ -334,4 +434,20 @@ setLocaleData(initialData, initialDomain);

if (hooks) {
/**
* @param {string} hookName
*/
var onHookAddedOrRemoved = function onHookAddedOrRemoved(hookName) {
if (I18N_HOOK_REGEXP.test(hookName)) {
notifyListeners();
}
};
hooks.addAction('hookAdded', 'core/i18n', onHookAddedOrRemoved);
hooks.addAction('hookRemoved', 'core/i18n', onHookAddedOrRemoved);
}
return {
getLocaleData: getLocaleData,
setLocaleData: setLocaleData,
subscribe: subscribe,
__: __,

@@ -341,3 +457,4 @@ _x: _x,

_nx: _nx,
isRTL: isRTL
isRTL: isRTL,
hasTranslation: hasTranslation
};

@@ -344,0 +461,0 @@ };

@@ -6,7 +6,11 @@ "use strict";

});
exports.isRTL = exports._nx = exports._n = exports._x = exports.__ = exports.setLocaleData = void 0;
exports.hasTranslation = exports.isRTL = exports._nx = exports._n = exports._x = exports.__ = exports.subscribe = exports.setLocaleData = exports.getLocaleData = exports.default = void 0;
var _createI18n = require("./create-i18n");
var _hooks = require("@wordpress/hooks");
var _createI18n = require("./create-i18n");
/**
* Internal dependencies
*/

@@ -16,9 +20,8 @@ /**

*/
var i18n = (0, _createI18n.createI18n)(undefined, undefined, _hooks.defaultHooks);
/**
* Internal dependencies
* Default, singleton instance of `I18n`.
*/
var i18n = (0, _createI18n.createI18n)(undefined, undefined, {
applyFilters: _hooks.applyFilters
});
var _default = i18n;
/*

@@ -31,5 +34,18 @@ * Comments in this file are duplicated from ./i18n due to

* @typedef {import('./create-i18n').LocaleData} LocaleData
* @typedef {import('./create-i18n').SubscribeCallback} SubscribeCallback
* @typedef {import('./create-i18n').UnsubscribeCallback} UnsubscribeCallback
*/
/**
* Returns locale data by domain in a Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*
* @param {string} [domain] Domain for which to get the data.
* @return {LocaleData} Locale data.
*/
exports.default = _default;
var getLocaleData = i18n.getLocaleData.bind(i18n);
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -44,4 +60,14 @@ * Jed-formatted JSON object shape.

exports.getLocaleData = getLocaleData;
var setLocaleData = i18n.setLocaleData.bind(i18n);
/**
* Subscribes to changes of locale data
*
* @param {SubscribeCallback} callback Subscription callback
* @return {UnsubscribeCallback} Unsubscribe callback
*/
exports.setLocaleData = setLocaleData;
var subscribe = i18n.subscribe.bind(i18n);
/**
* Retrieve the translation of text.

@@ -57,3 +83,3 @@ *

exports.setLocaleData = setLocaleData;
exports.subscribe = subscribe;

@@ -130,3 +156,14 @@ var __ = i18n.__.bind(i18n);

var isRTL = i18n.isRTL.bind(i18n);
/**
* Check if there is a translation for a given string (in singular form).
*
* @param {string} single Singular form of the string to look up.
* @param {string} [context] Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
* @return {boolean} Whether the translation exists or not.
*/
exports.isRTL = isRTL;
var hasTranslation = i18n.hasTranslation.bind(i18n);
exports.hasTranslation = hasTranslation;
//# sourceMappingURL=default-i18n.js.map
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {

@@ -8,3 +10,6 @@ value: true

sprintf: true,
defaultI18n: true,
setLocaleData: true,
getLocaleData: true,
subscribe: true,
__: true,

@@ -14,3 +19,4 @@ _x: true,

_nx: true,
isRTL: true
isRTL: true,
hasTranslation: true
};

@@ -23,2 +29,8 @@ Object.defineProperty(exports, "sprintf", {

});
Object.defineProperty(exports, "defaultI18n", {
enumerable: true,
get: function get() {
return _defaultI18n.default;
}
});
Object.defineProperty(exports, "setLocaleData", {

@@ -30,2 +42,14 @@ enumerable: true,

});
Object.defineProperty(exports, "getLocaleData", {
enumerable: true,
get: function get() {
return _defaultI18n.getLocaleData;
}
});
Object.defineProperty(exports, "subscribe", {
enumerable: true,
get: function get() {
return _defaultI18n.subscribe;
}
});
Object.defineProperty(exports, "__", {

@@ -61,2 +85,8 @@ enumerable: true,

});
Object.defineProperty(exports, "hasTranslation", {
enumerable: true,
get: function get() {
return _defaultI18n.hasTranslation;
}
});

@@ -79,3 +109,3 @@ var _sprintf = require("./sprintf");

var _defaultI18n = require("./default-i18n");
var _defaultI18n = _interopRequireWildcard(require("./default-i18n"));
//# sourceMappingURL=index.js.map

@@ -5,7 +5,10 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## 3.18.0 (2021-02-2)
## 3.19.0-next.0 (2021-03-11)
### Enhancements
- Allow the return values of i18n functions to be filtered ([#27966](https://github.com/WordPress/gutenberg/pull/27966))
- Export the default `I18n` instance as `defaultI18n`, in addition to already exported bound methods.
- Add new `getLocaleData` method to get the internal Tannin locale data object.
- Add new `subscribe` method to subscribe to changes in the internal locale data.
- Add new `hasTranslation` method to determine whether a translation for a string is available.

@@ -12,0 +15,0 @@ ## 3.17.0 (2020-12-17)

{
"name": "@wordpress/i18n",
"version": "3.18.0",
"version": "3.19.1-next.53e2173694.0",
"description": "WordPress internationalization (i18n) library.",

@@ -30,3 +30,3 @@ "author": "The WordPress Contributors",

"@babel/runtime": "^7.12.5",
"@wordpress/hooks": "^2.11.1",
"@wordpress/hooks": "^2.12.1-next.53e2173694.0",
"gettext-parser": "^1.3.1",

@@ -41,3 +41,3 @@ "lodash": "^4.17.19",

},
"gitHead": "76eba7cb18dbb129b1b1ddf9fd4a8a87bace0962"
"gitHead": "cac63ac5e871791c3ea5bc6a5a7dd06d6229cccb"
}

@@ -38,3 +38,3 @@ # Internationalization (i18n)

- _initialDomain_ `[string]`: Domain for which configuration applies.
- _hooks_ `[ApplyFiltersInterface]`: Hooks implementation.
- _hooks_ `[Hooks]`: Hooks implementation.

@@ -45,2 +45,36 @@ _Returns_

<a name="defaultI18n" href="#defaultI18n">#</a> **defaultI18n**
Default, singleton instance of `I18n`.
<a name="getLocaleData" href="#getLocaleData">#</a> **getLocaleData**
Returns locale data by domain in a Jed-formatted JSON object shape.
_Related_
- <http://messageformat.github.io/Jed/>
_Parameters_
- _domain_ `[string]`: Domain for which to get the data.
_Returns_
- `LocaleData`: Locale data.
<a name="hasTranslation" href="#hasTranslation">#</a> **hasTranslation**
Check if there is a translation for a given string (in singular form).
_Parameters_
- _single_ `string`: Singular form of the string to look up.
- _context_ `[string]`: Context information for the translators.
- _domain_ `[string]`: Domain to retrieve the translated text.
_Returns_
- `boolean`: Whether the translation exists or not.
<a name="isRTL" href="#isRTL">#</a> **isRTL**

@@ -91,2 +125,14 @@

<a name="subscribe" href="#subscribe">#</a> **subscribe**
Subscribes to changes of locale data
_Parameters_
- _callback_ `SubscribeCallback`: Subscription callback
_Returns_
- `UnsubscribeCallback`: Unsubscribe callback
<a name="_n" href="#_n">#</a> **\_n**

@@ -93,0 +139,0 @@

@@ -25,5 +25,19 @@ /**

/* eslint-disable jsdoc/valid-types */
/*
* Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,
* `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.
*/
const I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
/**
* @typedef {(domain?: string) => LocaleData} GetLocaleData
*
* Returns locale data by domain in a
* Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*/
/**
* @typedef {(data?: LocaleData, domain?: string) => void} SetLocaleData
*
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -34,3 +48,10 @@ * Jed-formatted JSON object shape.

*/
/** @typedef {() => void} SubscribeCallback */
/** @typedef {() => void} UnsubscribeCallback */
/**
* @typedef {(callback: SubscribeCallback) => UnsubscribeCallback} Subscribe
*
* Subscribes to changes of locale data
*/
/**
* @typedef {(domain?: string) => string} GetFilterDomain

@@ -80,5 +101,7 @@ * Retrieve the domain to use when calling domain-specific filters.

/**
* @typedef {{ applyFilters: (hookName:string, ...args: unknown[]) => unknown}} ApplyFiltersInterface
* @typedef {(single: string, context?: string, domain?: string) => boolean} HasTranslation
*
* Check if there is a translation for a given string in singular form.
*/
/* eslint-enable jsdoc/valid-types */
/** @typedef {import('@wordpress/hooks').Hooks} Hooks */

@@ -89,4 +112,6 @@ /**

* @typedef I18n
* @property {GetLocaleData} getLocaleData Returns locale data by domain in a Jed-formatted JSON object shape.
* @property {SetLocaleData} setLocaleData Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
* @property {Subscribe} subscribe Subscribes to changes of Tannin locale data.
* @property {__} __ Retrieve the translation of text.

@@ -99,2 +124,3 @@ * @property {_x} _x Retrieve translated string with gettext context.

* @property {IsRtl} isRTL Check if current locale is RTL.
* @property {HasTranslation} hasTranslation Check if there is a translation for a given string.
*/

@@ -107,3 +133,3 @@

* @param {string} [initialDomain] Domain for which configuration applies.
* @param {ApplyFiltersInterface} [hooks] Hooks implementation.
* @param {Hooks} [hooks] Hooks implementation.
* @return {I18n} I18n instance

@@ -119,4 +145,27 @@ */

/** @type {SetLocaleData} */
const setLocaleData = ( data, domain = 'default' ) => {
const listeners = new Set();
const notifyListeners = () => {
listeners.forEach( ( listener ) => listener() );
};
/**
* Subscribe to changes of locale data.
*
* @param {SubscribeCallback} callback Subscription callback.
* @return {UnsubscribeCallback} Unsubscribe callback.
*/
const subscribe = ( callback ) => {
listeners.add( callback );
return () => listeners.delete( callback );
};
/** @type {GetLocaleData} */
const getLocaleData = ( domain = 'default' ) => tannin.data[ domain ];
/**
* @param {LocaleData} [data]
* @param {string} [domain]
*/
const doSetLocaleData = ( data, domain = 'default' ) => {
tannin.data[ domain ] = {

@@ -136,2 +185,8 @@ ...DEFAULT_LOCALE_DATA,

/** @type {SetLocaleData} */
const setLocaleData = ( data, domain ) => {
doSetLocaleData( data, domain );
notifyListeners();
};
/**

@@ -160,3 +215,4 @@ * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not

if ( ! tannin.data[ domain ] ) {
setLocaleData( undefined, domain );
// use `doSetLocaleData` to set silently, without notifying listeners
doSetLocaleData( undefined, domain );
}

@@ -168,8 +224,3 @@

/** @type {GetFilterDomain} */
const getFilterDomain = ( domain ) => {
if ( typeof domain === 'undefined' ) {
return 'default';
}
return domain;
};
const getFilterDomain = ( domain = 'default' ) => domain;

@@ -179,2 +230,6 @@ /** @type {__} */

let translation = dcnpgettext( domain, undefined, text );
if ( ! hooks ) {
return translation;
}
/**

@@ -187,5 +242,2 @@ * Filters text with its translation.

*/
if ( typeof hooks === 'undefined' ) {
return translation;
}
translation = /** @type {string} */ (

@@ -212,2 +264,6 @@ /** @type {*} */ hooks.applyFilters(

let translation = dcnpgettext( domain, context, text );
if ( ! hooks ) {
return translation;
}
/**

@@ -221,5 +277,2 @@ * Filters text with its translation based on context information.

*/
if ( typeof hooks === 'undefined' ) {
return translation;
}
translation = /** @type {string} */ (

@@ -254,5 +307,6 @@ /** @type {*} */ hooks.applyFilters(

);
if ( typeof hooks === 'undefined' ) {
if ( ! hooks ) {
return translation;
}
/**

@@ -298,5 +352,6 @@ * Filters the singular or plural form of a string.

);
if ( typeof hooks === 'undefined' ) {
if ( ! hooks ) {
return translation;
}
/**

@@ -342,2 +397,38 @@ * Filters the singular or plural form of a string with gettext context.

/** @type {HasTranslation} */
const hasTranslation = ( single, context, domain ) => {
const key = context ? context + '\u0004' + single : single;
let result = !! tannin.data?.[ domain ?? 'default' ]?.[ key ];
if ( hooks ) {
/**
* Filters the presence of a translation in the locale data.
*
* @param {boolean} hasTranslation Whether the translation is present or not..
* @param {string} single The singular form of the translated text (used as key in locale data)
* @param {string} context Context information for the translators.
* @param {string} domain Text domain. Unique identifier for retrieving translated strings.
*/
result = /** @type { boolean } */ (
/** @type {*} */ hooks.applyFilters(
'i18n.has_translation',
result,
single,
context,
domain
)
);
result = /** @type { boolean } */ (
/** @type {*} */ hooks.applyFilters(
'i18n.has_translation_' + getFilterDomain( domain ),
result,
single,
context,
domain
)
);
}
return result;
};
if ( initialData ) {

@@ -347,4 +438,20 @@ setLocaleData( initialData, initialDomain );

if ( hooks ) {
/**
* @param {string} hookName
*/
const onHookAddedOrRemoved = ( hookName ) => {
if ( I18N_HOOK_REGEXP.test( hookName ) ) {
notifyListeners();
}
};
hooks.addAction( 'hookAdded', 'core/i18n', onHookAddedOrRemoved );
hooks.addAction( 'hookRemoved', 'core/i18n', onHookAddedOrRemoved );
}
return {
getLocaleData,
setLocaleData,
subscribe,
__,

@@ -355,3 +462,4 @@ _x,

isRTL,
hasTranslation,
};
};
/**
* Internal dependencies
*/
import { createI18n } from './create-i18n';
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
import { defaultHooks } from '@wordpress/hooks';
const i18n = createI18n( undefined, undefined, defaultHooks );
/**
* Internal dependencies
* Default, singleton instance of `I18n`.
*/
import { createI18n } from './create-i18n';
export default i18n;
const i18n = createI18n( undefined, undefined, { applyFilters } );
/*

@@ -20,5 +25,17 @@ * Comments in this file are duplicated from ./i18n due to

* @typedef {import('./create-i18n').LocaleData} LocaleData
* @typedef {import('./create-i18n').SubscribeCallback} SubscribeCallback
* @typedef {import('./create-i18n').UnsubscribeCallback} UnsubscribeCallback
*/
/**
* Returns locale data by domain in a Jed-formatted JSON object shape.
*
* @see http://messageformat.github.io/Jed/
*
* @param {string} [domain] Domain for which to get the data.
* @return {LocaleData} Locale data.
*/
export const getLocaleData = i18n.getLocaleData.bind( i18n );
/**
* Merges locale data into the Tannin instance by domain. Accepts data in a

@@ -35,2 +52,10 @@ * Jed-formatted JSON object shape.

/**
* Subscribes to changes of locale data
*
* @param {SubscribeCallback} callback Subscription callback
* @return {UnsubscribeCallback} Unsubscribe callback
*/
export const subscribe = i18n.subscribe.bind( i18n );
/**
* Retrieve the translation of text.

@@ -104,1 +129,11 @@ *

export const isRTL = i18n.isRTL.bind( i18n );
/**
* Check if there is a translation for a given string (in singular form).
*
* @param {string} single Singular form of the string to look up.
* @param {string} [context] Context information for the translators.
* @param {string} [domain] Domain to retrieve the translated text.
* @return {boolean} Whether the translation exists or not.
*/
export const hasTranslation = i18n.hasTranslation.bind( i18n );
export { sprintf } from './sprintf';
export * from './create-i18n';
export { setLocaleData, __, _x, _n, _nx, isRTL } from './default-i18n';
export {
default as defaultI18n,
setLocaleData,
getLocaleData,
subscribe,
__,
_x,
_n,
_nx,
isRTL,
hasTranslation,
} from './default-i18n';
/* eslint-disable @wordpress/i18n-text-domain, @wordpress/i18n-translator-comments */
/**
* WordPress dependencies
*/
import { createHooks } from '@wordpress/hooks';
/**
* Internal dependencies

@@ -195,60 +200,163 @@ */

describe( 'i18n filters', () => {
function createHooksWithI18nFilters() {
const hooks = createHooks();
hooks.addFilter(
'i18n.gettext',
'test',
( translation ) => translation + '/i18n.gettext'
);
hooks.addFilter(
'i18n.gettext_default',
'test',
( translation ) => translation + '/i18n.gettext_default'
);
hooks.addFilter(
'i18n.gettext_domain',
'test',
( translation ) => translation + '/i18n.gettext_domain'
);
hooks.addFilter(
'i18n.ngettext',
'test',
( translation ) => translation + '/i18n.ngettext'
);
hooks.addFilter(
'i18n.ngettext_default',
'test',
( translation ) => translation + '/i18n.ngettext_default'
);
hooks.addFilter(
'i18n.ngettext_domain',
'test',
( translation ) => translation + '/i18n.ngettext_domain'
);
hooks.addFilter(
'i18n.gettext_with_context',
'test',
( translation, text, context ) =>
translation + `/i18n.gettext_with_${ context }`
);
hooks.addFilter(
'i18n.gettext_with_context_default',
'test',
( translation, text, context ) =>
translation + `/i18n.gettext_with_${ context }_default`
);
hooks.addFilter(
'i18n.gettext_with_context_domain',
'test',
( translation, text, context ) =>
translation + `/i18n.gettext_with_${ context }_domain`
);
hooks.addFilter(
'i18n.ngettext_with_context',
'test',
( translation, single, plural, number, context ) =>
translation + `/i18n.ngettext_with_${ context }`
);
hooks.addFilter(
'i18n.ngettext_with_context_default',
'test',
( translation, single, plural, number, context ) =>
translation + `/i18n.ngettext_with_${ context }_default`
);
hooks.addFilter(
'i18n.ngettext_with_context_domain',
'test',
( translation, single, plural, number, context ) =>
translation + `/i18n.ngettext_with_${ context }_domain`
);
hooks.addFilter(
'i18n.has_translation',
'test',
( hasTranslation, single, context, domain ) => {
if (
single === 'Always' &&
! context &&
( domain ?? 'default' ) === 'default'
) {
return true;
}
return hasTranslation;
}
);
return hooks;
}
test( '__() calls filters', () => {
const i18n = createI18n( undefined, undefined, {
applyFilters: ( filter, translation ) => translation + filter,
} );
const hooks = createHooksWithI18nFilters();
const i18n = createI18n( undefined, undefined, hooks );
expect( i18n.__( 'hello' ) ).toEqual(
'helloi18n.gettexti18n.gettext_default'
'hello/i18n.gettext/i18n.gettext_default'
);
expect( i18n.__( 'hello', 'domain' ) ).toEqual(
'helloi18n.gettexti18n.gettext_domain'
'hello/i18n.gettext/i18n.gettext_domain'
);
} );
test( '_x() calls filters', () => {
const i18n = createI18n( undefined, undefined, {
applyFilters: ( filter, translation ) => translation + filter,
} );
expect( i18n._x( 'hello', 'context' ) ).toEqual(
'helloi18n.gettext_with_contexti18n.gettext_with_context_default'
const hooks = createHooksWithI18nFilters();
const i18n = createI18n( undefined, undefined, hooks );
expect( i18n._x( 'hello', 'ctx' ) ).toEqual(
'hello/i18n.gettext_with_ctx/i18n.gettext_with_ctx_default'
);
expect( i18n._x( 'hello', 'context', 'domain' ) ).toEqual(
'helloi18n.gettext_with_contexti18n.gettext_with_context_domain'
expect( i18n._x( 'hello', 'ctx', 'domain' ) ).toEqual(
'hello/i18n.gettext_with_ctx/i18n.gettext_with_ctx_domain'
);
} );
test( '_n() calls filters', () => {
const i18n = createI18n( undefined, undefined, {
applyFilters: ( filter, translation ) => translation + filter,
} );
const hooks = createHooksWithI18nFilters();
const i18n = createI18n( undefined, undefined, hooks );
expect( i18n._n( 'hello', 'hellos', 1 ) ).toEqual(
'helloi18n.ngettexti18n.ngettext_default'
'hello/i18n.ngettext/i18n.ngettext_default'
);
expect( i18n._n( 'hello', 'hellos', 1, 'domain' ) ).toEqual(
'helloi18n.ngettexti18n.ngettext_domain'
'hello/i18n.ngettext/i18n.ngettext_domain'
);
expect( i18n._n( 'hello', 'hellos', 2 ) ).toEqual(
'hellosi18n.ngettexti18n.ngettext_default'
'hellos/i18n.ngettext/i18n.ngettext_default'
);
expect( i18n._n( 'hello', 'hellos', 2, 'domain' ) ).toEqual(
'hellosi18n.ngettexti18n.ngettext_domain'
'hellos/i18n.ngettext/i18n.ngettext_domain'
);
} );
test( '_nx() calls filters', () => {
const i18n = createI18n( undefined, undefined, {
applyFilters: ( filter, translation ) => translation + filter,
} );
expect( i18n._nx( 'hello', 'hellos', 1, 'context' ) ).toEqual(
'helloi18n.ngettext_with_contexti18n.ngettext_with_context_default'
const hooks = createHooksWithI18nFilters();
const i18n = createI18n( undefined, undefined, hooks );
expect( i18n._nx( 'hello', 'hellos', 1, 'ctx' ) ).toEqual(
'hello/i18n.ngettext_with_ctx/i18n.ngettext_with_ctx_default'
);
expect( i18n._nx( 'hello', 'hellos', 1, 'context', 'domain' ) ).toEqual(
'helloi18n.ngettext_with_contexti18n.ngettext_with_context_domain'
expect( i18n._nx( 'hello', 'hellos', 1, 'ctx', 'domain' ) ).toEqual(
'hello/i18n.ngettext_with_ctx/i18n.ngettext_with_ctx_domain'
);
expect( i18n._nx( 'hello', 'hellos', 2, 'context' ) ).toEqual(
'hellosi18n.ngettext_with_contexti18n.ngettext_with_context_default'
expect( i18n._nx( 'hello', 'hellos', 2, 'ctx' ) ).toEqual(
'hellos/i18n.ngettext_with_ctx/i18n.ngettext_with_ctx_default'
);
expect( i18n._nx( 'hello', 'hellos', 2, 'context', 'domain' ) ).toEqual(
'hellosi18n.ngettext_with_contexti18n.ngettext_with_context_domain'
expect( i18n._nx( 'hello', 'hellos', 2, 'ctx', 'domain' ) ).toEqual(
'hellos/i18n.ngettext_with_ctx/i18n.ngettext_with_ctx_domain'
);
} );
test( 'hasTranslation() calls filters', () => {
const hooks = createHooksWithI18nFilters();
const { hasTranslation } = createI18n( frenchLocale, undefined, hooks );
expect( hasTranslation( 'hello' ) ).toBe( true );
expect( hasTranslation( 'hello', 'not a greeting' ) ).toBe( false );
expect( hasTranslation( 'Always' ) ).toBe( true );
expect( hasTranslation( 'Always', 'other context' ) ).toBe( false );
expect( hasTranslation( 'Always', undefined, 'domain' ) ).toBe( false );
} );
} );
/* eslint-enable @wordpress/i18n-text-domain, @wordpress/i18n-translator-comments */

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc