Sha256: 9e95e4060262fb37e0e2ad6ca904c36e7771b8a6a09ecbff75d038e9e6e238f0

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
module Decidim
  # This type represents a translated field in multiple languages.
  TranslatedFieldType = GraphQL::ObjectType.define do
    name "TranslatedField"
    description "A translated field"

    field :locales do
      type types[types.String]
      description "Lists all the locales in which this translation is available"
      resolve ->(obj, _args, _ctx) { obj.keys }
    end

    field :translations do
      type !types[LocalizedStringType]
      description "All the localized strings for this translation."

      argument :locales do
        type types[types.String]
        description "A list of locales to scope the translations to."
      end

      resolve lambda { |obj, args, _ctx|
        translations = obj.stringify_keys
        translations = translations.slice(*args["locales"]) if args["locales"]

        translations.map { |locale, text| OpenStruct.new(locale: locale, text: text) }
      }
    end

    field :translation do
      type types.String
      description "Returns a single translation given a locale."
      argument :locale, !types.String, "A locale to search for"

      resolve lambda { |obj, args, _ctx|
        translations = obj.stringify_keys
        translations[args["locale"]]
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-core-0.1.0 lib/decidim/core/api/translated_field_type.rb