Skip to content

Commit 79a48fe

Browse files
committed
chore: Add jsdoc to translations
1 parent 641f512 commit 79a48fe

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

packages/translations/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ import { type Translations } from "./types";
2020
export type * from "./types";
2121
export * from "./mapping";
2222

23+
/** Locale identifier string. Supports BCP 47 format (e.g., "en-US") or any string. */
2324
export type Locale = "en-US" | `${string}-${string}` | string;
2425

26+
/**
27+
* Registers a locale with its translations and optional fallback locale.
28+
*
29+
* @param locale - The locale identifier (e.g., "en-US", "fr-FR").
30+
* @param translations - The translation object for this locale.
31+
* @param fallback - Optional fallback locale to use when a translation is missing.
32+
* @returns A registered locale object.
33+
*/
2534
export function registerLocale(
2635
locale: Locale,
2736
translations: Translations,
@@ -34,10 +43,15 @@ export function registerLocale(
3443
};
3544
}
3645

46+
/** Pre-registered English US locale with default translations. */
3747
export const enUs = registerLocale("en-US", enUS);
3848

49+
/** A registered locale with its translations and optional fallback. */
3950
export type RegisteredLocale = {
51+
/** The locale identifier. */
4052
locale: Locale;
53+
/** The translation object for this locale. */
4154
translations: Translations;
55+
/** Optional fallback locale to use when a translation is missing. */
4256
fallback?: RegisteredLocale;
4357
};

packages/translations/src/locales/en-us.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { type Translations } from "../types";
1818

