Skip to content

Commit 363f187

Browse files
Googlerbojeil-google
authored andcommitted
[firebase-release] Updated FirebaseUI for web to 2.4.1.
- Passes the language code to Firebase Auth instance to fully localize firebase-ui web. PiperOrigin-RevId: 172029023 Change-Id: I7de921fd4079feb736c0ca41b87f23431bcb7a32
1 parent 79daa62 commit 363f187

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

gulpfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function getLocaleForFileName(locale) {
129129
* Gets the path to the temporary JS file that contains all FirebaseUI code
130130
* but no external dependencies.
131131
* @param {string} locale
132+
* @return {string} The path of the temporary JS file.
132133
*/
133134
function getTmpJsPath(locale) {
134135
const localeForFileName = getLocaleForFileName(locale);
@@ -163,6 +164,7 @@ function repeatTaskForAllLocales(taskName, dependencies, operation) {
163164
/**
164165
* Builds the core FirebaseUI binary in the given locale.
165166
* @param {string} locale
167+
* @return {*} A stream that finishes when compilation finishes.
166168
*/
167169
function buildFirebaseUiJs(locale) {
168170
const flags = {
@@ -225,6 +227,7 @@ const buildJsTasks = repeatTaskForAllLocales(
225227
* Creates the default FirebaseUI binaries for basic usage without
226228
* localization. For example, it copies firebaseui__en.js to firebaseui.js.
227229
* @param {string} fileName
230+
* @return {!Promise} A promise that resolves on completion.
228231
*/
229232
function makeDefaultFile(fileName) {
230233
const localeForFileName = getLocaleForFileName(DEFAULT_LOCALE);

javascript/utils/util.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,11 @@ firebaseui.auth.util.getElement = function(element, opt_notFoundDesc) {
193193
firebaseui.auth.util.getCurrentUrl = function() {
194194
return window.location.href;
195195
};
196+
197+
198+
/**
199+
* @return {string} The country code in canonical Unicode format.
200+
*/
201+
firebaseui.auth.util.getUnicodeLocale = function() {
202+
return goog.LOCALE.replace(/_/g, '-');
203+
};

javascript/utils/util_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,16 @@ function testIsHttpOrHttps() {
9999
});
100100
assertFalse(firebaseui.auth.util.isHttpOrHttps());
101101
}
102+
103+
function testGetUnicodeLocale() {
104+
stubs.replace(goog, 'LOCALE', 'de');
105+
assertEquals('de', firebaseui.auth.util.getUnicodeLocale());
106+
107+
stubs.replace(goog, 'LOCALE', 'zh-CN');
108+
assertEquals('zh-CN', firebaseui.auth.util.getUnicodeLocale());
109+
110+
// The locale should have a dash instead of an underscore.
111+
stubs.replace(goog, 'LOCALE', 'zh_CN');
112+
assertEquals('zh-CN', firebaseui.auth.util.getUnicodeLocale());
113+
114+
}

javascript/widgets/authui.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ firebaseui.auth.AuthUI = function(auth, opt_appId) {
8686
}
8787
/** @private {!firebase.auth.Auth} The Firebase Auth instance. */
8888
this.auth_ = auth;
89+
/** @private {?string} The original Auth language code. */
90+
this.originalAuthLanguageCode_ = null;
8991
// Log FirebaseUI on external Auth instance.
9092
firebaseui.auth.AuthUI.logFirebaseUI_(this.auth_);
9193
var tempApp = firebase.initializeApp({
@@ -327,6 +329,16 @@ firebaseui.auth.AuthUI.prototype.start = function(element, config) {
327329
this.checkIfDestroyed_();
328330
var self = this;
329331

332+
// Save the original language code of external Auth instance.
333+
if (typeof this.auth_.languageCode !== 'undefined') {
334+
this.originalAuthLanguageCode_ = this.auth_.languageCode;
335+
}
336+
// Make sure the locale uses hyphens instead of underscores.
337+
var unicodeLocale = firebaseui.auth.util.getUnicodeLocale();
338+
// Sync the language code of Auth instance with widget.
339+
this.auth_.languageCode = unicodeLocale;
340+
this.tempAuth_.languageCode = unicodeLocale;
341+
330342
// There is a problem when config in second call modifies accountchooser.com
331343
// related config. eg. acUiConfig
332344
// These changes will be ignored as only the first accountchooser.com related
@@ -360,9 +372,8 @@ firebaseui.auth.AuthUI.prototype.initElement_ = function(element) {
360372
// Set the "lang" attribute; without this, there are subtle rendering errors
361373
// like vowel capitalization in Turkish.
362374

363-
// Make sure the locale uses hyphens instead of strings.
364-
var locale = goog.LOCALE.replace(/_/g, '-');
365-
container.setAttribute('lang', locale);
375+
// Make sure the locale uses hyphens instead of underscores.
376+
container.setAttribute('lang', firebaseui.auth.util.getUnicodeLocale());
366377

367378
// Only one auth instance can be rendered per page. This is because
368379
// accountchooser.com callbacks are set once to the AuthUI instance that
@@ -434,6 +445,10 @@ firebaseui.auth.AuthUI.prototype.reset = function() {
434445
if (this.widgetElement_) {
435446
this.widgetElement_.removeAttribute('lang');
436447
}
448+
// Change back the languageCode of external Auth instance.
449+
if (typeof this.auth_.languageCode !== 'undefined') {
450+
this.auth_.languageCode = this.originalAuthLanguageCode_;
451+
}
437452

438453
// After reset, if the sign-in widget callback is called again, it should not
439454
// resolve with the previous redirect result.

javascript/widgets/authui_test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,27 +479,62 @@ function testStart() {
479479

480480

481481
function testSetLang() {
482-
createAndInstallTestInstances();
483482
testStubs.replace(goog, 'LOCALE', 'de');
483+
// Language code of auth instance is set to goog.LOCALE at initialization.
484+
// Replace goog.LOCALE and then install instance.
485+
createAndInstallTestInstances();
484486
app1.start(container1, config1);
485487
assertEquals('de', container1.getAttribute('lang'));
488+
assertEquals('de', app1.getAuth().languageCode);
489+
assertEquals('de', app1.getExternalAuth().languageCode);
486490
app1.reset();
487491
assertFalse(container1.hasAttribute('lang'));
492+
}
488493

494+
function testSetLang_codeWithdash() {
489495
testStubs.replace(goog, 'LOCALE', 'zh-CN');
496+
// Language code of auth instance is set to goog.LOCALE at initialization.
497+
// Replace goog.LOCALE and then install instance.
498+
createAndInstallTestInstances();
490499
app1.start(container1, config1);
491500
assertEquals('zh-CN', container1.getAttribute('lang'));
501+
assertEquals('zh-CN', app1.getAuth().languageCode);
502+
assertEquals('zh-CN', app1.getExternalAuth().languageCode);
492503
app1.reset();
493504
assertFalse(container1.hasAttribute('lang'));
505+
}
494506

507+
function testSetLang_codeWithUnderscore() {
495508
testStubs.replace(goog, 'LOCALE', 'zh_CN');
509+
// Language code of auth instance is set to goog.LOCALE at initialization.
510+
// Replace goog.LOCALE and then install instance.
511+
createAndInstallTestInstances();
496512
app1.start(container1, config1);
497513
// The lang should have a dash instead of an underscore.
498514
assertEquals('zh-CN', container1.getAttribute('lang'));
515+
assertEquals('zh-CN', app1.getAuth().languageCode);
516+
assertEquals('zh-CN', app1.getExternalAuth().languageCode);
499517
app1.reset();
500518
assertFalse(container1.hasAttribute('lang'));
501519
}
502520

521+
function testStart_overrideLanguageCode() {
522+
// Set the language code of widget to zh-CN.
523+
testStubs.replace(goog, 'LOCALE', 'zh-CN');
524+
createAndInstallTestInstances();
525+
testAuth.install();
526+
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
527+
app.getAuth().assertSetPersistence(['session'], null);
528+
// Set the language code of auth to de.
529+
testAuth.languageCode = 'de';
530+
// Override language code of auth to zh-CN.
531+
app.start(container1, config1);
532+
assertEquals('zh-CN', app.getExternalAuth().languageCode);
533+
app.reset();
534+
// Confirm language code of auth changed back to de.
535+
assertEquals('de', testAuth.languageCode);
536+
}
537+
503538

504539
function testStart_elementNotFound() {
505540
// Test widget start method with missing element.

0 commit comments

Comments
 (0)