Skip to content

fix: include hl directive into script tag if locale is passed #370

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/@react-oauth/google/src/GoogleLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ export default function GoogleLogin({
shape,
logo_alignment,
width,
locale,
click_listener,
containerProps,
...props
}: GoogleLoginProps) {
const btnContainerRef = useRef<HTMLDivElement>(null);
const { clientId, scriptLoadedSuccessfully } = useGoogleOAuth();
const { clientId, locale, scriptLoadedSuccessfully } = useGoogleOAuth();

const onSuccessRef = useRef(onSuccess);
onSuccessRef.current = onSuccess;
Expand Down
4 changes: 4 additions & 0 deletions packages/@react-oauth/google/src/GoogleOAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useLoadGsiScript, {

interface GoogleOAuthContextProps {
clientId: string;
locale?: string;
scriptLoadedSuccessfully: boolean;
}

Expand All @@ -19,6 +20,7 @@ interface GoogleOAuthProviderProps extends UseLoadGsiScriptOptions {
export default function GoogleOAuthProvider({
clientId,
nonce,
locale,
onScriptLoadSuccess,
onScriptLoadError,
children,
Expand All @@ -27,10 +29,12 @@ export default function GoogleOAuthProvider({
nonce,
onScriptLoadSuccess,
onScriptLoadError,
locale,
});

const contextValue = useMemo(
() => ({
locale,
clientId,
scriptLoadedSuccessfully,
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-oauth/google/src/google-auth-window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare global {
prompt: (momentListener?: MomentListener) => void;
renderButton: (
parent: HTMLElement,
options: GsiButtonConfiguration,
options: GsiButtonConfiguration & { locale?: string },
) => void;
disableAutoSelect: () => void;
storeCredential: (
Expand Down
7 changes: 6 additions & 1 deletion packages/@react-oauth/google/src/hooks/useLoadGsiScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface UseLoadGsiScriptOptions {
* Nonce applied to GSI script tag. Propagates to GSI's inline style tag
*/
nonce?: string;
/**
* ISO-639 code of [locale](https://developers.google.com/identity/gsi/web/reference/js-reference#locale) to use for content
*/
locale?: string;
/**
* Callback fires on load [gsi](https://accounts.google.com/gsi/client) script success
*/
Expand All @@ -18,7 +22,7 @@ export interface UseLoadGsiScriptOptions {
export default function useLoadGsiScript(
options: UseLoadGsiScriptOptions = {},
): boolean {
const { nonce, onScriptLoadSuccess, onScriptLoadError } = options;
const { nonce, locale, onScriptLoadSuccess, onScriptLoadError } = options;

const [scriptLoadedSuccessfully, setScriptLoadedSuccessfully] =
useState(false);
Expand All @@ -32,6 +36,7 @@ export default function useLoadGsiScript(
useEffect(() => {
const scriptTag = document.createElement('script');
scriptTag.src = 'https://accounts.google.com/gsi/client';
if (locale) scriptTag.src += `?hl=${locale}`;
scriptTag.async = true;
scriptTag.defer = true;
scriptTag.nonce = nonce;
Expand Down
2 changes: 0 additions & 2 deletions packages/@react-oauth/google/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export interface GsiButtonConfiguration {
logo_alignment?: 'left' | 'center';
/** The button [width](https://developers.google.com/identity/gsi/web/reference/js-reference#width), in pixels */
width?: string | number;
/** If set, then the button [language](https://developers.google.com/identity/gsi/web/reference/js-reference#locale) is rendered */
locale?: string;
/** If set, this [function](https://developers.google.com/identity/gsi/web/reference/js-reference#click_listener) will be called when the # with Google button is clicked. */
click_listener?: () => void;
}
Expand Down