diff --git a/app/controllers/api/v1/instances/languages_controller.rb b/app/controllers/api/v1/instances/languages_controller.rb new file mode 100644 index 00000000000..17509e748cd --- /dev/null +++ b/app/controllers/api/v1/instances/languages_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class Api::V1::Instances::LanguagesController < Api::BaseController + skip_before_action :require_authenticated_user!, unless: :limited_federation_mode? + skip_around_action :set_locale + + before_action :set_languages + + vary_by '' + + def show + cache_even_if_authenticated! + render json: @languages, each_serializer: REST::LanguageSerializer + end + + private + + def set_languages + @languages = LanguagesHelper::SUPPORTED_LOCALES.keys.map { |code| LanguagePresenter.new(code) } + end +end diff --git a/app/presenters/language_presenter.rb b/app/presenters/language_presenter.rb new file mode 100644 index 00000000000..69ea991d54b --- /dev/null +++ b/app/presenters/language_presenter.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class LanguagePresenter < ActiveModelSerializers::Model + attributes :code, :name, :native_name + + def initialize(code) + super() + + @code = code + @item = LanguagesHelper::SUPPORTED_LOCALES[code] + end + + def name + @item[0] + end + + def native_name + @item[1] + end +end diff --git a/app/serializers/rest/language_serializer.rb b/app/serializers/rest/language_serializer.rb new file mode 100644 index 00000000000..e07956465ce --- /dev/null +++ b/app/serializers/rest/language_serializer.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class REST::LanguageSerializer < ActiveModel::Serializer + attributes :code, :name +end diff --git a/config/routes/api.rb b/config/routes/api.rb index 6db03147b20..48c4c652205 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -120,6 +120,7 @@ namespace :api, format: false do resource :privacy_policy, only: [:show], controller: 'instances/privacy_policies' resource :extended_description, only: [:show], controller: 'instances/extended_descriptions' resource :translation_languages, only: [:show], controller: 'instances/translation_languages' + resource :languages, only: [:show], controller: 'instances/languages' resource :activity, only: [:show], controller: 'instances/activity' end diff --git a/spec/requests/api/v1/instances/languages_spec.rb b/spec/requests/api/v1/instances/languages_spec.rb new file mode 100644 index 00000000000..8ab8bf99ce5 --- /dev/null +++ b/spec/requests/api/v1/instances/languages_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Languages' do + describe 'GET /api/v1/instance/languages' do + before do + get '/api/v1/instance/languages' + end + + it 'returns http success' do + expect(response).to have_http_status(200) + end + + it 'returns the supported languages' do + expect(body_as_json.pluck(:code)).to match_array LanguagesHelper::SUPPORTED_LOCALES.keys.map(&:to_s) + end + end +end