From c64cf1436019a4f35afc47fecfa07eff30421e60 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Wed, 30 Jul 2025 14:14:52 -0600 Subject: [PATCH] Avoid redundant locale assignment in `with_locale` If `tmp_locale&.to_sym == self.locale`, then temporarily reassigning `self.locale` is redundant. This small optimisation allows for early return when the locale is already set correctly. --- lib/i18n.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index 9a6a535d..3f2b9812 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -344,11 +344,13 @@ def localize(object, locale: nil, format: nil, **options) # Executes block with given I18n.locale set. def with_locale(tmp_locale = nil) - if tmp_locale == nil + tmp_locale_sym = tmp_locale&.to_sym + + if tmp_locale_sym.nil? || tmp_locale_sym == self.locale yield else current_locale = self.locale - self.locale = tmp_locale + self.locale = tmp_locale_sym begin yield ensure