discourse/app/services/themes/get_translations.rb
Martin Brennan 4b947d2404
DEV: Partially refactor themes controller to services (#33141)
This commit starts to introduce services to replace actions in the
ThemesController. We will start with the low langing fruit:

* Create
* Destroy
* BulkDestroy
* GetTranslations

Endpoints like #import and #update will be much harder.
Also, the https://github.com/discourse/discourse-theme-creator plugin
overrides some of these controller actions directly, so we need
to be careful.
2025-06-23 10:14:58 +10:00

50 lines
1.2 KiB
Ruby

# frozen_string_literal: true
# Gets all of the translation overrides for a theme, which are defined
# in locale yml files for a theme. A ThemeField is created for each locale,
# which in turn creates ThemeTranslationOverride records.
#
# @example
# Themes::GetTranslations.call(
# guardian: guardian,
# params: {
# id: theme.id,
# locale: "en",
# }
# )
#
class Themes::GetTranslations
include Service::Base
params do
attribute :locale, :string
attribute :id, :integer
validates :id, presence: true
validates :locale, presence: true
validate :validate_locale, if: -> { locale.present? }
def validate_locale
return if I18n.available_locales.include?(locale.to_sym)
errors.add(:base, I18n.t("errors.messages.invalid_locale", invalid_locale: locale))
end
end
model :theme
model :translations
private
def fetch_theme(params:)
Theme.find_by(id: params.id)
end
def fetch_translations(theme:, params:)
I18n.with_locale(params.locale) do
theme.translations.map do |translation|
{ key: translation.key, value: translation.value, default: translation.default }
end
end
end
end