Skip to content

Commit

Permalink
switch from I18n class to createI18n fn
Browse files Browse the repository at this point in the history
  • Loading branch information
James Newell committed Mar 4, 2020
1 parent 663f14c commit 80b30bc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 56 deletions.
7 changes: 5 additions & 2 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ For a complete example, see the [Internationalization section of the Block Edito

<!-- START TOKEN(Autogenerated API docs) -->

<a name="I18n" href="#I18n">#</a> **I18n**
<a name="createI18n" href="#createI18n">#</a> **createI18n**

Instantiable I18n methods
_Parameters_

- _initialData_ `[LocaleData]`: Locale data configuration.
- _initialDomain_ `[string]`: Domain for which configuration applies.

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

Expand Down
97 changes: 51 additions & 46 deletions packages/i18n/src/i18n.js → packages/i18n/src/create-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ const DEFAULT_LOCALE_DATA = {
};

/**
* Instantiable I18n methods
* @param {LocaleData} [initialData] Locale data configuration.
* @param {string} [initialDomain] Domain for which configuration applies.
*/
export class I18n {
export const createI18n = ( initialData, initialDomain ) => {
/**
* @param {LocaleData} [data] Locale data configuration.
* @param {string} [domain] Domain for which configuration applies.
* The underlying instance of Tannin to which exported functions interface.
*
* @type {Tannin}
*/
constructor( data, domain ) {
/**
* The underlying instance of Tannin to which exported functions interface.
*
* @type {Tannin}
*/
this.tannin = new Tannin( {} );
this.setLocaleData( data, domain );
}
const tannin = new Tannin( {} );

/**
* Merges locale data into the Tannin instance by domain. Accepts data in a
Expand All @@ -46,20 +40,20 @@ export class I18n {
* @param {LocaleData} [data] Locale data configuration.
* @param {string} [domain] Domain for which configuration applies.
*/
setLocaleData( data, domain = 'default' ) {
this.tannin.data[ domain ] = {
const setLocaleData = ( data, domain = 'default' ) => {
tannin.data[ domain ] = {
...DEFAULT_LOCALE_DATA,
...this.tannin.data[ domain ],
...tannin.data[ domain ],
...data,
};

// Populate default domain configuration (supported locale date which omits
// a plural forms expression).
this.tannin.data[ domain ][ '' ] = {
tannin.data[ domain ][ '' ] = {
...DEFAULT_LOCALE_DATA[ '' ],
...this.tannin.data[ domain ][ '' ],
...tannin.data[ domain ][ '' ],
};
}
};

/**
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
Expand All @@ -76,19 +70,19 @@ export class I18n {
*
* @return {string} The translated string.
*/
dcnpgettext( domain = 'default', context, single, plural, number ) {
if ( ! this.tannin.data[ domain ] ) {
this.setLocaleData( undefined, domain );
const dcnpgettext = (
domain = 'default',
context,
single,
plural,
number
) => {
if ( ! tannin.data[ domain ] ) {
setLocaleData( undefined, domain );
}

return this.tannin.dcnpgettext(
domain,
context,
single,
plural,
number
);
}
return tannin.dcnpgettext( domain, context, single, plural, number );
};

/**
* Retrieve the translation of text.
Expand All @@ -100,9 +94,9 @@ export class I18n {
*
* @return {string} Translated text.
*/
__( text, domain ) {
return this.dcnpgettext( domain, undefined, text );
}
const __ = ( text, domain ) => {
return dcnpgettext( domain, undefined, text );
};

/**
* Retrieve translated string with gettext context.
Expand All @@ -115,9 +109,9 @@ export class I18n {
*
* @return {string} Translated context string without pipe.
*/
_x( text, context, domain ) {
return this.dcnpgettext( domain, context, text );
}
const _x = ( text, context, domain ) => {
return dcnpgettext( domain, context, text );
};

/**
* Translates and retrieves the singular or plural form based on the supplied
Expand All @@ -133,9 +127,9 @@ export class I18n {
*
* @return {string} The translated singular or plural form.
*/
_n( single, plural, number, domain ) {
return this.dcnpgettext( domain, undefined, single, plural, number );
}
const _n = ( single, plural, number, domain ) => {
return dcnpgettext( domain, undefined, single, plural, number );
};

/**
* Translates and retrieves the singular or plural form based on the supplied
Expand All @@ -152,9 +146,9 @@ export class I18n {
*
* @return {string} The translated singular or plural form.
*/
_nx( single, plural, number, context, domain ) {
return this.dcnpgettext( domain, context, single, plural, number );
}
const _nx = ( single, plural, number, context, domain ) => {
return dcnpgettext( domain, context, single, plural, number );
};

/**
* Check if current locale is RTL.
Expand All @@ -166,7 +160,18 @@ export class I18n {
*
* @return {boolean} Whether locale is RTL.
*/
isRTL() {
return 'rtl' === this._x( 'ltr', 'text direction' );
}
}
const isRTL = () => {
return 'rtl' === _x( 'ltr', 'text direction' );
};

setLocaleData( initialData, initialDomain );

return {
setLocaleData,
__,
_x,
_n,
_nx,
isRTL,
};
};
4 changes: 2 additions & 2 deletions packages/i18n/src/default-i18n.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { I18n } from './i18n';
import { createI18n } from './create-i18n';

export const i18n = new I18n();
export const i18n = createI18n();

/*
* Comments in this file are duplicated from ./i18n due to
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { sprintf } from './sprintf';
export { I18n } from './i18n';
export { createI18n } from './create-i18n';
export { i18n, setLocaleData, __, _x, _n, _nx, isRTL } from './default-i18n';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { I18n } from '../';
import { createI18n } from '..';

const strayaLocale = {
hello: [ 'gday' ],
Expand All @@ -11,14 +11,14 @@ const frenchLocale = {
hello: [ 'bonjour' ],
};

describe( 'I18n', () => {
describe( 'createI18n', () => {
test( 'instantiated with locale data', () => {
const straya = new I18n( strayaLocale );
const straya = createI18n( strayaLocale );
expect( straya.__( 'hello' ) ).toEqual( 'gday' );
} );
test( 'multiple instances maintain their own distinct locale data', () => {
const straya = new I18n();
const french = new I18n();
const straya = createI18n();
const french = createI18n();

straya.setLocaleData( strayaLocale );
french.setLocaleData( frenchLocale );
Expand Down

0 comments on commit 80b30bc

Please sign in to comment.