Class: NewsmastMastodon::Api::V1::UserLocalesController

Inherits:
Api::BaseController
  • Object
show all
Includes:
Concerns::ApiResponseHelper
Defined in:
app/controllers/newsmast_mastodon/api/v1/user_locales_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/v1/user_locale/create Saves the user’s locale preference to database using ‘lang’ parameter



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/newsmast_mastodon/api/v1/user_locales_controller.rb', line 13

def create
  locale = locale_params[:lang]&.to_sym
  unless I18n.available_locales.include?(locale)
    render_errors('api.errors.invalid_request', :bad_request, {
      available_locales: I18n.available_locales
    })
    return
  end

  # Update user's locale preference
  if current_user.update(locale: locale.to_s)
    I18n.locale = locale
    render_success({
      locale: I18n.locale,
      message: I18n.t('api.messages.updated')
    })
  else
    render_validation_errors(current_user.errors)
  end
end