Files
grecia/app/lib/search_dictionary_selector.rb
Javi Martín 6de4737b70 Allow different default locales per tenant
Note that, for everything to work consistently, we need to make sure
that the default locale is one of the available locales.

Also note that we aren't overwriting the `#save ` method set by
globalize. I didn't feel too comfortable changing a monkey-patch which
ideally shouldn't be there in the first place, I haven't found a case
where `Globalize.locale` is `nil` (since it defaults to `I18n.locale`,
which should never be `nil`), so using `I18n.default_locale` probably
doesn't affect us.
2024-06-05 16:10:56 +02:00

53 lines
1.1 KiB
Ruby

module SearchDictionarySelector
SQL_QUERY = "SELECT cfgname FROM pg_ts_config".freeze
I18N_TO_DICTIONARY = {
ar: "arabic",
ca: "catalan",
de: "german",
dk: "danish",
el: "greek",
en: "english",
es: "spanish",
eu: "basque",
fi: "finnish",
fr: "french",
ga: "irish",
hu: "hungarian",
id: "indonesian",
it: "italian",
lt: "lithuanian",
nb: "norwegian",
ne: "nepali",
nl: "dutch",
nn: "norwegian",
pt: "portuguese",
ro: "romanian",
ru: "russian",
sr: "serbian",
sv: "swedish",
tr: "turkish",
yi: "yiddish"
}.freeze
class << self
def call
find_from_i18n_default
end
private
def find_from_i18n_default
key_to_lookup = Setting.default_locale.to_s.split("-").first.to_sym
dictionary = I18N_TO_DICTIONARY[key_to_lookup]
dictionary ||= "simple"
available_dictionaries.include?(dictionary) ? dictionary : available_dictionaries.first
end
def available_dictionaries
result = ActiveRecord::Base.connection.execute(SQL_QUERY)
result.to_a.map { |row| row["cfgname"] }
end
end
end