Skip to content

Commit 2413145

Browse files
authored
lib/wp: use Core i18n to read current locale (#102628)
* lib/wp: use Core i18n to read current locale * Fix unit tests: configure the right i18n library
1 parent 6ee7a94 commit 2413145

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

client/lib/wp/localization/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import i18n from 'i18n-calypso';
1+
import { getLocaleData } from '@wordpress/i18n';
22
import { parse, stringify } from 'qs';
33

44
/**
@@ -8,7 +8,8 @@ import { parse, stringify } from 'qs';
88
* @returns {Object} Revised parameters, if non-default locale
99
*/
1010
export function addLocaleQueryParam( params ) {
11-
const locale = i18n.getLocaleVariant() || i18n.getLocaleSlug();
11+
const localeData = getLocaleData();
12+
const locale = localeData?.[ '' ]?.localeVariant || localeData?.[ '' ]?.localeSlug;
1213

1314
if ( ! locale || 'en' === locale ) {
1415
return params;

client/lib/wp/localization/test/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import i18n from 'i18n-calypso';
1+
import { setLocaleData, resetLocaleData } from '@wordpress/i18n';
22
import { addLocaleQueryParam, injectLocalization } from '../';
33

44
describe( 'index', () => {
55
beforeEach( () => {
6-
i18n.configure(); // ensure everything is reset
6+
resetLocaleData(); // ensure everything is reset
77
} );
88

99
describe( '#addLocaleQueryParam()', () => {
@@ -19,19 +19,19 @@ describe( 'index', () => {
1919
} );
2020

2121
test( 'should include the locale query parameter for a non-default locale', () => {
22-
i18n.setLocale( { '': { localeSlug: 'fr' } } );
22+
setLocaleData( { '': { localeSlug: 'fr' } } );
2323
const params = addLocaleQueryParam( { query: 'search=foo' } );
2424
expect( params ).toEqual( { query: 'locale=fr&search=foo' } );
2525
} );
2626

2727
test( 'should include the locale query parameter for a locale variant', () => {
28-
i18n.setLocale( { '': { localeSlug: 'de', localeVariant: 'de_formal' } } );
28+
setLocaleData( { '': { localeSlug: 'de', localeVariant: 'de_formal' } } );
2929
const params = addLocaleQueryParam( { query: 'search=foo' } );
3030
expect( params ).toEqual( { query: 'locale=de_formal&search=foo' } );
3131
} );
3232

3333
test( 'should prioritize the locale specified on the request', () => {
34-
i18n.setLocale( { '': { localeSlug: 'fr' } } );
34+
setLocaleData( { '': { localeSlug: 'fr' } } );
3535
const params = addLocaleQueryParam( { query: 'locale=cs' } );
3636
expect( params ).toEqual( { query: 'locale=cs' } );
3737
} );
@@ -52,7 +52,7 @@ describe( 'index', () => {
5252
} );
5353

5454
test( 'should modify params by default', async () => {
55-
i18n.setLocale( { '': { localeSlug: 'fr' } } );
55+
setLocaleData( { '': { localeSlug: 'fr' } } );
5656
const wpcom = {
5757
async request( params ) {
5858
expect( params.query ).toBe( 'locale=fr&search=foo' );

0 commit comments

Comments
 (0)