Skip to content

Commit 3c41777

Browse files
committed
fix: scope locale to request via around_action
1 parent aebaebf commit 3c41777

7 files changed

Lines changed: 14 additions & 16 deletions

File tree

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.0
1+
3.3.6

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ ruby ">= 3.1"
55
gem "rails", "~> 7.2.0"
66
gem "puma", ">= 5.0"
77
gem "bootsnap", require: false
8-
gem "tzinfo-data", platforms: %i[windows jruby]
8+
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]
99

1010
group :development do
11-
gem "debug", platforms: %i[mri windows]
11+
gem "debug", platforms: %i[mri mingw mswin x64_mingw]
1212
end

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Demonstrates automated localization for a Ruby on Rails app that authors UI stri
88

99
- `config/locales/en.yml` – source UI strings (English)
1010
- `config/locales/{es,fr,de}.yml` – translated catalogs, generated by Lingo.dev
11-
- `app/controllers/application_controller.rb``set_locale` before_action that picks from URL param or `Accept-Language`
11+
- `app/controllers/application_controller.rb``switch_locale` around_action that picks from URL param or `Accept-Language`
1212
- `app/views/home/index.html.erb` – view demonstrating `t()` interpolation and pluralization
1313
- `i18n.json` – Lingo.dev CLI configuration (`yaml-root-key` bucket)
1414
- `.github/workflows/translate.yml` – GitHub Actions workflow
@@ -24,7 +24,7 @@ Demonstrates automated localization for a Ruby on Rails app that authors UI stri
2424

2525
The Lingo.dev CLI reads `config/locales/en.yml`, identifies new or changed entries using a lockfile, translates the delta through a configured localization engine, and writes per-locale files (`es.yml`, `fr.yml`, `de.yml`) alongside the source. The locale root key (`en:``es:`), nested namespaces, `%{name}` interpolation, and plural categories (`zero`, `one`, `other`) are preserved – only translatable text changes. The GitHub Actions workflow runs this on every push to main.
2626

27-
Rails' built-in i18n API loads every YAML file under `config/locales/` at boot. `ApplicationController#set_locale` picks the request locale from a URL parameter or the `Accept-Language` header, and views render translations with `t()` and `l()` helpers.
27+
Rails' built-in i18n API loads every YAML file under `config/locales/` at boot. `ApplicationController#switch_locale` picks the request locale from a URL parameter or the `Accept-Language` header and wraps the action in `I18n.with_locale`, so views render translations with `t()` and `l()` helpers without leaking the locale across requests.
2828

2929
## Locales
3030

app/controllers/application_controller.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class ApplicationController < ActionController::Base
2-
before_action :set_locale
2+
around_action :switch_locale
33

44
private
55

6-
def set_locale
7-
I18n.locale = params[:locale] ||
8-
http_accept_locale ||
9-
I18n.default_locale
6+
def switch_locale(&action)
7+
locale = params[:locale] || http_accept_locale || I18n.default_locale
8+
I18n.with_locale(locale, &action)
109
end
1110

1211
def http_accept_locale
13-
request.env["HTTP_ACCEPT_LANGUAGE"]
14-
&.scan(/[a-z]{2}/)
15-
&.find { |l| I18n.available_locales.map(&:to_s).include?(l) }
12+
header = request.headers["Accept-Language"].to_s
13+
header.scan(/[a-z]{2}/).find { |l| I18n.available_locales.map(&:to_s).include?(l) }
1614
end
1715

1816
def default_url_options

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
<div class="wordmark"><%= t("app.title") %></div>
167167
<nav class="lang-switcher">
168168
<% I18n.available_locales.each do |locale| %>
169-
<%= link_to locale.upcase, url_for(locale: locale), class: (I18n.locale == locale ? "active" : nil) %>
169+
<%= link_to locale.upcase, url_for(locale: locale), class: ("active" if I18n.locale == locale) %>
170170
<% end %>
171171
</nav>
172172
</header>

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Bundler.require(*Rails.groups)
88

9-
module RailsLocalizationExample
9+
module RubyOnRailsLocalizationExample
1010
class Application < Rails::Application
1111
config.load_defaults 7.2
1212

config/boot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
22

33
require "bundler/setup"
4-
require "bootsnap/setup" if File.exist?(File.expand_path("../Gemfile.lock", __dir__))
4+
require "bootsnap/setup"

0 commit comments

Comments
 (0)