Skip to content

Commit c64cf14

Browse files
committed
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.
1 parent 9ede664 commit c64cf14

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/i18n.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,13 @@ def localize(object, locale: nil, format: nil, **options)
344344

345345
# Executes block with given I18n.locale set.
346346
def with_locale(tmp_locale = nil)
347-
if tmp_locale == nil
347+
tmp_locale_sym = tmp_locale&.to_sym
348+
349+
if tmp_locale_sym.nil? || tmp_locale_sym == self.locale
348350
yield
349351
else
350352
current_locale = self.locale
351-
self.locale = tmp_locale
353+
self.locale = tmp_locale_sym
352354
begin
353355
yield
354356
ensure

0 commit comments

Comments
 (0)