Skip to content

Commit

Permalink
Replace i18n-calypso usage in i18n-utils with LocaleProvider (#47570)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsl authored Nov 20, 2020
1 parent 890da56 commit a5f70bb
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 350 deletions.
1 change: 0 additions & 1 deletion packages/i18n-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"dependencies": {
"@wordpress/compose": "^3.19.3",
"i18n-calypso": "^5.0.0",
"react": "^16.12.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { localizeUrl } from './localize-url';
export { localizeUrl, useLocalizeUrl } from './localize-url';
export { LocaleProvider, useLocale, withLocale } from './locale-context';
19 changes: 13 additions & 6 deletions packages/i18n-utils/src/localize-url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* Internal dependencies
*/
import { getLocaleSlug } from 'i18n-calypso';

import { useLocale } from './locale-context';
import {
localesWithBlog,
localesWithPrivacyPolicy,
Expand Down Expand Up @@ -86,10 +85,7 @@ const urlLocalizationMapping: UrlLocalizationMapping = {
'wordpress.com': setLocalizedUrlHost( 'wordpress.com', magnificentNonEnLocales ),
};

export function localizeUrl( fullUrl: string, toLocale?: Locale ): string {
const locale =
toLocale || ( typeof getLocaleSlug === 'function' ? getLocaleSlug() || 'en' : 'en' );

export function localizeUrl( fullUrl: string, locale: Locale ): string {
const url = new URL( String( fullUrl ), INVALID_URL );

// Ignore and passthrough /relative/urls that have no host specified
Expand Down Expand Up @@ -132,3 +128,14 @@ export function localizeUrl( fullUrl: string, toLocale?: Locale ): string {
// Nothing needed to be changed, just return it unmodified.
return fullUrl;
}

export function useLocalizeUrl(): ( fullUrl: string, locale?: Locale ) => string {
const providerLocale = useLocale();

return ( fullUrl: string, locale?: Locale ) => {
if ( locale ) {
return localizeUrl( fullUrl, locale );
}
return localizeUrl( fullUrl, providerLocale );
};
}
280 changes: 280 additions & 0 deletions packages/i18n-utils/src/test/localize-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
/* eslint-disable no-shadow -- shadowing localizeUrl makes tests readable */

/**
* External dependencies
*/
import { renderHook } from '@testing-library/react-hooks';

/**
* Internal dependencies
*/
import { localizeUrl, useLocalizeUrl } from '../';

jest.mock( '../locale-context', () => {
const original = jest.requireActual( '../locale-context' );
return Object.assign( Object.create( Object.getPrototypeOf( original ) ), original, {
useLocale: jest.fn( () => 'en' ),
} );
} );

const { useLocale } = jest.requireMock( '../locale-context' );

describe( '#localizeUrl', () => {
function testLocalizeUrl( locale = 'en' ) {
// Replace the locale given by `useLocale()` with the mocked locale, return a new versio nof localizeUrl
useLocale.mockImplementation( () => locale );
const {
result: { current: localizeUrl },
} = renderHook( () => useLocalizeUrl() ); // eslint-disable-line react-hooks/rules-of-hooks -- being called within renderHook context
return localizeUrl;
}

test( 'should use useLocale for current provider locale as the switch to locale when none is specified', () => {
let localizeUrl;

localizeUrl = testLocalizeUrl( 'pt-br' );
expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual(
'https://br.forums.wordpress.com/'
);
localizeUrl = testLocalizeUrl( 'en' );
expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual(
'https://en.forums.wordpress.com/'
);
} );

test( 'should not change URL for `en`', () => {
[
'https://wordpress.com/',
'https://de.wordpress.com/',
'https://wordpress.com/start/',
'https://wordpress.com/wp-login.php?action=lostpassword',
].forEach( ( fullUrl ) => {
expect( localizeUrl( fullUrl, 'en' ) ).toEqual( fullUrl );
} );
} );

test( 'should not change relative URLs', () => {
[ '/me/account', '/settings' ].forEach( ( fullUrl ) => {
expect( localizeUrl( fullUrl, 'en' ) ).toEqual( fullUrl );
expect( localizeUrl( fullUrl, 'fr' ) ).toEqual( fullUrl );
} );
} );

test( 'handles invalid URLs', () => {
[ undefined, null, [], {}, { href: 'https://test' }, 'not-a-url', () => {} ].forEach(
( fullUrl ) => {
expect( localizeUrl( fullUrl, 'en' ) ).toEqual( fullUrl );
expect( localizeUrl( fullUrl, 'fr' ) ).toEqual( fullUrl );
}
);
} );

test( 'handles double localizeUrl', () => {
expect( localizeUrl( localizeUrl( 'https://automattic.com/cookies/', 'de' ), 'de' ) ).toEqual(
'https://automattic.com/de/cookies/'
);
expect(
localizeUrl(
localizeUrl( 'https://en.support.wordpress.com/all-about-domains/', 'de' ),
'de'
)
).toEqual( 'https://wordpress.com/de/support/all-about-domains/' );

expect( localizeUrl( localizeUrl( 'https://wordpress.com/', 'de' ), 'de' ) ).toEqual(
'https://de.wordpress.com/'
);
expect( localizeUrl( localizeUrl( 'https://en.blog.wordpress.com/', 'de' ), 'de' ) ).toEqual(
'https://wordpress.com/blog/'
);
} );

test( 'trailing slash variations', () => {
expect( localizeUrl( 'https://automattic.com/cookies/', 'de' ) ).toEqual(
'https://automattic.com/de/cookies/'
);
expect( localizeUrl( 'https://automattic.com/cookies', 'de' ) ).toEqual(
'https://automattic.com/de/cookies/'
);
} );

test( 'logged-out homepage', () => {
expect( localizeUrl( 'https://wordpress.com/', 'en' ) ).toEqual( 'https://wordpress.com/' );
expect( localizeUrl( 'https://wordpress.com/', 'de' ) ).toEqual( 'https://de.wordpress.com/' );
expect( localizeUrl( 'https://wordpress.com/', 'pt-br' ) ).toEqual(
'https://br.wordpress.com/'
);
expect( localizeUrl( 'https://wordpress.com/', 'pl' ) ).toEqual( 'https://wordpress.com/' );

expect( localizeUrl( 'https://en.wordpress.com/', 'en' ) ).toEqual( 'https://wordpress.com/' );
} );

test( 'blog url', () => {
expect( localizeUrl( 'https://en.blog.wordpress.com/', 'en' ) ).toEqual(
'https://wordpress.com/blog/'
);
expect( localizeUrl( 'https://en.blog.wordpress.com/', 'de' ) ).toEqual(
'https://wordpress.com/blog/'
);
expect( localizeUrl( 'https://en.blog.wordpress.com/', 'pt-br' ) ).toEqual(
'https://wordpress.com/br/blog/'
);
expect( localizeUrl( 'https://en.blog.wordpress.com/', 'pl' ) ).toEqual(
'https://wordpress.com/blog/'
);
// Don't rewrite specific blog posts
expect( localizeUrl( 'https://en.blog.wordpress.com/2020/01/01/test/', 'pt-br' ) ).toEqual(
'https://wordpress.com/blog/2020/01/01/test/'
);
expect( localizeUrl( 'https://wordpress.com/blog/2020/01/01/test/', 'pt-br' ) ).toEqual(
'https://wordpress.com/blog/2020/01/01/test/'
);
} );

test( 'support url', () => {
expect( localizeUrl( 'https://en.support.wordpress.com/', 'en' ) ).toEqual(
'https://wordpress.com/support/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/', 'de' ) ).toEqual(
'https://wordpress.com/de/support/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/', 'pt-br' ) ).toEqual(
'https://wordpress.com/br/support/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/', 'pl' ) ).toEqual(
'https://wordpress.com/support/'
);

expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'en' ) ).toEqual(
'https://wordpress.com/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'de' ) ).toEqual(
'https://wordpress.com/de/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'pt-br' ) ).toEqual(
'https://wordpress.com/br/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'pl' ) ).toEqual(
'https://wordpress.com/support/path/'
);

expect( localizeUrl( 'https://en.support.wordpress.com/', 'en' ) ).toEqual(
'https://wordpress.com/support/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/', 'de' ) ).toEqual(
'https://wordpress.com/de/support/'
);

expect( localizeUrl( 'https://en.support.wordpress.com/', 'pt-br' ) ).toEqual(
'https://wordpress.com/br/support/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/', 'pl' ) ).toEqual(
'https://wordpress.com/support/'
);

expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'en' ) ).toEqual(
'https://wordpress.com/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'de' ) ).toEqual(
'https://wordpress.com/de/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'pt-br' ) ).toEqual(
'https://wordpress.com/br/support/path/'
);
expect( localizeUrl( 'https://en.support.wordpress.com/path/', 'pl' ) ).toEqual(
'https://wordpress.com/support/path/'
);
} );

