diff --git a/packages/i18n-utils/package.json b/packages/i18n-utils/package.json index e856cbde82520..cf17249536e49 100644 --- a/packages/i18n-utils/package.json +++ b/packages/i18n-utils/package.json @@ -24,7 +24,6 @@ }, "dependencies": { "@wordpress/compose": "^3.19.3", - "i18n-calypso": "^5.0.0", "react": "^16.12.0" }, "devDependencies": { diff --git a/packages/i18n-utils/src/index.ts b/packages/i18n-utils/src/index.ts index f8f6bf4d2ceb9..8a133104a794c 100644 --- a/packages/i18n-utils/src/index.ts +++ b/packages/i18n-utils/src/index.ts @@ -1,2 +1,2 @@ -export { localizeUrl } from './localize-url'; +export { localizeUrl, useLocalizeUrl } from './localize-url'; export { LocaleProvider, useLocale, withLocale } from './locale-context'; diff --git a/packages/i18n-utils/src/localize-url.ts b/packages/i18n-utils/src/localize-url.ts index 566bc87547540..3bccaad364b43 100644 --- a/packages/i18n-utils/src/localize-url.ts +++ b/packages/i18n-utils/src/localize-url.ts @@ -1,8 +1,7 @@ /** * Internal dependencies */ -import { getLocaleSlug } from 'i18n-calypso'; - +import { useLocale } from './locale-context'; import { localesWithBlog, localesWithPrivacyPolicy, @@ -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 @@ -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 ); + }; +} diff --git a/packages/i18n-utils/src/test/localize-url.js b/packages/i18n-utils/src/test/localize-url.js new file mode 100644 index 0000000000000..2bcdedfae3144 --- /dev/null +++ b/packages/i18n-utils/src/test/localize-url.js @@ -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' + ); + } ); +} ); diff --git a/packages/i18n-utils/test/localize-url.js b/packages/i18n-utils/test/localize-url.js deleted file mode 100644 index a4b097687376b..0000000000000 --- a/packages/i18n-utils/test/localize-url.js +++ /dev/null @@ -1,342 +0,0 @@ -/** - * Internal dependencies - */ -import { localizeUrl } from '../src'; - -// Mock only the getLocaleSlug function from i18n-calypso, and use -// original references for all the other functions -function mockFunctions() { - const original = jest.requireActual( 'i18n-calypso' ).default; - return Object.assign( Object.create( Object.getPrototypeOf( original ) ), original, { - getLocaleSlug: jest.fn( () => 'en' ), - } ); -} -jest.mock( 'i18n-calypso', () => mockFunctions() ); -const { getLocaleSlug } = jest.requireMock( 'i18n-calypso' ); - -describe( '#localizeUrl', () => { - 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 ) => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( fullUrl ) ).toEqual( fullUrl ); - } ); - } ); - - test( 'should not change relative URLs', () => { - [ '/me/account', '/settings' ].forEach( ( fullUrl ) => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( fullUrl ) ).toEqual( fullUrl ); - getLocaleSlug.mockImplementationOnce( () => 'fr' ); - expect( localizeUrl( fullUrl ) ).toEqual( fullUrl ); - } ); - } ); - - test( 'handles invalid URLs', () => { - [ undefined, null, [], {}, { href: 'https://test' }, 'not-a-url', () => {} ].forEach( - ( fullUrl ) => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( fullUrl ) ).toEqual( fullUrl ); - getLocaleSlug(); // make sure to consume it. - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( fullUrl, 'fr' ) ).toEqual( fullUrl ); - getLocaleSlug(); // make sure to consume it. - getLocaleSlug.mockImplementationOnce( () => 'fr' ); - expect( localizeUrl( fullUrl ) ).toEqual( fullUrl ); - getLocaleSlug(); // make sure to consume it. - } - ); - } ); - - test( 'handles double localizeUrl', () => { - getLocaleSlug.mockImplementationOnce( () => 'de' ).mockImplementationOnce( () => 'de' ); - expect( localizeUrl( localizeUrl( 'https://automattic.com/cookies/' ) ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - getLocaleSlug(); - getLocaleSlug(); // make sure to consume it. - - getLocaleSlug.mockImplementationOnce( () => 'de' ).mockImplementationOnce( () => 'de' ); - expect( - localizeUrl( localizeUrl( 'https://en.support.wordpress.com/all-about-domains/' ) ) - ).toEqual( 'https://wordpress.com/de/support/all-about-domains/' ); - getLocaleSlug(); - getLocaleSlug(); // make sure to consume it. - - getLocaleSlug.mockImplementationOnce( () => 'de' ).mockImplementationOnce( () => 'de' ); - expect( localizeUrl( localizeUrl( 'https://wordpress.com/' ) ) ).toEqual( - 'https://de.wordpress.com/' - ); - getLocaleSlug(); - getLocaleSlug(); // make sure to consume it. - - getLocaleSlug.mockImplementationOnce( () => 'de' ).mockImplementationOnce( () => 'de' ); - expect( localizeUrl( localizeUrl( 'https://en.blog.wordpress.com/' ) ) ).toEqual( - 'https://wordpress.com/blog/' - ); - getLocaleSlug(); - getLocaleSlug(); // make sure to consume it. - } ); - - test( 'trailing slash variations', () => { - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://automattic.com/cookies' ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - } ); - - test( 'overriding locale', () => { - getLocaleSlug.mockImplementationOnce( () => 'ru' ); - expect( localizeUrl( 'https://automattic.com/cookies/', 'de' ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - getLocaleSlug(); // make sure to consume it. - - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://automattic.com/cookies', 'fr' ) ).toEqual( - 'https://automattic.com/fr/cookies/' - ); - getLocaleSlug(); // make sure to consume it. - - // Finally make sure that no overriding has stuck and it uses the getLocaleSlug() when no override is specified. - getLocaleSlug.mockImplementationOnce( () => 'ru' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/cookies/' - ); - getLocaleSlug(); // make sure to consume it. - - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - getLocaleSlug(); // make sure to consume it. - } ); - - test( 'logged-out homepage', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://wordpress.com/' ) ).toEqual( 'https://wordpress.com/' ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://wordpress.com/' ) ).toEqual( 'https://de.wordpress.com/' ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://wordpress.com/' ) ).toEqual( 'https://br.wordpress.com/' ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://wordpress.com/' ) ).toEqual( 'https://wordpress.com/' ); - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.wordpress.com/' ) ).toEqual( 'https://wordpress.com/' ); - } ); - - test( 'blog url', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.blog.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/blog/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.blog.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/blog/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.blog.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/br/blog/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.blog.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/blog/' - ); - // Don't rewrite specific blog posts - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.blog.wordpress.com/2020/01/01/test/' ) ).toEqual( - 'https://wordpress.com/blog/2020/01/01/test/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://wordpress.com/blog/2020/01/01/test/' ) ).toEqual( - 'https://wordpress.com/blog/2020/01/01/test/' - ); - } ); - - test( 'support url', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/de/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/br/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/support/' - ); - - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/de/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/br/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/support/path/' - ); - - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/de/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/br/support/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual( - 'https://wordpress.com/support/' - ); - - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/de/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/br/support/path/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.support.wordpress.com/path/' ) ).toEqual( - 'https://wordpress.com/support/path/' - ); - } ); - - test( 'forums url', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual( - 'https://en.forums.wordpress.com/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual( - 'https://de.forums.wordpress.com/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual( - 'https://br.forums.wordpress.com/' - ); - getLocaleSlug.mockImplementationOnce( () => 'th' ); - expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual( - 'https://th.forums.wordpress.com/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://en.forums.wordpress.com/' ) ).toEqual( - 'https://en.forums.wordpress.com/' - ); - } ); - - test( 'privacy policy', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://automattic.com/privacy/' ) ).toEqual( - 'https://automattic.com/privacy/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://automattic.com/privacy/' ) ).toEqual( - 'https://automattic.com/de/privacy/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://automattic.com/privacy/' ) ).toEqual( - 'https://automattic.com/privacy/' - ); - } ); - - test( 'cookie policy', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/cookies/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/de/cookies/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://automattic.com/cookies/' ) ).toEqual( - 'https://automattic.com/cookies/' - ); - } ); - - test( 'tos', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://wordpress.com/tos/' ) ).toEqual( 'https://wordpress.com/tos/' ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://wordpress.com/tos/' ) ).toEqual( - 'https://de.wordpress.com/tos/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://wordpress.com/tos/' ) ).toEqual( 'https://wordpress.com/tos/' ); - getLocaleSlug.mockImplementationOnce( () => 'th' ); - expect( localizeUrl( 'https://wordpress.com/tos/' ) ).toEqual( 'https://wordpress.com/tos/' ); - } ); - - test( 'jetpack', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://jetpack.com/features/comparison/' ) ).toEqual( - 'https://jetpack.com/features/comparison/' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://jetpack.com/features/comparison/' ) ).toEqual( - 'https://de.jetpack.com/features/comparison/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pt-br' ); - expect( localizeUrl( 'https://jetpack.com/features/comparison/' ) ).toEqual( - 'https://br.jetpack.com/features/comparison/' - ); - getLocaleSlug.mockImplementationOnce( () => 'zh-tw' ); - expect( localizeUrl( 'https://jetpack.com/features/comparison/' ) ).toEqual( - 'https://zh-tw.jetpack.com/features/comparison/' - ); - getLocaleSlug.mockImplementationOnce( () => 'pl' ); - expect( localizeUrl( 'https://jetpack.com/features/comparison/' ) ).toEqual( - 'https://jetpack.com/features/comparison/' - ); - } ); - - test( 'WordPress.com URLs', () => { - getLocaleSlug.mockImplementationOnce( () => 'en' ); - expect( localizeUrl( 'https://wordpress.com/wp-login.php?action=lostpassword' ) ).toEqual( - 'https://wordpress.com/wp-login.php?action=lostpassword' - ); - getLocaleSlug.mockImplementationOnce( () => 'de' ); - expect( localizeUrl( 'https://wordpress.com/wp-login.php?action=lostpassword' ) ).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' - ); - } ); -} );