Sha256: 3a7a3cc24007c6979f3839d6b82cd076fc9f204912674c22af282eade41b89cc

Contents?: true

Size: 1.26 KB

Versions: 37

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

37 entries across 37 versions & 2 rubygems

Version Path
decidim-core-0.9.3 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.9.2 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.9.1 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.9.0 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.8.4 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.8.3 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.8.2 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.8.1 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.8.0 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.7.4 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.7.3 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.7.2 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.7.1 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.7.0 lib/decidim/core/api/translated_field_type.rb
decidim-core-0.6.8 lib/decidim/core/api/translated_field_type.rb
decidim-0.6.8 decidim-core/lib/decidim/core/api/translated_field_type.rb
decidim-core-0.6.7 lib/decidim/core/api/translated_field_type.rb
decidim-0.6.7 decidim-core/lib/decidim/core/api/translated_field_type.rb
decidim-core-0.6.6 lib/decidim/core/api/translated_field_type.rb
decidim-0.6.6 decidim-core/lib/decidim/core/api/translated_field_type.rb