Skip to content

Commit

Permalink
i18n: Add isRTL function (#20298)
Browse files Browse the repository at this point in the history
* Add isRTL function
* Refactor unit tests for isolation
* Add isRTL unit tests

RTL documentation courtesy of:

Courtesy of https://developer.mozilla.org/en-US/docs/Glossary/rtl
  • Loading branch information
sirreal authored Feb 26, 2020
1 parent 5dab5f7 commit 2ff5493
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/i18n/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New Feature

- Add `isRTL` function (#20298)

## 3.1.0 (2018-11-15)

### Enhancements
Expand Down
13 changes: 13 additions & 0 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ For a complete example, see the [Internationalization section of the Block Edito

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

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

Check if current locale is RTL.

**RTL (Right To Left)** is a locale property indicating that text is written from right to left.
For example, the `he` locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common
language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages,
including English (`en`, `en-US`, `en-GB`, etc.), Spanish (`es`), and French (`fr`).

_Returns_

- `boolean`: Whether locale is RTL.

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

Merges locale data into the Tannin instance by domain. Accepts data in a
Expand Down
14 changes: 14 additions & 0 deletions packages/i18n/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ export function _nx( single, plural, number, context, domain ) {
return dcnpgettext( domain, context, single, plural, number );
}

/**
* Check if current locale is RTL.
*
* **RTL (Right To Left)** is a locale property indicating that text is written from right to left.
* For example, the `he` locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common
* language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages,
* including English (`en`, `en-US`, `en-GB`, etc.), Spanish (`es`), and French (`fr`).
*
* @return {boolean} Whether locale is RTL.
*/
export function isRTL() {
return 'rtl' === _x( 'ltr', 'text direction' );
}

/**
* Returns a formatted string. If an error occurs in applying the format, the
* original format string is returned.
Expand Down
52 changes: 45 additions & 7 deletions packages/i18n/src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Internal dependencies
*/
import { sprintf, __, _x, _n, _nx, setLocaleData } from '../';

// Mock memoization as identity function. Inline since Jest errors on out-of-
// scope references in a mock callback.
jest.mock( 'memize', () => ( fn ) => fn );
Expand Down Expand Up @@ -31,22 +26,34 @@ const additionalLocaleData = {
'%d cat': [ '%d chat', '%d chats' ],
};

setLocaleData( localeData, 'test_domain' );
// Get clean locale data
let sprintf, __, _x, _n, _nx, isRTL, setLocaleData;
beforeEach( () => {
const module = require.resolve( '..' );
delete require.cache[ module ];
( { sprintf, __, _x, _n, _nx, isRTL, setLocaleData } = require( '..' ) );
} );

describe( 'i18n', () => {
describe( '__', () => {
beforeEach( setDefaultLocalData );

it( 'use the translation', () => {
expect( __( 'hello', 'test_domain' ) ).toBe( 'bonjour' );
} );
} );

describe( '_x', () => {
beforeEach( setDefaultLocalData );

it( 'use the translation with context', () => {
expect( _x( 'feed', 'verb', 'test_domain' ) ).toBe( 'nourrir' );
} );
} );

describe( '_n', () => {
beforeEach( setDefaultLocalData );

it( 'use the plural form', () => {
expect( _n( '%d banana', '%d bananas', 3, 'test_domain' ) ).toBe(
'%d bananes'
Expand All @@ -61,6 +68,8 @@ describe( 'i18n', () => {
} );

describe( '_nx', () => {
beforeEach( setDefaultLocalData );

it( 'use the plural form', () => {
expect(
_nx( '%d apple', '%d apples', 3, 'fruit', 'test_domain' )
Expand All @@ -74,7 +83,31 @@ describe( 'i18n', () => {
} );
} );

describe( 'sprintf()', () => {
describe( 'isRTL', () => {
const ARLocaleData = {
'': {
plural_forms:
'nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;',
language: 'ar',
localeSlug: 'ar',
},
'text direction\u0004ltr': [ 'rtl' ],
Back: [ 'رجوع' ],
};

it( 'is false for non-rtl', () => {
expect( isRTL() ).toBe( false );
} );

it( 'is true for rtl', () => {
setLocaleData( ARLocaleData );
expect( isRTL() ).toBe( true );
} );
} );

describe( 'sprintf', () => {
beforeEach( setDefaultLocalData );

it( 'absorbs errors', () => {
// Disable reason: Failing case is the purpose of the test.
// eslint-disable-next-line @wordpress/valid-sprintf
Expand All @@ -93,6 +126,7 @@ describe( 'i18n', () => {

describe( 'setLocaleData', () => {
beforeAll( () => {
setDefaultLocalData();
setLocaleData( additionalLocaleData, 'test_domain' );
} );

Expand Down Expand Up @@ -147,3 +181,7 @@ describe( 'i18n', () => {
} );
} );
} );

function setDefaultLocalData() {
setLocaleData( localeData, 'test_domain' );
}

0 comments on commit 2ff5493

Please sign in to comment.