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 isRTL function #20298

Merged
merged 4 commits into from
Feb 26, 2020
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
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.
sirreal marked this conversation as resolved.
Show resolved Hide resolved

**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' );
}