Skip to content

Commit

Permalink
Bring in changes from #47492
Browse files Browse the repository at this point in the history
  • Loading branch information
lsl committed Nov 19, 2020
1 parent 3471fc3 commit e80c69d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
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 );
};
}
File renamed without changes.

0 comments on commit e80c69d

Please sign in to comment.