Skip to content

Commit 38722bb

Browse files
fix: include hl directive into script tag if locale is passed (#370)
Co-authored-by: Mo'men Sherif <[email protected]>
1 parent 102e060 commit 38722bb

File tree

10 files changed

+16793
-11911
lines changed

10 files changed

+16793
-11911
lines changed

.changeset/spicy-socks-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-oauth/google': patch
3+
---
4+
5+
fix locale parameter to gsi script

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ yarn-debug.log*
2222
yarn-error.log*
2323

2424
.turbo
25+
26+
.yarn/install-state.gz
27+
.yarn/unplugged/
28+
.yarn/build-state.yml
29+
.yarn/cache/

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@
3737
"commitizen": {
3838
"path": "cz-conventional-changelog"
3939
}
40-
}
40+
},
41+
"packageManager": "[email protected]+sha512.5383cc12567a95f1d668fbe762dfe0075c595b4bfff433be478dbbe24e05251a8e8c3eb992a986667c1d53b6c3a9c85b8398c35a960587fbd9fa3a0915406728"
4142
}

packages/@react-oauth/google/src/GoogleLogin.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export default function GoogleLogin({
3333
shape,
3434
logo_alignment,
3535
width,
36-
locale,
3736
click_listener,
3837
state,
3938
containerProps,
4039
...props
4140
}: GoogleLoginProps) {
4241
const btnContainerRef = useRef<HTMLDivElement>(null);
43-
const { clientId, scriptLoadedSuccessfully } = useGoogleOAuth();
42+
const { clientId, locale, scriptLoadedSuccessfully } = useGoogleOAuth();
4443

4544
const onSuccessRef = useRef(onSuccess);
4645
onSuccessRef.current = onSuccess;

packages/@react-oauth/google/src/GoogleOAuthProvider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import useLoadGsiScript, {
66

77
interface GoogleOAuthContextProps {
88
clientId: string;
9+
locale?: string;
910
scriptLoadedSuccessfully: boolean;
1011
}
1112

@@ -19,6 +20,7 @@ interface GoogleOAuthProviderProps extends UseLoadGsiScriptOptions {
1920
export default function GoogleOAuthProvider({
2021
clientId,
2122
nonce,
23+
locale,
2224
onScriptLoadSuccess,
2325
onScriptLoadError,
2426
children,
@@ -27,10 +29,12 @@ export default function GoogleOAuthProvider({
2729
nonce,
2830
onScriptLoadSuccess,
2931
onScriptLoadError,
32+
locale,
3033
});
3134

3235
const contextValue = useMemo(
3336
() => ({
37+
locale,
3438
clientId,
3539
scriptLoadedSuccessfully,
3640
}),

packages/@react-oauth/google/src/google-auth-window.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare global {
1616
prompt: (momentListener?: MomentListener) => void;
1717
renderButton: (
1818
parent: HTMLElement,
19-
options: GsiButtonConfiguration,
19+
options: GsiButtonConfiguration & { locale?: string },
2020
) => void;
2121
disableAutoSelect: () => void;
2222
storeCredential: (

packages/@react-oauth/google/src/hooks/useLoadGsiScript.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export interface UseLoadGsiScriptOptions {
55
* Nonce applied to GSI script tag. Propagates to GSI's inline style tag
66
*/
77
nonce?: string;
8+
/**
9+
* ISO-639 code of [locale](https://developers.google.com/identity/gsi/web/reference/js-reference#locale) to use for content
10+
*/
11+
locale?: string;
812
/**
913
* Callback fires on load [gsi](https://accounts.google.com/gsi/client) script success
1014
*/
@@ -18,7 +22,7 @@ export interface UseLoadGsiScriptOptions {
1822
export default function useLoadGsiScript(
1923
options: UseLoadGsiScriptOptions = {},
2024
): boolean {
21-
const { nonce, onScriptLoadSuccess, onScriptLoadError } = options;
25+
const { nonce, locale, onScriptLoadSuccess, onScriptLoadError } = options;
2226

2327
const [scriptLoadedSuccessfully, setScriptLoadedSuccessfully] =
2428
useState(false);
@@ -32,6 +36,7 @@ export default function useLoadGsiScript(
3236
useEffect(() => {
3337
const scriptTag = document.createElement('script');
3438
scriptTag.src = 'https://accounts.google.com/gsi/client';
39+
if (locale) scriptTag.src += `?hl=${locale}`;
3540
scriptTag.async = true;
3641
scriptTag.defer = true;
3742
scriptTag.nonce = nonce;

packages/@react-oauth/google/src/types/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ export interface GsiButtonConfiguration {
9696
logo_alignment?: 'left' | 'center';
9797
/** The button [width](https://developers.google.com/identity/gsi/web/reference/js-reference#width), in pixels */
9898
width?: string | number;
99-
/** If set, then the button [language](https://developers.google.com/identity/gsi/web/reference/js-reference#locale) is rendered */
100-
locale?: string;
10199
/** If set, this [function](https://developers.google.com/identity/gsi/web/reference/js-reference#click_listener) will be called when the Sign in with Google button is clicked. */
102100
click_listener?: () => void;
103101
/** If set, this [string](https://developers.google.com/identity/gsi/web/reference/js-reference#state) returns with the ID token. */

0 commit comments

Comments
 (0)