test( 'forums url', () => {
expect( localizeUrl( 'https://en.forums.wordpress.com/', 'en' ) ).toEqual(
'https://en.forums.wordpress.com/'
);
expect( localizeUrl( 'https://en.forums.wordpress.com/', 'de' ) ).toEqual(
'https://de.forums.wordpress.com/'
);
expect( localizeUrl( 'https://en.forums.wordpress.com/', 'pt-br' ) ).toEqual(
'https://br.forums.wordpress.com/'
);
expect( localizeUrl( 'https://en.forums.wordpress.com/', 'th' ) ).toEqual(
'https://th.forums.wordpress.com/'
);
expect( localizeUrl( 'https://en.forums.wordpress.com/', 'pl' ) ).toEqual(
'https://en.forums.wordpress.com/'
);
} );

test( 'privacy policy', () => {
expect( localizeUrl( 'https://automattic.com/privacy/', 'en' ) ).toEqual(
'https://automattic.com/privacy/'
);
expect( localizeUrl( 'https://automattic.com/privacy/', 'de' ) ).toEqual(
'https://automattic.com/de/privacy/'
);
expect( localizeUrl( 'https://automattic.com/privacy/', 'pl' ) ).toEqual(
'https://automattic.com/privacy/'
);
} );

test( 'cookie policy', () => {
expect( localizeUrl( 'https://automattic.com/cookies/', 'en' ) ).toEqual(
'https://automattic.com/cookies/'
);
expect( localizeUrl( 'https://automattic.com/cookies/', 'de' ) ).toEqual(
'https://automattic.com/de/cookies/'
);
expect( localizeUrl( 'https://automattic.com/cookies/', 'pl' ) ).toEqual(
'https://automattic.com/cookies/'
);
} );

