Socket
Socket
Sign inDemoInstall

@wordpress/i18n

Package Overview
Dependencies
15
Maintainers
8
Versions
145
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 3.1.0

88

build-module/index.js

@@ -0,8 +1,22 @@

import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
/**
* External dependencies
*/
import Jed from 'jed';
import Tannin from 'tannin';
import memoize from 'memize';
var i18n;
import sprintfjs from 'sprintf-js';
/**
* Default locale data to use for Tannin domain when not otherwise provided.
* Assumes an English plural forms expression.
*
* @type {Object}
*/
var DEFAULT_LOCALE_DATA = {
'': {
plural_forms: 'plural=(n!=1)'
}
};
/**
* Log to console, once per message; or more precisely, per referentially equal

@@ -18,47 +32,28 @@ * argument set. Because Jed throws errors, we log these to the console instead

/**
* Merges locale data into the Jed instance by domain. Creates a new Jed
* instance if one has not yet been assigned.
* The underlying instance of Tannin to which exported functions interface.
*
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} localeData Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
* @type {Tannin}
*/
export function setLocaleData() {
var localeData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
'': {}
};
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
if (!i18n) {
i18n = new Jed({
domain: 'default',
locale_data: {
default: {
'': {}
}
}
});
}
i18n.options.locale_data[domain] = Object.assign({}, i18n.options.locale_data[domain], localeData);
}
var i18n = new Tannin({});
/**
* Returns the current Jed instance, initializing with a default configuration
* if not already assigned.
* Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
*
* @return {Jed} Jed instance.
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} data Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
*/
function getI18n() {
if (!i18n) {
setLocaleData();
}
export function setLocaleData(data) {
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
i18n.data[domain] = _objectSpread({}, DEFAULT_LOCALE_DATA, i18n.data[domain], data); // Populate default domain configuration (supported locale date which omits
// a plural forms expression).
return i18n;
i18n.data[domain][''] = _objectSpread({}, DEFAULT_LOCALE_DATA[''], i18n.data[domain]['']);
}
/**
* Wrapper for Jed's `dcnpgettext`, its most qualified function. Absorbs errors
* which are thrown as the result of invalid translation.
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
* otherwise previously assigned.
*

@@ -76,4 +71,3 @@ * @param {?string} domain Domain to retrieve the translated text.

var dcnpgettext = memoize(function () {
function dcnpgettext() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';

@@ -85,9 +79,8 @@ var context = arguments.length > 1 ? arguments[1] : undefined;

try {
return getI18n().dcnpgettext(domain, context, single, plural, number);
} catch (error) {
logErrorOnce('Jed localization error: \n\n' + error.toString());
return single;
if (!i18n.data[domain]) {
setLocaleData(undefined, domain);
}
});
return i18n.dcnpgettext(domain, context, single, plural, number);
}
/**

@@ -104,2 +97,3 @@ * Retrieve the translation of text.

export function __(text, domain) {

@@ -178,5 +172,5 @@ return dcnpgettext(domain, undefined, text);

return Jed.sprintf.apply(Jed, [format].concat(args));
return sprintfjs.sprintf.apply(sprintfjs, [format].concat(args));
} catch (error) {
logErrorOnce('Jed sprintf error: \n\n' + error.toString());
logErrorOnce('sprintf error: \n\n' + error.toString());
return format;

@@ -183,0 +177,0 @@ }

@@ -15,11 +15,26 @@ "use strict";

var _jed = _interopRequireDefault(require("jed"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _tannin = _interopRequireDefault(require("tannin"));
var _memize = _interopRequireDefault(require("memize"));
var _sprintfJs = _interopRequireDefault(require("sprintf-js"));
/**
* External dependencies
*/
var i18n;
/**
* Default locale data to use for Tannin domain when not otherwise provided.
* Assumes an English plural forms expression.
*
* @type {Object}
*/
var DEFAULT_LOCALE_DATA = {
'': {
plural_forms: 'plural=(n!=1)'
}
};
/**
* Log to console, once per message; or more precisely, per referentially equal

@@ -35,48 +50,28 @@ * argument set. Because Jed throws errors, we log these to the console instead

/**
* Merges locale data into the Jed instance by domain. Creates a new Jed
* instance if one has not yet been assigned.
* The underlying instance of Tannin to which exported functions interface.
*
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} localeData Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
* @type {Tannin}
*/
function setLocaleData() {
var localeData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
'': {}
};
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
if (!i18n) {
i18n = new _jed.default({
domain: 'default',
locale_data: {
default: {
'': {}
}
}
});
}
i18n.options.locale_data[domain] = Object.assign({}, i18n.options.locale_data[domain], localeData);
}
var i18n = new _tannin.default({});
/**
* Returns the current Jed instance, initializing with a default configuration
* if not already assigned.
* Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
*
* @return {Jed} Jed instance.
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} data Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
*/
function setLocaleData(data) {
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
i18n.data[domain] = (0, _objectSpread2.default)({}, DEFAULT_LOCALE_DATA, i18n.data[domain], data); // Populate default domain configuration (supported locale date which omits
// a plural forms expression).
function getI18n() {
if (!i18n) {
setLocaleData();
}
return i18n;
i18n.data[domain][''] = (0, _objectSpread2.default)({}, DEFAULT_LOCALE_DATA[''], i18n.data[domain]['']);
}
/**
* Wrapper for Jed's `dcnpgettext`, its most qualified function. Absorbs errors
* which are thrown as the result of invalid translation.
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
* otherwise previously assigned.
*

@@ -95,3 +90,3 @@ * @param {?string} domain Domain to retrieve the translated text.

var dcnpgettext = (0, _memize.default)(function () {
function dcnpgettext() {
var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';

@@ -103,9 +98,8 @@ var context = arguments.length > 1 ? arguments[1] : undefined;

try {
return getI18n().dcnpgettext(domain, context, single, plural, number);
} catch (error) {
logErrorOnce('Jed localization error: \n\n' + error.toString());
return single;
if (!i18n.data[domain]) {
setLocaleData(undefined, domain);
}
});
return i18n.dcnpgettext(domain, context, single, plural, number);
}
/**

@@ -122,2 +116,3 @@ * Retrieve the translation of text.

function __(text, domain) {

@@ -200,5 +195,5 @@ return dcnpgettext(domain, undefined, text);

return _jed.default.sprintf.apply(_jed.default, [format].concat(args));
return _sprintfJs.default.sprintf.apply(_sprintfJs.default, [format].concat(args));
} catch (error) {
logErrorOnce('Jed sprintf error: \n\n' + error.toString());
logErrorOnce('sprintf error: \n\n' + error.toString());
return format;

@@ -205,0 +200,0 @@ }

@@ -0,1 +1,7 @@

## 3.1.0 (Unreleased)
### Enhancements
- The module has been internally refactored to use [Tannin](https://github.com/aduth/tannin) in place of [Jed](https://github.com/messageformat/Jed/). This has no impact on the public interface of the module, but should come with considerable benefit to performance, memory usage, and bundle size.
## 3.0.0 (2018-09-30)

@@ -2,0 +8,0 @@

{
"name": "@wordpress/i18n",
"version": "3.0.1",
"version": "3.1.0",
"description": "WordPress internationalization (i18n) library.",

@@ -28,5 +28,6 @@ "author": "The WordPress Contributors",

"gettext-parser": "^1.3.1",
"jed": "^1.1.1",
"lodash": "^4.17.10",
"memize": "^1.0.5"
"memize": "^1.0.5",
"sprintf-js": "^1.1.1",
"tannin": "^1.0.1"
},

@@ -39,3 +40,3 @@ "devDependencies": {

},
"gitHead": "0aa5c4340f57a69ab935f9e819d74958aad2e022"
"gitHead": "a177dd725495d102d24aee87394c724985bd9f72"
}
/**
* External dependencies
*/
import Jed from 'jed';
import Tannin from 'tannin';
import memoize from 'memize';
import sprintfjs from 'sprintf-js';
let i18n;
/**
* Default locale data to use for Tannin domain when not otherwise provided.
* Assumes an English plural forms expression.
*
* @type {Object}
*/
const DEFAULT_LOCALE_DATA = {
'': {
plural_forms: 'plural=(n!=1)',
},
};

@@ -19,44 +30,35 @@ /**

/**
* Merges locale data into the Jed instance by domain. Creates a new Jed
* instance if one has not yet been assigned.
* The underlying instance of Tannin to which exported functions interface.
*
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} localeData Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
* @type {Tannin}
*/
export function setLocaleData( localeData = { '': {} }, domain = 'default' ) {
if ( ! i18n ) {
i18n = new Jed( {
domain: 'default',
locale_data: {
default: { '': {} },
},
} );
}
const i18n = new Tannin( {} );
i18n.options.locale_data[ domain ] = Object.assign(
{},
i18n.options.locale_data[ domain ],
localeData
);
}
/**
* Returns the current Jed instance, initializing with a default configuration
* if not already assigned.
* Merges locale data into the Tannin instance by domain. Accepts data in a
* Jed-formatted JSON object shape.
*
* @return {Jed} Jed instance.
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} data Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
*/
function getI18n() {
if ( ! i18n ) {
setLocaleData();
}
export function setLocaleData( data, domain = 'default' ) {
i18n.data[ domain ] = {
...DEFAULT_LOCALE_DATA,
...i18n.data[ domain ],
...data,
};
return i18n;
// Populate default domain configuration (supported locale date which omits
// a plural forms expression).
i18n.data[ domain ][ '' ] = {
...DEFAULT_LOCALE_DATA[ '' ],
...i18n.data[ domain ][ '' ],
};
}
/**
* Wrapper for Jed's `dcnpgettext`, its most qualified function. Absorbs errors
* which are thrown as the result of invalid translation.
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
* otherwise previously assigned.
*

@@ -73,12 +75,10 @@ * @param {?string} domain Domain to retrieve the translated text.

*/
const dcnpgettext = memoize( ( domain = 'default', context, single, plural, number ) => {
try {
return getI18n().dcnpgettext( domain, context, single, plural, number );
} catch ( error ) {
logErrorOnce( 'Jed localization error: \n\n' + error.toString() );
return single;
function dcnpgettext( domain = 'default', context, single, plural, number ) {
if ( ! i18n.data[ domain ] ) {
setLocaleData( undefined, domain );
}
} );
return i18n.dcnpgettext( domain, context, single, plural, number );
}
/**

@@ -163,5 +163,5 @@ * Retrieve the translation of text.

try {
return Jed.sprintf( format, ...args );
return sprintfjs.sprintf( format, ...args );
} catch ( error ) {
logErrorOnce( 'Jed sprintf error: \n\n' + error.toString() );
logErrorOnce( 'sprintf error: \n\n' + error.toString() );

@@ -168,0 +168,0 @@ return format;

@@ -25,9 +25,9 @@ /**

'%d banana': [ 'une banane', '%d bananes' ],
'%d banana': [ '%d banane', '%d bananes' ],
'fruit\u0004%d apple': [ 'une pomme', '%d pommes' ],
'fruit\u0004%d apple': [ '%d pomme', '%d pommes' ],
};
const additionalLocaleData = {
cheeseburger: [ 'hamburger au fromage' ],
'%d cat': [ 'un chat', '%d chats' ],
'%d cat': [ '%d chat', '%d chats' ],
};

@@ -38,24 +38,2 @@

describe( 'i18n', () => {
describe( 'error absorb', () => {
it( '__', () => {
__( 'Hello', 'domain-without-data' );
expect( console ).toHaveErrored();
} );
it( '_x', () => {
_x( 'feed', 'verb', 'domain-without-data' );
expect( console ).toHaveErrored();
} );
it( '_n', () => {
_n( '%d banana', '%d bananas', 3, 'domain-without-data' );
expect( console ).toHaveErrored();
} );
it( '_nx', () => {
_nx( '%d apple', '%d apples', 3, 'fruit', 'domain-without-data' );
expect( console ).toHaveErrored();
} );
} );
describe( '__', () => {

@@ -79,3 +57,3 @@ it( 'use the translation', () => {

it( 'use the singular form', () => {
expect( _n( '%d banana', '%d bananas', 1, 'test_domain' ) ).toBe( 'une banane' );
expect( _n( '%d banana', '%d bananas', 1, 'test_domain' ) ).toBe( '%d banane' );
} );

@@ -90,3 +68,3 @@ } );

it( 'use the singular form', () => {
expect( _nx( '%d apple', '%d apples', 1, 'fruit', 'test_domain' ) ).toBe( 'une pomme' );
expect( _nx( '%d apple', '%d apples', 1, 'fruit', 'test_domain' ) ).toBe( '%d pomme' );
} );

@@ -110,6 +88,20 @@ } );

describe( 'setAdditionalLocale', () => {
describe( 'setLocaleData', () => {
beforeAll( () => {
setLocaleData( additionalLocaleData, 'test_domain' );
} );
it( 'supports omitted plural forms expression', () => {
setLocaleData( {
'': {
domain: 'test_domain2',
lang: 'fr',
},
'%d banana': [ '%d banane', '%d bananes' ],
}, 'test_domain2' );
expect( _n( '%d banana', '%d bananes', 2, 'test_domain2' ) ).toBe( '%d bananes' );
} );
describe( '__', () => {

@@ -131,3 +123,3 @@ it( 'existing translation still available', () => {

it( 'new singular form was added', () => {
expect( _n( '%d cat', '%d cats', 1, 'test_domain' ) ).toBe( 'un chat' );
expect( _n( '%d cat', '%d cats', 1, 'test_domain' ) ).toBe( '%d chat' );
} );

@@ -134,0 +126,0 @@

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