如果用户更改区域设置,请重新加载 "Sign In With Google" 按钮
Reload the "Sign In With Google" button if user changes locale
我在 JavaScript 中找不到任何记录的方法来重新加载新的“使用 Google 登录”按钮。
我必须删除脚本标签和“按钮”div,然后重新添加它们。
有谁知道更好的方法吗?
你看过 JS renderButton
方法了吗?
假设您有类似这样的东西来初始化库并在 JS 中显示按钮,您可以将第二个参数中的语言环境更新为 renderButton 并再次调用该方法以切换语言。
<html>
<body>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
function handleCredentialResponse(response) {
console.log("Encoded JWT ID token: " + response.credential);
}
window.onload = function () {
google.accounts.id.initialize({
client_id: "YOUR_GOOGLE_CLIENT_ID",
callback: handleCredentialResponse
});
google.accounts.id.renderButton(
document.getElementById("buttonDiv"),
{ theme: "outline", size: "large", locale: "the new locale" }
);
google.accounts.id.prompt(); // also display the One Tap dialog
}
</script>
<div id="buttonDiv"></div>
</div>
</body>
</html>
显然,您会在上面的 window.onload 示例之外再次调用 renderButton
,不过我并没有在代码示例中展示这一点。
我在 JavaScript 中找不到任何记录的方法来重新加载新的“使用 Google 登录”按钮。
我必须删除脚本标签和“按钮”div,然后重新添加它们。
有谁知道更好的方法吗?
你看过 JS renderButton
方法了吗?
假设您有类似这样的东西来初始化库并在 JS 中显示按钮,您可以将第二个参数中的语言环境更新为 renderButton 并再次调用该方法以切换语言。
<html>
<body>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
function handleCredentialResponse(response) {
console.log("Encoded JWT ID token: " + response.credential);
}
window.onload = function () {
google.accounts.id.initialize({
client_id: "YOUR_GOOGLE_CLIENT_ID",
callback: handleCredentialResponse
});
google.accounts.id.renderButton(
document.getElementById("buttonDiv"),
{ theme: "outline", size: "large", locale: "the new locale" }
);
google.accounts.id.prompt(); // also display the One Tap dialog
}
</script>
<div id="buttonDiv"></div>
</div>
</body>
</html>
显然,您会在上面的 window.onload 示例之外再次调用 renderButton
,不过我并没有在代码示例中展示这一点。