Skip to content

Commit 5ccb8a4

Browse files
committed
Refactor translation management interface
- Simplified the translations index view by removing unnecessary locale filter tabs and replacing them with a more structured tab navigation system. - Introduced lazy loading for translation data by locale, model type, data type, and attribute. - Enhanced the locale switcher to display formatted locale names. - Updated locale files (English, Spanish, French) with new keys and translations for the revamped translation management features. - Added new routes for fetching translations by locale, model type, data type, and attribute. - Implemented request specs for translation management to ensure proper rendering and filtering functionality.
1 parent b2c8286 commit 5ccb8a4

File tree

17 files changed

+2664
-305
lines changed

17 files changed

+2664
-305
lines changed

app/controllers/better_together/application_controller.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,14 @@ def extract_locale_from_accept_language_header
214214
end
215215

216216
def set_locale
217-
locale = params[:locale] || # Request parameter
218-
session[:locale] || # Session stored locale
219-
helpers.current_person&.locale || # Model saved configuration
220-
extract_locale_from_accept_language_header || # Language header - browser config
221-
I18n.default_locale # Set in your config files, english by super-default
217+
raw_locale = params[:locale] || # Request parameter
218+
session[:locale] || # Session stored locale
219+
helpers.current_person&.locale || # Model saved configuration
220+
extract_locale_from_accept_language_header || # Language header - browser config
221+
I18n.default_locale # Set in your config files, english by super-default
222+
223+
# Normalize and validate locale to prevent I18n::InvalidLocale errors
224+
locale = normalize_locale(raw_locale)
222225

223226
I18n.locale = locale
224227
session[:locale] = locale # Store the locale in the session
@@ -280,5 +283,28 @@ def determine_layout
280283
def turbo_native_app?
281284
request.user_agent.to_s.include?('Turbo Native')
282285
end
286+
287+
# Normalize locale parameter to prevent I18n::InvalidLocale errors
288+
# @param raw_locale [String, Symbol, nil] The raw locale value to normalize
289+
# @return [String] A valid, normalized locale string
290+
def normalize_locale(raw_locale)
291+
return I18n.default_locale.to_s if raw_locale.nil?
292+
293+
# Convert to string and normalize case
294+
candidate_locale = raw_locale.to_s.downcase.strip
295+
296+
# Check if it's a valid available locale
297+
available_locales = I18n.available_locales.map(&:to_s)
298+
if available_locales.include?(candidate_locale)
299+
candidate_locale
300+
else
301+
# Try to find a partial match (e.g., 'en-US' -> 'en')
302+
partial_match = available_locales.find { |loc| candidate_locale.start_with?(loc) }
303+
partial_match || I18n.default_locale.to_s
304+
end
305+
rescue StandardError => e
306+
Rails.logger.warn("Error normalizing locale '#{raw_locale}': #{e.message}")
307+
I18n.default_locale.to_s
308+
end
283309
end
284310
end

0 commit comments

Comments
 (0)