-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update/i18n utils unreverted #47492
Closed
Closed
Update/i18n utils unreverted #47492
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bb931b2
Add locale to i18n-utils so it can be used and set in React components
p-jackson 5bcd83d
remove calypso
lsl d051237
fix tests ~
lsl 4069986
wrap the new localizeUrl function
lsl 6752212
Use LocaleProvider in calypso
lsl 3832b59
more test fixes
lsl 1ab6219
Missed export for localizeUrl underlying
lsl d9056d2
Pivot to useLocalizeUrl from useI18nUtils
lsl b5a7725
Don't lie about types
lsl cf74ca3
Update client/components/calypso-i18n-provider/index.tsx
lsl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export { localizeUrl } from './localize-url'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
export { localizeUrl, useLocalizeUrl } from './localize-url'; | ||
export { LocaleProvider, useLocale } from './locale-context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { createContext, useContext } from 'react'; | ||
|
||
export const LocaleContext = createContext< string >( 'en' ); | ||
|
||
interface Props { | ||
localeSlug: string; | ||
} | ||
|
||
export const LocaleProvider: React.FC< Props > = ( { children, localeSlug } ) => ( | ||
<LocaleContext.Provider value={ localeSlug }>{ children }</LocaleContext.Provider> | ||
); | ||
|
||
export function useLocale(): string { | ||
return useContext( LocaleContext ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getLocaleSlug } from 'i18n-calypso'; | ||
|
||
import { | ||
localesWithBlog, | ||
localesWithPrivacyPolicy, | ||
|
@@ -14,6 +12,7 @@ import { | |
jetpackComLocales, | ||
Locale, | ||
} from './locales'; | ||
import { useLocale } from './locale-context'; | ||
|
||
const INVALID_URL = `http://__domain__.invalid`; | ||
|
||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
const providerLocale = useLocale(); | ||
|
||
return ( fullUrl: string, locale?: Locale ) => { | ||
lsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( locale ) { | ||
return localizeUrl( fullUrl, locale ); | ||
} | ||
return localizeUrl( fullUrl, providerLocale ); | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use
config( 'i18n_default_locale_slug' )
for fallback instead of'en'
string literal.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, TIL! There's some other places I can use this config value too 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exposed and used the hard coded one in i18n-utils, I think we need to strip these i18n config values from calypso usage and rely on the utils package unless we want to configure LocaleProvider with the configuration as well?
This is done in #47613