test( 'tos', () => {
expect( localizeUrl( 'https://wordpress.com/tos/', 'en' ) ).toEqual(
'https://wordpress.com/tos/'
);
expect( localizeUrl( 'https://wordpress.com/tos/', 'de' ) ).toEqual(
'https://de.wordpress.com/tos/'
);
expect( localizeUrl( 'https://wordpress.com/tos/', 'pl' ) ).toEqual(
'https://wordpress.com/tos/'
);
expect( localizeUrl( 'https://wordpress.com/tos/', 'th' ) ).toEqual(
'https://wordpress.com/tos/'
);
} );

test( 'jetpack', () => {
expect( localizeUrl( 'https://jetpack.com/features/comparison/', 'en' ) ).toEqual(
'https://jetpack.com/features/comparison/'
);
expect( localizeUrl( 'https://jetpack.com/features/comparison/', 'de' ) ).toEqual(
'https://de.jetpack.com/features/comparison/'
);
expect( localizeUrl( 'https://jetpack.com/features/comparison/', 'pt-br' ) ).toEqual(
'https://br.jetpack.com/features/comparison/'
);
expect( localizeUrl( 'https://jetpack.com/features/comparison/', 'zh-tw' ) ).toEqual(
'https://zh-tw.jetpack.com/features/comparison/'
);
expect( localizeUrl( 'https://jetpack.com/features/comparison/', 'pl' ) ).toEqual(
'https://jetpack.com/features/comparison/'
);
} );

test( 'WordPress.com URLs', () => {
expect( localizeUrl( 'https://wordpress.com/wp-login.php?action=lostpassword', 'en' ) ).toEqual(
'https://wordpress.com/wp-login.php?action=lostpassword'
);
expect( localizeUrl( 'https://wordpress.com/wp-login.php?action=lostpassword', 'de' ) ).toEqual(
'https://de.wordpress.com/wp-login.php?action=lostpassword'
);
} );

test( 'WordPress.com new style support URLs', () => {
expect( localizeUrl( 'https://wordpress.com/support/reader/', 'de' ) ).toEqual(
'https://wordpress.com/de/support/reader/'
);
expect( localizeUrl( 'https://wordpress.com/support/reader/#blocking-sites', 'de' ) ).toEqual(
'https://wordpress.com/de/support/reader/#blocking-sites'
);
} );
} );
Loading

0 comments on commit a5f70bb

Please sign in to comment.