From d137d796b752f51598de34330b9d15b9fe89dd97 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Feb 2020 22:05:38 +0100 Subject: [PATCH 1/4] Add isRTL function --- packages/i18n/README.md | 8 ++++++++ packages/i18n/src/index.js | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/i18n/README.md b/packages/i18n/README.md index 50376d53c36475..1d3394ad892125 100644 --- a/packages/i18n/README.md +++ b/packages/i18n/README.md @@ -27,6 +27,14 @@ For a complete example, see the [Internationalization section of the Block Edito +# **isRTL** + +Check if current locale is RTL. + +_Returns_ + +- `boolean`: Whether locale is RTL. + # **setLocaleData** Merges locale data into the Tannin instance by domain. Accepts data in a diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index c696cb9f764006..2c036fd829b40e 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -150,6 +150,15 @@ export function _nx( single, plural, number, context, domain ) { return dcnpgettext( domain, context, single, plural, number ); } +/** + * Check if current locale is RTL. + * + * @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. From 0655f5253cb719e046f18fce98238e50efa60286 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Feb 2020 22:07:38 +0100 Subject: [PATCH 2/4] Add isRTL tests - Better test isolation by re-requiring the module - Add `beforeEach` setting up default locale data - Add isRTL tests --- packages/i18n/src/test/index.js | 52 ++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index b5e68537754df4..5246bc8f3659b1 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -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 ); @@ -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' @@ -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' ) @@ -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 @@ -93,6 +126,7 @@ describe( 'i18n', () => { describe( 'setLocaleData', () => { beforeAll( () => { + setDefaultLocalData(); setLocaleData( additionalLocaleData, 'test_domain' ); } ); @@ -147,3 +181,7 @@ describe( 'i18n', () => { } ); } ); } ); + +function setDefaultLocalData() { + setLocaleData( localeData, 'test_domain' ); +} From 2e8620a63ba4d76572ac9389142f5d6ffdb2cb47 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Feb 2020 22:19:15 +0100 Subject: [PATCH 3/4] Add changelog entry --- packages/i18n/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/i18n/CHANGELOG.md b/packages/i18n/CHANGELOG.md index 8eb9757eb2088c..700b3b520eefbf 100644 --- a/packages/i18n/CHANGELOG.md +++ b/packages/i18n/CHANGELOG.md @@ -1,3 +1,9 @@ +## Master + +### New Feature + +- Add `isRTL` function (#20298) + ## 3.1.0 (2018-11-15) ### Enhancements From 801ac7e1509ed0567701289f84c61a78808816f7 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 19 Feb 2020 10:02:20 +0100 Subject: [PATCH 4/4] Add note explaining RTL. Courtesy of https://developer.mozilla.org/en-US/docs/Glossary/rtl --- packages/i18n/README.md | 5 +++++ packages/i18n/src/index.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/packages/i18n/README.md b/packages/i18n/README.md index 1d3394ad892125..21847b5f3f0172 100644 --- a/packages/i18n/README.md +++ b/packages/i18n/README.md @@ -31,6 +31,11 @@ For a complete example, see the [Internationalization section of the Block Edito 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. diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 2c036fd829b40e..2b4286819c9cad 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -153,6 +153,11 @@ export function _nx( single, plural, number, context, domain ) { /** * 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() {