Skip to content

Commit ec4ffa7

Browse files
authored
Merge pull request #1179 from firebase/@invertase/align-translations
2 parents ea7089b + 8daea56 commit ec4ffa7

32 files changed

+1661
-1018
lines changed

examples/nextjs/lib/firebase/clientApp.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { initializeApp, getApps } from "firebase/app";
2020
import { firebaseConfig } from "./config";
2121
import { connectAuthEmulator, getAuth } from "firebase/auth";
2222
import { autoAnonymousLogin, initializeUI } from "@firebase-ui/core";
23-
import { customLanguage, english } from "@firebase-ui/translations";
2423

2524
export const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
2625

@@ -29,19 +28,6 @@ export const auth = getAuth(firebaseApp);
2928
export const ui = initializeUI({
3029
app: firebaseApp,
3130
behaviors: [autoAnonymousLogin()],
32-
translations: [
33-
customLanguage(english.locale, {
34-
labels: {
35-
signIn: "Sign In",
36-
},
37-
prompts: {
38-
signInToAccount: "Sign in to your account",
39-
},
40-
errors: {
41-
invalidEmail: "Please enter a valid email address",
42-
},
43-
}),
44-
],
4531
});
4632

4733
connectAuthEmulator(auth, "http://localhost:9099");

examples/react/lib/firebase/clientApp.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { initializeApp, getApps } from "firebase/app";
2020
import { firebaseConfig } from "./config";
2121
import { connectAuthEmulator, getAuth } from "firebase/auth";
2222
import { autoAnonymousLogin, initializeUI } from "@firebase-ui/core";
23-
import { customLanguage, english } from "@firebase-ui/translations";
2423

2524
export const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
2625

@@ -29,19 +28,6 @@ export const auth = getAuth(firebaseApp);
2928
export const ui = initializeUI({
3029
app: firebaseApp,
3130
behaviors: [autoAnonymousLogin()],
32-
translations: [
33-
customLanguage(english.locale, {
34-
labels: {
35-
signIn: "Sign In",
36-
},
37-
prompts: {
38-
signInToAccount: "Sign in to your account",
39-
},
40-
errors: {
41-
invalidEmail: "Please enter a valid email address",
42-
},
43-
}),
44-
],
4531
});
4632

4733
if (import.meta.env.MODE === "development") {

packages/angular/src/lib/auth/forms/email-link-form/email-link-form.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
FirebaseUIError,
2626
completeEmailLinkSignIn,
2727
sendSignInLinkToEmail,
28+
FirebaseUIConfiguration,
2829
} from "@firebase-ui/core";
2930
import { firstValueFrom } from "rxjs";
3031

