Files
grecia/app/components/layout/locale_switcher_component.rb
Javi Martín 1d955b7a20 Simplify using helper methods in components
The `use_helpers` method was added in ViewComponent 3.8.0, and it's
included by default in all components since version 3.11.0.

Note we sometimes delegated the `can?` method to the controller instead
of the helpers, for no particularly reason. We're unifying that code as
well.
2024-03-02 17:34:25 +01:00

44 lines
856 B
Ruby

class Layout::LocaleSwitcherComponent < ApplicationComponent
use_helpers :name_for_locale, :link_list, :current_path_with_query_params, :rtl?
def render?
locales.size > 1
end
private
def many_locales?
locales.size > 4
end
def locales
I18n.available_locales
end
def label
t("layouts.header.locale")
end
def label_id
"locale_switcher_label"
end
def language_links
locales.map do |locale|
[
name_for_locale(locale),
current_path_with_query_params(locale: locale),
locale == I18n.locale,
lang: locale,
data: { turbolinks: rtl?(I18n.locale) == rtl?(locale) }
]
end
end
def language_options
language_links.map do |text, path, _, options|
[text, path, options]
end
end
end