Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
TLDR
This PR offers a solution for URL-based locale handling via middleware.
Background
This PR addresses: #6400 (comment)
rubygems.org supports 9 languages, but locale switching was temporarily disabled in #6399 to address a bot abuse incident. Bots were exploiting the locale system (appending locale strings to URLs) to bypass Fastly's CDN cache entirely and hit the Rails origin directly, causing repeated application crashes.
The previous implementation also stored locale in session cookies in
session[:locale]and read it fromparams[:locale]query parameters, meaning the same URL (/gems/rails) could return different content depending on user state. This made thorough CDN caching effectively impossible, since the cache couldn't treat any URL as a stable, language-specific resource.Approach Details
With this approach:
PATH_INFO/SCRIPT_NAMEare rewritten so Rails can continue routing against the existing unscoped routesApplicationControllersetsI18n.localefrom the middleware-provided valueWhy this approach
One of the main motivations here was to avoid helper churn.
Because the middleware rewrites the request before Rails routing runs, most existing helpers can stay as they are:
During a localized request like
/de/pages/about, those helpers still generate/de/...links becauseSCRIPT_NAMEis set by the middleware.UI Changes
Re-introduced locale switchers:
Alternative Considered: Scoped Routes
The other option is to put locale into the route definitions directly, for example:
That is a more "Rails-native" approach, and we could pivot to it if that feels preferable.
The main tradeoff with scoped routes is helper churn. The problem is that many existing route helpers currently rely on positional arguments, and once
localebecomes part of the route those calls become ambiguous. In practice, helpers likepage_path("about")andrubygem_path("rails")often need to becomepage_path(id: "about")andrubygem_path(id: "rails"), with similar changes across the app and this approach needing to be adopted by contributors going forward.So the tradeoff as I see it is:
Changes Included Here
Gemcutter::Middleware::LocaleFromPathApplicationControllerlocale_switch_pathhelper for locale menu linksNotes / Feedback Requested
I wanted to put up the middleware version first because it keeps the rest of the app much closer to its current shape.
That said, as explained above I think there are two viable directions here:
I’d especially welcome feedback on whether the team would prefer:
Testing