-
Notifications
You must be signed in to change notification settings - Fork 3k
Add language detection for password-protected link page #3515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ctafsiras
wants to merge
1
commit into
dubinc:main
Choose a base branch
from
ctafsiras:feat/password-language-detection-360
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| export const translations = { | ||
| en: { | ||
| passwordRequired: "Password required", | ||
| description: | ||
| "This link is password protected. Enter the password to view it.", | ||
| whatIsDub: "What is Dub?", | ||
| passwordLabel: "Password", | ||
| incorrectPassword: "Incorrect password", | ||
| viewPage: "View page", | ||
| }, | ||
| zh: { | ||
| passwordRequired: "需要密码", | ||
| description: "此链接受密码保护。请输入密码以查看。", | ||
| whatIsDub: "什么是 Dub?", | ||
| passwordLabel: "密码", | ||
| incorrectPassword: "密码错误", | ||
| viewPage: "查看页面", | ||
| }, | ||
| es: { | ||
| passwordRequired: "Contraseña requerida", | ||
| description: | ||
| "Este enlace está protegido con contraseña. Ingresa la contraseña para verlo.", | ||
| whatIsDub: "¿Qué es Dub?", | ||
| passwordLabel: "Contraseña", | ||
| incorrectPassword: "Contraseña incorrecta", | ||
| viewPage: "Ver página", | ||
| }, | ||
| fr: { | ||
| passwordRequired: "Mot de passe requis", | ||
| description: | ||
| "Ce lien est protégé par un mot de passe. Entrez le mot de passe pour y accéder.", | ||
| whatIsDub: "Qu'est-ce que Dub ?", | ||
| passwordLabel: "Mot de passe", | ||
| incorrectPassword: "Mot de passe incorrect", | ||
| viewPage: "Voir la page", | ||
| }, | ||
| tr: { | ||
| passwordRequired: "Şifre gerekli", | ||
| description: | ||
| "Bu bağlantı şifre korumalıdır. Görüntülemek için şifreyi girin.", | ||
| whatIsDub: "Dub nedir?", | ||
| passwordLabel: "Şifre", | ||
| incorrectPassword: "Yanlış şifre", | ||
| viewPage: "Sayfayı görüntüle", | ||
| }, | ||
| } as const; | ||
|
|
||
| export type Language = keyof typeof translations; | ||
|
|
||
| export function getLanguage(acceptLanguage?: string | null): Language { | ||
| if (!acceptLanguage) return "en"; | ||
|
|
||
| const languages = acceptLanguage | ||
| .toLowerCase() | ||
| .split(",") | ||
| .map((lang) => { | ||
| const [code] = lang.trim().split(";"); | ||
| return code.split("-")[0]; // Extract base language code (e.g., "en" from "en-US") | ||
| }); | ||
|
|
||
| // Check for supported languages in order of preference | ||
| for (const lang of languages) { | ||
| if (lang in translations) { | ||
| return lang as Language; | ||
| } | ||
| } | ||
|
|
||
| // Default to English if no match found | ||
| return "en"; | ||
| } | ||
|
|
||
| export function getTranslations(language: Language) { | ||
| return translations[language]; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getLanguagedoes not compute the actual best match fromAccept-Language.Current logic uses list order only and ignores
qweights, so headers likefr;q=0.8,en;q=0.9can resolve incorrectly. Also, prefer own-key matching overinfor user-derived keys.💡 Suggested fix
export function getLanguage(acceptLanguage?: string | null): Language { if (!acceptLanguage) return "en"; - const languages = acceptLanguage + const languages = acceptLanguage .toLowerCase() .split(",") - .map((lang) => { - const [code] = lang.trim().split(";"); - return code.split("-")[0]; // Extract base language code (e.g., "en" from "en-US") - }); + .map((entry, index) => { + const [rawCode, ...params] = entry.trim().split(";"); + const baseCode = rawCode.split("-")[0]; + const qParam = params.find((p) => p.trim().startsWith("q=")); + const q = qParam ? Number.parseFloat(qParam.split("=")[1]) : 1; + return { + baseCode, + q: Number.isFinite(q) ? q : 0, + index, + }; + }) + .sort((a, b) => b.q - a.q || a.index - b.index); // Check for supported languages in order of preference - for (const lang of languages) { - if (lang in translations) { - return lang as Language; + for (const { baseCode } of languages) { + if (Object.hasOwn(translations, baseCode)) { + return baseCode as Language; } } // Default to English if no match found return "en"; }📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the thorough review! This is a valid observation about
q-weight parsing.However, this
getLanguage()implementation intentionally mirrors the existing pattern in the deeplink page (apps/web/app/app.dub.co/(deeplink)/deeplink/[domain]/[[...key]]/translations.ts), which was merged by the maintainer in PR #3362. Keeping both implementations consistent felt more appropriate than diverging.In practice, most browsers send
Accept-Languageheaders with the preferred language listed first (the order already reflects priority), so the current approach works correctly for the vast majority of real-world cases.Happy to improve
q-weight handling across both locations in a follow-up PR if the maintainers would like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 567
@ctafsiras, thank you for the clarification! I've verified that the implementation indeed matches the existing pattern in the deeplink page, and your reasoning about maintaining consistency makes sense—especially since that approach was already reviewed and merged by the maintainers.You're right that browser behavior typically handles this well in practice, as the preferred language usually appears first in the
Accept-Languageheader.If you'd like to track the potential q-weight improvement for both locations, feel free to open an issue for future consideration. Either way, maintaining consistency with the existing codebase is a reasonable choice here. 👍
✏️ Learnings added
🧠 Learnings used