mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-03 11:47:35 +08:00
Moves the `in_user_locale?` check to the localizable which can be used by any localized model. /t/163217, related https://github.com/discourse/discourse/pull/35152
26 lines
946 B
Ruby
26 lines
946 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Localizable
|
|
extend ActiveSupport::Concern
|
|
|
|
included { has_many :localizations, class_name: "#{model_name}Localization", dependent: :destroy }
|
|
|
|
# Returns the localization for the given locale, or the best match if an exact match is not found.
|
|
# The query used to find the localization is optimized for performance, and assumes
|
|
# that localizations are indexed by locale, and have been preloaded where necessary.
|
|
# @return [Localization, nil] the localization object for the given locale, or nil if no match is found.
|
|
def get_localization(locale = I18n.locale)
|
|
locale_str = locale.to_s.sub("-", "_")
|
|
|
|
# prioritise exact match
|
|
if match = localizations.find { |l| l.locale == locale_str }
|
|
return match
|
|
end
|
|
|
|
localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
|
|
end
|
|
|
|
def in_user_locale?
|
|
LocaleNormalizer.is_same?(locale, I18n.locale)
|
|
end
|
|
end
|