19+
/** English US (en-US) translation set with all default translations. */
1920
export const enUS = {
2021
errors: {
2122
userNotFound: "No account found with this email address",

packages/translations/src/mapping.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { enUS } from "./locales/en-us";
1818
import { type RegisteredLocale } from ".";
1919
import type { ErrorKey, TranslationCategory, TranslationKey, TranslationSet } from "./types";
2020

21+
/** Maps Firebase authentication error codes to translation keys. */
2122
export const ERROR_CODE_MAP = {
2223
"auth/user-not-found": "userNotFound",
2324
"auth/wrong-password": "wrongPassword",
@@ -48,8 +49,21 @@ export const ERROR_CODE_MAP = {
4849
"auth/second-factor-already-in-use": "secondFactorAlreadyInUse",
4950
} satisfies Record<string, ErrorKey>;
5051

52+
/** Firebase authentication error code type. */
5153
export type ErrorCode = keyof typeof ERROR_CODE_MAP;
5254

55+
/**
56+
* Retrieves a translation string for a given locale, category, and key.
57+
*
58+
* Falls back to the locale's fallback locale or English US if the translation is not found.
59+
* Supports string replacements using {placeholder} syntax.
60+
*
61+
* @param locale - The registered locale to get the translation from.
62+
* @param category - The translation category (e.g., "errors", "labels").
63+
* @param key - The translation key within the category.
64+
* @param replacements - Optional object with replacement values for placeholders in the translation string.
65+
* @returns The translated string, or an empty string if not found.
66+
*/
5367
export function getTranslation<T extends TranslationCategory>(
5468
locale: RegisteredLocale,
5569
category: T,

packages/translations/src/types.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,103 +14,204 @@
1414
* limitations under the License.
1515
*/
1616

17+
/** Category keys for translation sets (e.g., "errors", "messages", "labels", "prompts"). */
1718
export type TranslationCategory = keyof Required<Translations>;
19+
20+
/** Generic type for translation keys within a specific category. */
1821
export type TranslationKey<T extends TranslationCategory> = keyof Required<Translations>[T];
22+
23+
/** Record type representing a complete set of translations for a specific category. */
1924
export type TranslationSet<T extends TranslationCategory> = Record<TranslationKey<T>, string>;
25+
26+
/** Keys for error translation messages. */
2027
export type ErrorKey = keyof Required<Translations>["errors"];
28+
29+
/** Keys for informational message translations. */
2130
export type MessageKey = keyof Required<Translations>["messages"];
31+
32+
/** Keys for UI label translations. */
2233
export type LabelKey = keyof Required<Translations>["labels"];
34+
35+
/** Keys for prompt/instruction translations. */
2336
export type PromptKey = keyof Required<Translations>["prompts"];
37+
38+
/** Configuration type for translations, mapping locale identifiers to partial translation objects. */
2439
export type TranslationsConfig = Partial<Record<string, Partial<Translations>>>;
2540

41+
/** Complete translations interface containing all translation categories and their keys. */
2642
export type Translations = {
43+
/** Error message translations. */
2744
errors?: {
45+
/** Translation for when a user is not found. */
2846
userNotFound?: string;
47+
/** Translation for incorrect password. */
2948
wrongPassword?: string;
49+
/** Translation for invalid email address. */
3050
invalidEmail?: string;
51+
/** Translation for disabled user account. */
3152
userDisabled?: string;
53+
/** Translation for unverified email address. */
3254
unverifiedEmail?: string;
55+
/** Translation for network request failure. */
3356
networkRequestFailed?: string;
57+
/** Translation for too many requests. */
3458
tooManyRequests?: string;
59+
/** Translation for email already in use. */
3560
emailAlreadyInUse?: string;
61+
/** Translation for missing verification code. */
3662
missingVerificationCode?: string;
63+
/** Translation for invalid credentials. */
3764
invalidCredential?: string;
65+
/** Translation for weak password. */
3866
weakPassword?: string;
67+
/** Translation for operation not allowed. */
3968
operationNotAllowed?: string;
69+
/** Translation for invalid phone number. */
4070
invalidPhoneNumber?: string;
71+
/** Translation for missing phone number. */
4172
missingPhoneNumber?: string;
73+
/** Translation for SMS quota exceeded. */
4274
quotaExceeded?: string;
75+
/** Translation for expired verification code. */
4376
codeExpired?: string;
77+
/** Translation for reCAPTCHA check failure. */
4478
captchaCheckFailed?: string;
79+
/** Translation for missing verification ID. */
4580
missingVerificationId?: string;
81+
/** Translation for missing email address. */
4682
missingEmail?: string;
83+
/** Translation for required display name. */
4784
displayNameRequired?: string;
85+
/** Translation for invalid action code. */
4886
invalidActionCode?: string;
87+
/** Translation for credential already in use. */
4988
credentialAlreadyInUse?: string;
89+
/** Translation for operation requiring recent login. */
5090
requiresRecentLogin?: string;
91+
/** Translation for provider already linked. */
5192
providerAlreadyLinked?: string;
93+
/** Translation for invalid verification code. */
5294
invalidVerificationCode?: string;
95+
/** Translation for unknown error. */
5396
unknownError?: string;
97+
/** Translation for popup closed by user. */
5498
popupClosed?: string;
99+
/** Translation for account existing with different credential. */
55100
accountExistsWithDifferentCredential?: string;
101+
/** Translation for second factor already in use. */
56102
secondFactorAlreadyInUse?: string;
57103
};
104+
/** Informational message translations. */
58105
messages?: {
106+
/** Translation for password reset email sent confirmation. */
59107
passwordResetEmailSent?: string;
108+
/** Translation for sign-in link sent confirmation. */
60109
signInLinkSent?: string;
110+
/** Translation for verification code required first. */
61111
verificationCodeFirst?: string;
112+
/** Translation for checking email for reset instructions. */
62113
checkEmailForReset?: string;
114+
/** Translation for "or" divider text. */
63115
dividerOr?: string;
116+
/** Translation for terms and privacy policy notice. */
64117
termsAndPrivacy?: string;
118+
/** Translation for MFA SMS assertion prompt message. */
65119
mfaSmsAssertionPrompt?: string;
66120
};
121+
/** UI label translations. */
67122
labels?: {
123+
/** Translation for email address label. */
68124
emailAddress?: string;
125+
/** Translation for password label. */
69126
password?: string;
127+
/** Translation for display name label. */
70128
displayName?: string;
129+
/** Translation for forgot password link. */
71130
forgotPassword?: string;
131+
/** Translation for sign up button/link. */
72132
signUp?: string;
133+
/** Translation for sign in button/link. */
73134
signIn?: string;
135+
/** Translation for reset password button. */
74136
resetPassword?: string;
137+
/** Translation for create account button. */
75138
createAccount?: string;
139+
/** Translation for back to sign in link. */
76140
backToSignIn?: string;
141+
/** Translation for sign in with phone button. */
77142
signInWithPhone?: string;
143+
/** Translation for phone number label. */
78144
phoneNumber?: string;
145+
/** Translation for verification code label. */
79146
verificationCode?: string;
147+
/** Translation for send code button. */
80148
sendCode?: string;
149+
/** Translation for verify code button. */
81150
verifyCode?: string;
151+
/** Translation for sign in with Google button. */
82152
signInWithGoogle?: string;
153+
/** Translation for sign in with Facebook button. */
83154
signInWithFacebook?: string;
155+
/** Translation for sign in with Apple button. */
84156
signInWithApple?: string;
157+
/** Translation for sign in with Twitter/X button. */
85158
signInWithTwitter?: string;
159+
/** Translation for sign in with Microsoft button. */
86160
signInWithMicrosoft?: string;
161+
/** Translation for sign in with GitHub button. */
87162
signInWithGitHub?: string;
163+
/** Translation for sign in with email link button. */
88164
signInWithEmailLink?: string;
165+
/** Translation for send sign-in link button. */
89166
sendSignInLink?: string;
167+
/** Translation for terms of service link. */
90168
termsOfService?: string;
169+
/** Translation for privacy policy link. */
91170
privacyPolicy?: string;
171+
/** Translation for resend code button. */
92172
resendCode?: string;
173+
/** Translation for sending state text. */
93174
sending?: string;
175+
/** Translation for multi-factor enrollment label. */
94176
multiFactorEnrollment?: string;
177+
/** Translation for multi-factor assertion label. */
95178
multiFactorAssertion?: string;
179+
/** Translation for TOTP verification label. */
96180
mfaTotpVerification?: string;
181+
/** Translation for SMS verification label. */
97182
mfaSmsVerification?: string;
183+
/** Translation for generate QR code button. */
98184
generateQrCode?: string;
99185
};
186+
/** Prompt and instruction translations. */
100187
prompts?: {
188+
/** Translation for "don't have an account" prompt. */
101189
noAccount?: string;
190+
/** Translation for "already have an account" prompt. */
102191
haveAccount?: string;
192+
/** Translation for enter email to reset password prompt. */
103193
enterEmailToReset?: string;
194+
/** Translation for sign in to account prompt. */
104195
signInToAccount?: string;
196+
/** Translation for enter details to create account prompt. */
105197
enterDetailsToCreate?: string;
198+
/** Translation for SMS verification code prompt. */
106199
smsVerificationPrompt?: string;
200+
/** Translation for enter phone number prompt. */
107201
enterPhoneNumber?: string;
202+
/** Translation for enter verification code prompt. */
108203
enterVerificationCode?: string;
204+
/** Translation for enter email for link prompt. */
109205
enterEmailForLink?: string;
206+
/** Translation for MFA enrollment prompt. */
110207
mfaEnrollmentPrompt?: string;
208+
/** Translation for MFA assertion prompt. */
111209
mfaAssertionPrompt?: string;
210+
/** Translation for MFA assertion factor selection prompt. */
112211
mfaAssertionFactorPrompt?: string;
212+
/** Translation for TOTP QR code prompt. */
113213
mfaTotpQrCodePrompt?: string;
214+
/** Translation for TOTP enrollment verification prompt. */
114215
mfaTotpEnrollmentVerificationPrompt?: string;
115216
};
116217
};

0 commit comments

Comments
 (0)