@@ -74,7 +75,7 @@ export class EmailLinkFormComponent implements OnInit {
7475
formError: string | null = null;
7576
emailSent = false;
7677
private formSchema: any;
77-
private config: any;
78+
private config: FirebaseUIConfiguration;
7879

7980
form = injectForm({
8081
defaultValues: {
@@ -86,7 +87,7 @@ export class EmailLinkFormComponent implements OnInit {
8687
try {
8788
this.config = await firstValueFrom(this.ui.config());
8889

89-
this.formSchema = createEmailLinkFormSchema(this.config?.translations);
90+
this.formSchema = createEmailLinkFormSchema(this.config);
9091

9192
this.form.update({
9293
validators: {

packages/angular/src/lib/auth/forms/email-password-form/email-password-form.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { injectForm, TanStackField } from "@tanstack/angular-form";
2020
import { FirebaseUI } from "../../../provider";
2121
import { ButtonComponent } from "../../../components/button/button.component";
2222
import { TermsAndPrivacyComponent } from "../../../components/terms-and-privacy/terms-and-privacy.component";
23-
import { createEmailFormSchema, EmailFormSchema, FirebaseUIError, signInWithEmailAndPassword } from "@firebase-ui/core";
23+
import { createEmailFormSchema, EmailFormSchema, FirebaseUIConfiguration, FirebaseUIError, signInWithEmailAndPassword } from "@firebase-ui/core";
2424
import { firstValueFrom } from "rxjs";
2525
import { Router } from "@angular/router";
2626

@@ -105,7 +105,7 @@ export class EmailPasswordFormComponent implements OnInit {
105105

106106
formError: string | null = null;
107107
private formSchema: any;
108-
private config: any;
108+
private config: FirebaseUIConfiguration;
109109

110110
form = injectForm({
111111
defaultValues: {
@@ -120,7 +120,7 @@ export class EmailPasswordFormComponent implements OnInit {
120120
this.config = await firstValueFrom(this.ui.config());
121121

122122
// Create schema once
123-
this.formSchema = createEmailFormSchema(this.config?.translations);
123+
this.formSchema = createEmailFormSchema(this.config);
124124

125125
// Apply schema to form validators
126126
this.form.update({

packages/angular/src/lib/auth/forms/forgot-password-form/forgot-password-form.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { FirebaseUI } from "../../../provider";
2121
import { Auth } from "@angular/fire/auth";
2222
import { ButtonComponent } from "../../../components/button/button.component";
2323
import { TermsAndPrivacyComponent } from "../../../components/terms-and-privacy/terms-and-privacy.component";
24-
import { createForgotPasswordFormSchema, FirebaseUIError, sendPasswordResetEmail } from "@firebase-ui/core";
24+
import { createForgotPasswordFormSchema, FirebaseUIConfiguration, FirebaseUIError, sendPasswordResetEmail } from "@firebase-ui/core";
2525
import { firstValueFrom } from "rxjs";
2626
import { Router } from "@angular/router";
2727

@@ -80,7 +80,7 @@ export class ForgotPasswordFormComponent implements OnInit {
8080
formError: string | null = null;
8181
emailSent = false;
8282
private formSchema: any;
83-
private config: any;
83+
private config: FirebaseUIConfiguration;
8484

8585
form = injectForm({
8686
defaultValues: {
@@ -92,7 +92,7 @@ export class ForgotPasswordFormComponent implements OnInit {
9292
try {
9393
this.config = await firstValueFrom(this.ui.config());
9494

95-
this.formSchema = createForgotPasswordFormSchema(this.config?.translations);
95+
this.formSchema = createForgotPasswordFormSchema(this.config);
9696

9797
this.form.update({
9898
validators: {

packages/angular/src/lib/auth/forms/phone-form/phone-form.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
formatPhoneNumberWithCountry,
3232
confirmPhoneNumber,
3333
signInWithPhoneNumber,
34+
FirebaseUIConfiguration,
3435
} from "@firebase-ui/core";
3536
import { interval, Subscription, firstValueFrom } from "rxjs";
3637
import { Router } from "@angular/router";
@@ -102,7 +103,7 @@ export class PhoneNumberFormComponent implements OnInit, OnDestroy {
102103
recaptchaVerifier: RecaptchaVerifier | null = null;
103104
selectedCountry: CountryData = countryData[0];
104105
private formSchema: any;
105-
private config: any;
106+
private config: FirebaseUIConfiguration;
106107

107108
form = injectForm({
108109
defaultValues: {
@@ -114,7 +115,7 @@ export class PhoneNumberFormComponent implements OnInit, OnDestroy {
114115
try {
115116
this.config = await firstValueFrom(this.ui.config());
116117

117-
this.formSchema = createPhoneFormSchema(this.config?.translations).pick({
118+
this.formSchema = createPhoneFormSchema(this.config).pick({
118119
phoneNumber: true,
119120
});
120121

packages/angular/src/lib/auth/forms/register-form/register-form.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
EmailFormSchema,
2525
FirebaseUIError,
2626
createUserWithEmailAndPassword,
27+
FirebaseUIConfiguration,
2728
} from "@firebase-ui/core";
2829
import { Auth } from "@angular/fire/auth";
2930
import { TermsAndPrivacyComponent } from "../../../components/terms-and-privacy/terms-and-privacy.component";
@@ -105,7 +106,7 @@ export class RegisterFormComponent implements OnInit {
105106

106107
formError: string | null = null;
107108
private formSchema: any;
108-
private config: any;
109+
private config: FirebaseUIConfiguration;
109110

110111
form = injectForm({
111112
defaultValues: {
@@ -118,7 +119,7 @@ export class RegisterFormComponent implements OnInit {
118119
try {
119120
this.config = await firstValueFrom(this.ui.config());
120121

121-
this.formSchema = createEmailFormSchema(this.config?.translations);
122+
this.formSchema = createEmailFormSchema(this.config);
122123

123124
this.form.update({
124125
validators: {

packages/angular/tsconfig.lib.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"declaration": true,
88
"declarationMap": true,
99
"inlineSources": true,
10-
"types": []
10+
"types": [],
11+
"baseUrl": ".",
12+
"paths": {
13+
"@firebase-ui/core": ["../core/src/index.ts"],
14+
"@firebase-ui/translations": ["../translations/src/index.ts"]
15+
}
1116
},
1217
"exclude": ["**/*.spec.ts"]
1318
}

packages/core/src/config.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { english, Locale, RegisteredTranslations, TranslationsConfig } from "@firebase-ui/translations";
17+
import { enUs, RegisteredLocale } from "@firebase-ui/translations";
1818
import type { FirebaseApp } from "firebase/app";
1919
import { Auth, getAuth } from "firebase/auth";
2020
import { deepMap, DeepMapStore, map } from "nanostores";
@@ -23,20 +23,18 @@ import { FirebaseUIState } from "./state";
2323

2424
type FirebaseUIConfigurationOptions = {
2525
app: FirebaseApp;
26-
locale?: Locale | undefined;
27-
translations?: RegisteredTranslations[] | undefined;
28-
behaviors?: Partial<Behavior<keyof BehaviorHandlers>>[] | undefined;
29-
recaptchaMode?: "normal" | "invisible" | undefined;
26+
locale?: RegisteredLocale;
27+
behaviors?: Partial<Behavior<keyof BehaviorHandlers>>[];
28+
recaptchaMode?: "normal" | "invisible";
3029
};
3130

3231
export type FirebaseUIConfiguration = {
3332
app: FirebaseApp;
3433
getAuth: () => Auth;
35-
setLocale: (locale: Locale) => void;
34+
setLocale: (locale: RegisteredLocale) => void;
3635
state: FirebaseUIState;
3736
setState: (state: FirebaseUIState) => void;
38-
locale: Locale;
39-
translations: TranslationsConfig;
37+
locale: RegisteredLocale;
4038
behaviors: Partial<Record<BehaviorKey, BehaviorHandlers[BehaviorKey]>>;
4139
recaptchaMode: "normal" | "invisible";
4240
};
@@ -57,25 +55,13 @@ export function initializeUI(config: FirebaseUIConfigurationOptions, name: strin
5755
{} as Record<BehaviorKey, BehaviorHandlers[BehaviorKey]>
5856
);
5957

60-
config.translations ??= [];
61-
62-
// TODO: Is this right?
63-
config.translations.push(english);
64-
65-
const translations = config.translations?.reduce((acc, translation) => {
66-
return {
67-
...acc,
68-
[translation.locale]: translation.translations,
69-
};
70-
}, {} as TranslationsConfig);
71-
7258
$config.setKey(
7359
name,
7460
deepMap<FirebaseUIConfiguration>({
7561
app: config.app,
7662
getAuth: () => getAuth(config.app),
77-
locale: config.locale ?? english.locale,
78-
setLocale: (locale: Locale) => {
63+
locale: config.locale ?? enUs,
64+
setLocale: (locale: RegisteredLocale) => {
7965
const current = $config.get()[name]!;
8066
current.setKey(`locale`, locale);
8167
},
@@ -84,7 +70,6 @@ export function initializeUI(config: FirebaseUIConfigurationOptions, name: strin
8470
const current = $config.get()[name]!;
8571
current.setKey(`state`, state);
8672
},
87-
translations,
8873
behaviors: behaviors ?? {},
8974
recaptchaMode: config.recaptchaMode ?? "normal",
9075
})

packages/core/src/errors.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
english,
19-
ERROR_CODE_MAP,
20-
ErrorCode,
21-
getTranslation,
22-
Locale,
23-
TranslationsConfig,
24-
} from "@firebase-ui/translations";
17+
import { ERROR_CODE_MAP, ErrorCode } from "@firebase-ui/translations";
18+
import { getTranslation } from "./translations";
2519
import { FirebaseUIConfiguration } from "./config";
2620
export class FirebaseUIError extends Error {
2721
code: string;
2822

29-
constructor(error: any, translations?: TranslationsConfig, locale?: Locale) {
23+
constructor(error: any, ui: FirebaseUIConfiguration) {
3024
const errorCode: ErrorCode = error?.customData?.message?.match?.(/\(([^)]+)\)/)?.at(1) || error?.code || "unknown";
3125
const translationKey = ERROR_CODE_MAP[errorCode] || "unknownError";
32-
const message = getTranslation("errors", translationKey, translations, locale ?? english.locale);
26+
const message = getTranslation(ui, "errors", translationKey);
3327

3428
super(message);
3529
this.name = "FirebaseUIError";
@@ -44,7 +38,6 @@ export function handleFirebaseError(
4438
enableHandleExistingCredential?: boolean;
4539
}
4640
): never {
47-
const { translations, locale: defaultLocale } = ui;
4841
if (error?.code === "auth/account-exists-with-different-credential") {
4942
if (opts?.enableHandleExistingCredential && error.credential) {
5043
window.sessionStorage.setItem("pendingCred", JSON.stringify(error.credential));
@@ -59,14 +52,13 @@ export function handleFirebaseError(
5952
email: error.customData?.email,
6053
},
6154
},
62-
translations,
63-
defaultLocale
55+
ui,
6456
);
6557
}
6658

6759
// TODO: Debug why instanceof FirebaseError is not working
6860
if (error?.name === "FirebaseError") {
69-
throw new FirebaseUIError(error, translations, defaultLocale);
61+
throw new FirebaseUIError(error, ui);
7062
}
71-
throw new FirebaseUIError({ code: "unknown" }, translations, defaultLocale);
63+
throw new FirebaseUIError({ code: "unknown" }, ui);
7264
}

0 commit comments

Comments
 (0)