<commit_before># frozen_string_literal: true
module Decidim
  module Api
    # This type represents a translated field in multiple languages.
    TranslatedFieldType = GraphQL::ObjectType.define do
      name "TranslatedFieldType"
      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 ->(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 ->(obj, args, _ctx) {
          translations = obj.stringify_keys
          translations[args["locale"]]
        }
      end
    end
  end
end<commit_msg>Make locale mandatory in LocalizedString (#179)<commit_after># frozen_string_literal: true
module Decidim
  module Api
    # This type represents a translated field in multiple languages.
    TranslatedFieldType = GraphQL::ObjectType.define do
      name "TranslatedFieldType"
      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 ->(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 ->(obj, args, _ctx) {
          translations = obj.stringify_keys
          translations[args["locale"]]
        }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07656859269762496, 'fraction_numerical': 0.0010634526763559022, 'mean_word_length': 2.424757281553398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3953257', 'n_tokens_mistral': 740, 'n_tokens_neox': 711, 'n_words': 256}